2015-03-23 17:32:56 +01:00
|
|
|
/** Logging and debugging routines
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2015, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
2015-03-31 14:27:38 +02:00
|
|
|
#include <stdio.h>
|
2015-03-21 18:03:29 +01:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2015-03-31 14:27:38 +02: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.
|
|
|
|
*/
|
|
|
|
static int level = V;
|
2015-03-21 18:03:29 +01:00
|
|
|
|
2015-03-31 14:27:38 +02:00
|
|
|
/** A global clock used to prefix the log messages. */
|
2015-03-21 18:03:29 +01:00
|
|
|
static struct timespec epoch;
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
2015-03-31 14:27:38 +02:00
|
|
|
/** The current log indention level (per thread!). */
|
2015-03-21 18:03:29 +01:00
|
|
|
static __thread int indent = 0;
|
|
|
|
|
|
|
|
int log_indent(int levels)
|
|
|
|
{
|
|
|
|
int old = indent;
|
|
|
|
indent += levels;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_outdent(int *old)
|
|
|
|
{
|
|
|
|
indent = *old;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-31 14:27:38 +02:00
|
|
|
void log_setlevel(int lvl)
|
|
|
|
{
|
|
|
|
level = lvl;
|
|
|
|
debug(10, "Switched to debug level %u", level);
|
|
|
|
}
|
|
|
|
|
2015-03-21 18:03:29 +01:00
|
|
|
void log_reset()
|
|
|
|
{
|
|
|
|
clock_gettime(CLOCK_REALTIME, &epoch);
|
2015-03-31 14:27:38 +02:00
|
|
|
debug(10, "Debug clock resetted");
|
2015-03-21 18:03:29 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 14:27:38 +02:00
|
|
|
void log_print(const char *lvl, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
log_vprint(lvl, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_vprint(const char *lvl, const char *fmt, va_list ap)
|
2015-03-21 18:03:29 +01:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
char buf[512] = "";
|
|
|
|
|
|
|
|
/* Timestamp */
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
2015-03-31 14:27:38 +02:00
|
|
|
strap(buf, sizeof(buf), "%10.3f ", timespec_delta(&epoch, &ts));
|
2015-03-21 18:03:29 +01:00
|
|
|
|
|
|
|
/* Severity */
|
2015-03-31 14:27:38 +02:00
|
|
|
strap(buf, sizeof(buf), BLD("%-5s "), lvl);
|
|
|
|
|
2015-03-21 18:03:29 +01:00
|
|
|
/* Indention */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
for (int i = 0; i < indent; i++)
|
2015-03-31 14:27:38 +02:00
|
|
|
strap(buf, sizeof(buf), ACS_VERTICAL " ");
|
|
|
|
strap(buf, sizeof(buf), ACS_VERTRIGHT " ");
|
2015-03-21 18:03:29 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Format String */
|
|
|
|
vstrap(buf, sizeof(buf), fmt, ap);
|
|
|
|
|
|
|
|
/* Output */
|
|
|
|
#ifdef ENABLE_OPAL_ASYNC
|
|
|
|
OpalPrint("S2SS: %s\n", buf);
|
|
|
|
#endif
|
|
|
|
fprintf(stderr, "\r%s\n", buf);
|
|
|
|
}
|
2015-03-31 14:27:38 +02:00
|
|
|
|
2015-03-31 15:02:51 +02:00
|
|
|
void line()
|
|
|
|
{
|
|
|
|
char buf[LOG_WIDTH];
|
|
|
|
memset(buf, 0x71, sizeof(buf));
|
|
|
|
|
|
|
|
log_print("", "\b" ACS("%.*s"), LOG_WIDTH, buf);
|
|
|
|
}
|
|
|
|
|
2015-03-31 14:27:38 +02:00
|
|
|
void debug(int lvl, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (lvl <= level) {
|
|
|
|
va_start(ap, fmt);
|
|
|
|
log_vprint(DEBUG, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void info(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
log_vprint(INFO, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void warn(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
log_vprint(WARN, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void error(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
log_vprint(ERROR, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
void serror(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
log_print(ERROR, "%s: %s", buf, strerror(errno));
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cerror(config_setting_t *cfg, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
log_print(ERROR, "%s in %s:%u", buf,
|
|
|
|
config_setting_source_file(cfg)
|
|
|
|
? config_setting_source_file(cfg)
|
|
|
|
: "(stdio)",
|
|
|
|
config_setting_source_line(cfg));
|
|
|
|
die();
|
|
|
|
}
|