2015-03-23 17:32:56 +01:00
|
|
|
/** Logging and debugging routines
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*********************************************************************************/
|
2015-03-23 17:32:56 +01:00
|
|
|
|
2015-03-31 14:27:38 +02:00
|
|
|
#include <stdio.h>
|
2017-03-07 07:13:27 -04:00
|
|
|
#include <stdbool.h>
|
2017-07-12 00:56:08 +02:00
|
|
|
#include <stdarg.h>
|
2015-06-03 10:13:35 +02:00
|
|
|
#include <string.h>
|
2015-03-21 18:03:29 +01:00
|
|
|
#include <time.h>
|
2017-07-12 00:56:08 +02:00
|
|
|
#include <unistd.h>
|
2017-09-16 11:32:04 +02:00
|
|
|
#include <syslog.h>
|
2018-07-03 18:25:15 +02:00
|
|
|
#include <signal.h>
|
2015-03-21 18:03:29 +01:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/config.h>
|
|
|
|
#include <villas/log.h>
|
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/config.h>
|
2015-03-21 18:03:29 +01:00
|
|
|
|
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
|
|
|
|
|
2017-04-26 11:52:22 +02:00
|
|
|
struct log *global_log;
|
2017-08-20 10:51:41 +02:00
|
|
|
|
|
|
|
/* We register a default log instance */
|
|
|
|
__attribute__((constructor))
|
|
|
|
void register_default_log()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
static struct log default_log;
|
2017-09-06 23:29:29 +02:00
|
|
|
|
2017-08-20 10:51:41 +02:00
|
|
|
ret = log_init(&default_log, V, LOG_ALL);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initalize log");
|
2017-09-06 23:29:29 +02:00
|
|
|
|
2018-05-12 18:14:24 +02:00
|
|
|
ret = log_open(&default_log);
|
2017-08-20 10:51:41 +02:00
|
|
|
if (ret)
|
|
|
|
error("Failed to start log");
|
|
|
|
}
|
2017-03-29 04:25:30 +02:00
|
|
|
|
2017-02-16 09:29:04 -03:00
|
|
|
/** List of debug facilities as strings */
|
|
|
|
static const char *facilities_strs[] = {
|
|
|
|
"pool", /* LOG_POOL */
|
|
|
|
"queue", /* LOG_QUEUE */
|
|
|
|
"config", /* LOG_CONFIG */
|
|
|
|
"hook", /* LOG_HOOK */
|
|
|
|
"path", /* LOG_PATH */
|
2017-03-07 07:13:27 -04:00
|
|
|
"node", /* LOG_NODE */
|
2017-02-16 09:29:04 -03:00
|
|
|
"mem", /* LOG_MEM */
|
|
|
|
"web", /* LOG_WEB */
|
|
|
|
"api", /* LOG_API */
|
2017-03-07 07:13:27 -04:00
|
|
|
"log", /* LOG_LOG */
|
|
|
|
"vfio", /* LOG_VFIO */
|
|
|
|
"pci", /* LOG_PCI */
|
|
|
|
"xil", /* LOG_XIL */
|
2017-03-12 17:06:00 -03:00
|
|
|
"tc", /* LOG_TC */
|
|
|
|
"if", /* LOG_IF */
|
2017-06-17 19:34:00 +02:00
|
|
|
"advio", /* LOG_ADVIO */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
|
|
|
/* Node-types */
|
2017-02-16 09:29:04 -03:00
|
|
|
"socket", /* LOG_SOCKET */
|
|
|
|
"file", /* LOG_FILE */
|
|
|
|
"fpga", /* LOG_FPGA */
|
|
|
|
"ngsi", /* LOG_NGSI */
|
|
|
|
"websocket", /* LOG_WEBSOCKET */
|
2017-03-07 07:13:27 -04:00
|
|
|
"opal", /* LOG_OPAL */
|
2018-07-04 16:34:08 +02:00
|
|
|
"ib", /* LOG_IB */
|
2017-02-16 09:29:04 -03:00
|
|
|
};
|
2015-03-21 18:03:29 +01:00
|
|
|
|
2017-07-12 00:56:08 +02:00
|
|
|
static void log_resize(int signal, siginfo_t *sinfo, void *ctx)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &global_log->window);
|
|
|
|
if (ret)
|
|
|
|
return;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-09-16 15:33:30 +02:00
|
|
|
global_log->width = global_log->window.ws_col - 25;
|
|
|
|
if (global_log->prefix)
|
|
|
|
global_log->width -= strlenp(global_log->prefix);
|
2017-09-16 15:00:11 +02:00
|
|
|
|
2017-07-12 00:56:08 +02:00
|
|
|
debug(LOG_LOG | 15, "New terminal size: %dx%x", global_log->window.ws_row, global_log->window.ws_col);
|
|
|
|
}
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
int log_init(struct log *l, int level, long facilitites)
|
|
|
|
{
|
2017-07-12 00:56:08 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
/* Register this log instance globally */
|
2017-04-26 11:52:22 +02:00
|
|
|
global_log = l;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
|
|
|
l->level = level;
|
2017-09-16 11:32:04 +02:00
|
|
|
l->syslog = 0;
|
2017-03-11 23:50:30 -03:00
|
|
|
l->facilities = facilitites;
|
2017-03-29 04:25:30 +02:00
|
|
|
l->file = stderr;
|
|
|
|
l->path = NULL;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-09-06 23:29:29 +02:00
|
|
|
l->epoch = time_now();
|
|
|
|
l->prefix = getenv("VILLAS_LOG_PREFIX");
|
|
|
|
|
2017-07-12 00:56:08 +02:00
|
|
|
/* Register signal handler which is called whenever the
|
|
|
|
* terminal size changes. */
|
|
|
|
if (l->file == stderr) {
|
|
|
|
struct sigaction sa_resize = {
|
|
|
|
.sa_flags = SA_SIGINFO,
|
|
|
|
.sa_sigaction = log_resize
|
|
|
|
};
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-07-12 00:56:08 +02:00
|
|
|
sigemptyset(&sa_resize.sa_mask);
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-07-12 00:56:08 +02:00
|
|
|
ret = sigaction(SIGWINCH, &sa_resize, NULL);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-07-24 19:33:35 +02:00
|
|
|
|
2017-07-12 00:56:08 +02:00
|
|
|
/* Try to get initial window size */
|
2018-05-23 00:38:37 +02:00
|
|
|
ioctl(STDERR_FILENO, TIOCGWINSZ, &l->window);
|
|
|
|
|
|
|
|
/* Fallback if for some reason we can not determine a prober window size */
|
|
|
|
if (l->window.ws_col == 0)
|
|
|
|
l->window.ws_col = 150;
|
|
|
|
if (l->window.ws_row == 0)
|
|
|
|
l->window.ws_row = 50;
|
2017-07-12 00:56:08 +02:00
|
|
|
}
|
2017-09-16 15:00:11 +02:00
|
|
|
else {
|
|
|
|
l->window.ws_col = LOG_WIDTH;
|
|
|
|
l->window.ws_row = LOG_HEIGHT;
|
|
|
|
}
|
|
|
|
|
2017-09-16 15:33:30 +02:00
|
|
|
l->width = l->window.ws_col - 25;
|
|
|
|
if (l->prefix)
|
|
|
|
l->width -= strlenp(l->prefix);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
l->state = STATE_INITIALIZED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-12 18:14:24 +02:00
|
|
|
int log_open(struct log *l)
|
2017-03-11 23:50:30 -03:00
|
|
|
{
|
2017-09-16 11:32:04 +02:00
|
|
|
if (l->path) {
|
|
|
|
l->file = fopen(l->path, "a+");;
|
|
|
|
if (!l->file) {
|
|
|
|
l->file = stderr;
|
|
|
|
error("Failed to open log file '%s'", l->path);
|
|
|
|
}
|
2017-03-14 01:49:22 -03:00
|
|
|
}
|
2017-09-16 11:32:04 +02:00
|
|
|
else
|
|
|
|
l->file = stderr;
|
2017-03-14 01:49:22 -03:00
|
|
|
|
2018-05-12 18:14:24 +02:00
|
|
|
l->state = STATE_OPENED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-16 11:32:04 +02:00
|
|
|
if (l->syslog) {
|
|
|
|
openlog(NULL, LOG_PID, LOG_DAEMON);
|
|
|
|
}
|
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
debug(LOG_LOG | 5, "Log sub-system started: level=%d, faciltities=%#lx, path=%s", l->level, l->facilities, l->path);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-12 18:14:24 +02:00
|
|
|
int log_close(struct log *l)
|
2017-03-11 23:50:30 -03:00
|
|
|
{
|
2018-05-12 18:14:24 +02:00
|
|
|
if (l->state != STATE_OPENED)
|
2017-09-16 11:32:04 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (l->file != stderr && l->file != stdout) {
|
|
|
|
fclose(l->file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->syslog) {
|
|
|
|
closelog();
|
2017-04-02 04:56:08 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-12 18:14:24 +02:00
|
|
|
l->state = STATE_CLOSED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_destroy(struct log *l)
|
|
|
|
{
|
2017-04-02 04:56:08 +02:00
|
|
|
default_log.epoch = l->epoch;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-20 10:51:41 +02:00
|
|
|
global_log = &default_log;
|
2017-04-02 04:56:08 +02:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
l->state = STATE_DESTROYED;
|
2017-04-02 04:56:08 +02:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-03-07 07:13:27 -04:00
|
|
|
bool negate;
|
|
|
|
char *copy, *token;
|
|
|
|
long mask = 0, facilities = 0;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-16 09:29:04 -03:00
|
|
|
copy = strdup(expression);
|
2017-03-07 07:13:27 -04:00
|
|
|
token = strtok(copy, ",");
|
|
|
|
|
|
|
|
while (token != NULL) {
|
|
|
|
if (token[0] == '!') {
|
|
|
|
token++;
|
|
|
|
negate = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
negate = false;
|
|
|
|
|
|
|
|
/* Check for some classes */
|
|
|
|
if (!strcmp(token, "all"))
|
|
|
|
mask = LOG_ALL;
|
|
|
|
else if (!strcmp(token, "nodes"))
|
|
|
|
mask = LOG_NODES;
|
|
|
|
else if (!strcmp(token, "kernel"))
|
|
|
|
mask = LOG_KERNEL;
|
|
|
|
else {
|
|
|
|
for (int ind = 0; ind < ARRAY_LEN(facilities_strs); ind++) {
|
|
|
|
if (!strcmp(token, facilities_strs[ind])) {
|
|
|
|
mask = (1 << (ind+8));
|
|
|
|
goto found;
|
2017-02-16 09:29:04 -03:00
|
|
|
}
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-07 07:13:27 -04:00
|
|
|
error("Invalid log class '%s'", token);
|
2017-02-16 09:29:04 -03:00
|
|
|
}
|
2017-03-07 07:13:27 -04:00
|
|
|
|
|
|
|
found: if (negate)
|
|
|
|
facilities &= ~mask;
|
|
|
|
else
|
|
|
|
facilities |= mask;
|
|
|
|
|
|
|
|
token = strtok(NULL, ",");
|
2017-02-16 09:29:04 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-07 07:13:27 -04:00
|
|
|
l->facilities = facilities;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-16 09:29:04 -03:00
|
|
|
free(copy);
|
|
|
|
|
|
|
|
return l->facilities;
|
2015-03-31 14:27:38 +02:00
|
|
|
}
|
|
|
|
|
2017-02-16 09:29:04 -03:00
|
|
|
void log_print(struct log *l, const char *lvl, const char *fmt, ...)
|
2015-03-31 14:27:38 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
2015-09-22 13:20:20 +02:00
|
|
|
|
2015-03-31 14:27:38 +02:00
|
|
|
va_start(ap, fmt);
|
2017-02-16 09:29:04 -03:00
|
|
|
log_vprint(l, lvl, fmt, ap);
|
2015-03-31 14:27:38 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2017-02-16 09:29:04 -03:00
|
|
|
void log_vprint(struct log *l, const char *lvl, const char *fmt, va_list ap)
|
2015-03-21 18:03:29 +01:00
|
|
|
{
|
2015-10-12 16:25:08 +02:00
|
|
|
struct timespec ts = time_now();
|
2015-09-22 12:58:37 +02:00
|
|
|
char *buf = alloc(512);
|
2017-09-06 23:29:29 +02:00
|
|
|
|
2017-08-20 10:51:41 +02:00
|
|
|
/* Optional prefix */
|
|
|
|
if (l->prefix)
|
|
|
|
strcatf(&buf, "%s", l->prefix);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-12 00:56:08 +02:00
|
|
|
/* Timestamp & Severity */
|
2017-08-22 12:13:38 +02:00
|
|
|
strcatf(&buf, "%10.3f %-5s ", time_delta(&l->epoch, &ts), lvl);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-21 18:03:29 +01:00
|
|
|
/* Format String */
|
2015-09-22 12:58:37 +02:00
|
|
|
vstrcatf(&buf, fmt, ap);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-21 18:03:29 +01:00
|
|
|
/* Output */
|
|
|
|
#ifdef ENABLE_OPAL_ASYNC
|
2016-06-08 23:21:42 +02:00
|
|
|
OpalPrint("VILLASnode: %s\n", buf);
|
2015-03-21 18:03:29 +01:00
|
|
|
#endif
|
2018-07-06 14:13:01 +02:00
|
|
|
if (l->file)
|
|
|
|
fprintf(l->file, "%s\n", buf);
|
|
|
|
|
|
|
|
if (l->syslog)
|
|
|
|
vsyslog(LOG_INFO, fmt, ap);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
free(buf);
|
2017-07-24 19:33:35 +02:00
|
|
|
}
|