2015-03-23 17:32:56 +01:00
|
|
|
/** Logging and debugging routines
|
|
|
|
*
|
|
|
|
* @file
|
2015-06-02 21:53:04 +02:00
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-16 09:04:12 -03:00
|
|
|
#pragma once
|
2015-03-21 18:03:29 +01:00
|
|
|
|
2017-04-26 11:52:22 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-06-03 10:13:35 +02:00
|
|
|
#include <stdarg.h>
|
2017-03-11 23:50:30 -03:00
|
|
|
#include <time.h>
|
2017-07-12 00:56:08 +02:00
|
|
|
#include <sys/ioctl.h>
|
2015-03-31 14:27:38 +02:00
|
|
|
|
2017-09-16 11:32:04 +02:00
|
|
|
#include "advio.h"
|
2017-03-11 23:50:30 -03:00
|
|
|
#include "common.h"
|
2017-04-26 11:52:22 +02:00
|
|
|
#include "log_config.h"
|
2016-06-19 19:23:19 +02:00
|
|
|
|
2015-03-31 15:02:51 +02:00
|
|
|
/* The log level which is passed as first argument to print() */
|
2017-07-12 00:50:29 +02:00
|
|
|
#define LOG_LVL_DEBUG CLR_GRY("Debug")
|
|
|
|
#define LOG_LVL_INFO CLR_WHT("Info ")
|
|
|
|
#define LOG_LVL_WARN CLR_YEL("Warn ")
|
|
|
|
#define LOG_LVL_ERROR CLR_RED("Error")
|
|
|
|
#define LOG_LVL_STATS CLR_MAG("Stats")
|
2015-03-31 14:27:38 +02:00
|
|
|
|
2016-06-08 22:29:30 +02:00
|
|
|
/** Debug facilities.
|
|
|
|
*
|
|
|
|
* To be or-ed with the debug level
|
|
|
|
*/
|
2017-02-12 14:12:35 -03:00
|
|
|
enum log_facilities {
|
2018-06-15 15:42:46 +02:00
|
|
|
LOG_POOL = (1L << 8),
|
|
|
|
LOG_QUEUE = (1L << 9),
|
2018-07-04 16:34:08 +02:00
|
|
|
LOG_CONFIG = (1L << 10),
|
2018-06-15 15:42:46 +02:00
|
|
|
LOG_HOOK = (1L << 11),
|
|
|
|
LOG_PATH = (1L << 12),
|
|
|
|
LOG_NODE = (1L << 13),
|
|
|
|
LOG_MEM = (1L << 14),
|
|
|
|
LOG_WEB = (1L << 15),
|
|
|
|
LOG_API = (1L << 16),
|
|
|
|
LOG_LOG = (1L << 17),
|
|
|
|
LOG_VFIO = (1L << 18),
|
|
|
|
LOG_PCI = (1L << 19),
|
|
|
|
LOG_XIL = (1L << 20),
|
|
|
|
LOG_TC = (1L << 21),
|
|
|
|
LOG_IF = (1L << 22),
|
|
|
|
LOG_ADVIO = (1L << 23),
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-08 22:29:30 +02:00
|
|
|
/* Node-types */
|
2018-07-04 16:34:08 +02:00
|
|
|
LOG_SOCKET = (1L << 24),
|
2018-06-15 15:42:46 +02:00
|
|
|
LOG_FILE = (1L << 25),
|
|
|
|
LOG_FPGA = (1L << 26),
|
|
|
|
LOG_NGSI = (1L << 27),
|
2018-07-04 16:34:08 +02:00
|
|
|
LOG_WEBSOCKET = (1L << 28),
|
2018-06-15 15:42:46 +02:00
|
|
|
LOG_OPAL = (1L << 30),
|
2018-07-04 16:34:08 +02:00
|
|
|
LOG_COMEDI = (1L << 31),
|
|
|
|
LOG_IB = (1L << 32),
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-12 14:12:35 -03:00
|
|
|
/* Classes */
|
2018-07-04 16:34:08 +02:00
|
|
|
LOG_NODES = LOG_NODE | LOG_SOCKET | LOG_FILE | LOG_FPGA | LOG_NGSI | LOG_WEBSOCKET | LOG_OPAL | LOG_IB,
|
2017-03-12 17:06:00 -03:00
|
|
|
LOG_KERNEL = LOG_VFIO | LOG_PCI | LOG_TC | LOG_IF,
|
2017-05-05 19:24:16 +00:00
|
|
|
LOG_ALL = ~0xFF
|
2016-06-08 22:29:30 +02:00
|
|
|
};
|
|
|
|
|
2017-02-16 09:29:04 -03:00
|
|
|
struct log {
|
2017-03-11 23:50:30 -03:00
|
|
|
enum state state;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-16 09:29:04 -03:00
|
|
|
struct timespec epoch; /**< A global clock used to prefix the log messages. */
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-07-12 00:56:08 +02:00
|
|
|
struct winsize window; /**< Size of the terminal window. */
|
2017-09-16 15:00:11 +02:00
|
|
|
int width; /**< The real usable log output width which fits into one line. */
|
2017-02-16 09:29:04 -03:00
|
|
|
|
|
|
|
/** Debug level used by the debug() macro.
|
|
|
|
* It defaults to V (defined by the Makefile) and can be
|
|
|
|
* overwritten by the 'debug' setting in the configuration file. */
|
|
|
|
int level;
|
2017-07-12 00:56:08 +02:00
|
|
|
long facilities; /**< Debug facilities used by the debug() macro. */
|
|
|
|
const char *path; /**< Path of the log file. */
|
2017-08-23 16:51:15 +02:00
|
|
|
char *prefix; /**< Prefix each line with this string. */
|
2017-09-16 11:32:04 +02:00
|
|
|
int syslog; /**< Whether or not to log to syslogd. */
|
|
|
|
|
2017-07-12 00:56:08 +02:00
|
|
|
FILE *file; /**< Send all log output to this file / stdout / stderr. */
|
2017-02-16 09:29:04 -03:00
|
|
|
};
|
|
|
|
|
2017-04-26 11:52:22 +02:00
|
|
|
/** The global log instance. */
|
|
|
|
struct log *global_log;
|
|
|
|
struct log default_log;
|
|
|
|
|
2017-02-16 09:29:04 -03:00
|
|
|
/** Initialize log object */
|
2017-03-05 10:06:32 -04:00
|
|
|
int log_init(struct log *l, int level, long faciltities);
|
2017-02-16 09:29:04 -03:00
|
|
|
|
2018-05-12 18:14:24 +02:00
|
|
|
int log_open(struct log *l);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2018-05-12 18:14:24 +02:00
|
|
|
int log_close(struct log *l);
|
2017-03-03 20:21:33 -04:00
|
|
|
|
2017-02-16 09:29:04 -03:00
|
|
|
/** Destroy log object */
|
|
|
|
int log_destroy(struct log *l);
|
|
|
|
|
|
|
|
/** Set logging facilities based on expression.
|
2015-03-31 14:27:38 +02:00
|
|
|
*
|
2017-02-16 09:29:04 -03:00
|
|
|
* Currently we support two types of expressions:
|
|
|
|
* 1. A comma seperated list of logging facilities
|
|
|
|
* 2. A comma seperated list of logging facilities which is prefixes with an exclamation mark '!'
|
|
|
|
*
|
|
|
|
* The first case enables only faciltities which are in the list.
|
|
|
|
* The second case enables all faciltities with exception of those which are in the list.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-02-16 09:29:04 -03:00
|
|
|
* @param expression The expression
|
|
|
|
* @return The new facilties mask (see enum log_faciltities)
|
2015-03-31 14:27:38 +02:00
|
|
|
*/
|
2017-02-16 09:29:04 -03:00
|
|
|
int log_set_facility_expression(struct log *l, const char *expression);
|
2015-03-31 14:27:38 +02:00
|
|
|
|
2015-03-21 18:03:29 +01:00
|
|
|
/** Logs variadic messages to stdout.
|
|
|
|
*
|
|
|
|
* @param lvl The log level
|
|
|
|
* @param fmt The format string (printf alike)
|
|
|
|
*/
|
2017-02-16 09:29:04 -03:00
|
|
|
void log_print(struct log *l, const char *lvl, const char *fmt, ...)
|
|
|
|
__attribute__ ((format(printf, 3, 4)));
|
2015-03-21 18:03:29 +01:00
|
|
|
|
2015-03-31 14:27:38 +02:00
|
|
|
/** Logs variadic messages to stdout.
|
|
|
|
*
|
|
|
|
* @param lvl The log level
|
|
|
|
* @param fmt The format string (printf alike)
|
|
|
|
* @param va The variadic argument list (see stdarg.h)
|
2017-05-05 19:24:16 +00:00
|
|
|
*/
|
2017-02-16 09:29:04 -03:00
|
|
|
void log_vprint(struct log *l, const char *lvl, const char *fmt, va_list va);
|
2015-03-31 14:27:38 +02:00
|
|
|
|
2015-03-21 18:03:29 +01:00
|
|
|
/** Printf alike debug message with level. */
|
2017-02-16 09:28:16 -03:00
|
|
|
void debug(long lvl, const char *fmt, ...)
|
2015-03-31 14:27:38 +02:00
|
|
|
__attribute__ ((format(printf, 2, 3)));
|
2015-03-21 18:03:29 +01:00
|
|
|
|
|
|
|
/** Printf alike info message. */
|
2015-03-31 14:27:38 +02:00
|
|
|
void info(const char *fmt, ...)
|
|
|
|
__attribute__ ((format(printf, 1, 2)));
|
2015-03-21 18:03:29 +01:00
|
|
|
|
|
|
|
/** Printf alike warning message. */
|
2015-03-31 14:27:38 +02:00
|
|
|
void warn(const char *fmt, ...)
|
|
|
|
__attribute__ ((format(printf, 1, 2)));
|
2015-10-13 12:06:50 +02:00
|
|
|
|
|
|
|
/** Printf alike statistics message. */
|
|
|
|
void stats(const char *fmt, ...)
|
|
|
|
__attribute__ ((format(printf, 1, 2)));
|
|
|
|
|
2015-03-21 18:03:29 +01:00
|
|
|
/** Print error and exit. */
|
2015-03-31 14:27:38 +02:00
|
|
|
void error(const char *fmt, ...)
|
|
|
|
__attribute__ ((format(printf, 1, 2)));
|
2015-03-21 18:03:29 +01:00
|
|
|
|
|
|
|
/** Print error and strerror(errno). */
|
2015-03-31 14:27:38 +02:00
|
|
|
void serror(const char *fmt, ...)
|
|
|
|
__attribute__ ((format(printf, 1, 2)));
|
2015-03-21 18:03:29 +01:00
|
|
|
|
2017-04-26 11:52:22 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|