1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/log.c

198 lines
3.6 KiB
C
Raw Normal View History

2015-03-23 17:32:56 +01:00
/** Logging and debugging routines
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2016-02-09 05:33:19 +01:00
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
2016-06-08 23:21:42 +02:00
* This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential.
2015-08-07 01:11:43 +02:00
* Unauthorized copying of this file, via any medium is strictly prohibited.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
2015-03-23 17:32:56 +01:00
2015-03-31 14:27:38 +02:00
#include <stdio.h>
2015-06-03 10:13:35 +02:00
#include <string.h>
#include <time.h>
2015-06-03 10:13:35 +02:00
#include <errno.h>
#include "log.h"
#include "utils.h"
#include "config.h"
2015-06-02 22:06:15 +02:00
#include "timing.h"
2015-05-07 13:57:43 +02:00
#ifdef ENABLE_OPAL_ASYNC
/* Define RTLAB before including OpalPrint.h for messages to be sent
* to the OpalDisplay. Otherwise stdout will be used. */
2015-08-21 10:19:32 +02:00
#define RTLAB
#include "OpalPrint.h"
2015-05-07 13:57:43 +02:00
#endif
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
2015-06-02 22:29:15 +02:00
* overwritten by the 'debug' setting in the configuration file. */
2016-06-08 22:29:30 +02:00
static unsigned level = V;
/** Debug facilities used by the debug() macro. */
static unsigned facilities = ~0;
2015-03-31 14:27:38 +02:00
/** A global clock used to prefix the log messages. */
static struct timespec epoch;
#ifdef __GNUC__
2015-03-31 14:27:38 +02:00
/** The current log indention level (per thread!). */
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
2016-06-08 22:29:30 +02:00
void log_setlevel(int lvl, int fac)
2015-03-31 14:27:38 +02:00
{
level = lvl;
debug(10, "Switched to debug level %u", level);
}
2015-10-11 13:25:06 +02:00
void log_init()
{
2015-10-12 16:25:08 +02:00
epoch = time_now();
2015-03-31 14:27:38 +02:00
debug(10, "Debug clock resetted");
}
2015-03-31 14:27:38 +02:00
void log_print(const char *lvl, const char *fmt, ...)
{
va_list ap;
2015-09-22 13:20:20 +02:00
2015-03-31 14:27:38 +02:00
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-10-12 16:25:08 +02:00
struct timespec ts = time_now();
char *buf = alloc(512);
/* Timestamp */
strcatf(&buf, "%10.3f ", time_delta(&epoch, &ts));
/* Severity */
strcatf(&buf, "%5s ", lvl);
2015-08-07 01:11:43 +02:00
/* Indention */
#ifdef __GNUC__
for (int i = 0; i < indent; i++)
strcatf(&buf, ACS_VERTICAL " ");
strcatf(&buf, ACS_VERTRIGHT " ");
#endif
/* Format String */
vstrcatf(&buf, fmt, ap);
2015-08-07 01:11:43 +02:00
/* Output */
#ifdef ENABLE_OPAL_ASYNC
2016-06-08 23:21:42 +02:00
OpalPrint("VILLASnode: %s\n", buf);
#endif
fprintf(stderr, "\r%s\n", buf);
free(buf);
}
2015-03-31 14:27:38 +02:00
void line()
{
char buf[LOG_WIDTH];
memset(buf, 0x71, sizeof(buf));
2015-08-07 01:11:43 +02:00
log_print("", "\b" ACS("%.*s"), LOG_WIDTH, buf);
}
2016-06-08 22:29:30 +02:00
void debug(int class, const char *fmt, ...)
2015-03-31 14:27:38 +02:00
{
va_list ap;
2016-06-08 22:29:30 +02:00
int lvl = class & 0xFF;
int fac = class & ~0xFF;
2015-03-31 14:27:38 +02:00
2016-06-08 22:29:30 +02:00
if (((fac == 0) || (fac & facilities)) && (lvl <= level)) {
2015-03-31 14:27:38 +02:00
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);
}
2015-08-07 01:11:43 +02:00
2015-10-13 12:06:50 +02:00
void stats(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_vprint(STATS, fmt, ap);
va_end(ap);
}
2015-03-31 14:27:38 +02:00
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 = NULL;
2015-08-07 01:11:43 +02:00
2015-03-31 14:27:38 +02:00
va_start(ap, fmt);
vstrcatf(&buf, fmt, ap);
2015-03-31 14:27:38 +02:00
va_end(ap);
2015-08-07 01:11:43 +02:00
2015-03-31 18:13:43 +02:00
log_print(ERROR, "%s: %m (%u)", buf, errno);
free(buf);
2015-03-31 14:27:38 +02:00
die();
}
void cerror(config_setting_t *cfg, const char *fmt, ...)
{
va_list ap;
char *buf = NULL;
2015-08-07 01:11:43 +02:00
2015-03-31 14:27:38 +02:00
va_start(ap, fmt);
vstrcatf(&buf, fmt, ap);
2015-03-31 14:27:38 +02:00
va_end(ap);
2015-08-07 01:11:43 +02:00
2015-03-31 14:27:38 +02:00
log_print(ERROR, "%s in %s:%u", buf,
config_setting_source_file(cfg)
? config_setting_source_file(cfg)
2015-08-07 01:11:43 +02:00
: "(stdio)",
config_setting_source_line(cfg));
free(buf);
2015-03-31 14:27:38 +02:00
die();
2015-03-31 18:13:43 +02:00
}