|
|
@@ -52,35 +52,45 @@
|
|
|
#include <sys/un.h>
|
|
|
#endif
|
|
|
#include <syslog.h>
|
|
|
+#include <stdlib.h>
|
|
|
+
|
|
|
#include "print.h"
|
|
|
#include "totemip.h"
|
|
|
#include "../include/saAis.h"
|
|
|
|
|
|
-static unsigned int logmode = LOG_MODE_STDERR | LOG_MODE_SYSLOG;
|
|
|
+static unsigned int logmode = LOG_MODE_BUFFER | LOG_MODE_STDERR | LOG_MODE_SYSLOG;
|
|
|
static char *logfile = 0;
|
|
|
+static int log_setup_called;
|
|
|
|
|
|
+#ifndef MAX_LOGGERS
|
|
|
#define MAX_LOGGERS 32
|
|
|
+#endif
|
|
|
struct logger loggers[MAX_LOGGERS];
|
|
|
|
|
|
-#define LOG_MODE_DEBUG 1
|
|
|
-#define LOG_MODE_TIMESTAMP 2
|
|
|
-#define LOG_MODE_FILE 4
|
|
|
-#define LOG_MODE_SYSLOG 8
|
|
|
-#define LOG_MODE_STDERR 16
|
|
|
-
|
|
|
static FILE *log_file_fp = 0;
|
|
|
|
|
|
-struct sockaddr_un syslog_sockaddr = {
|
|
|
- sun_family: AF_UNIX,
|
|
|
- sun_path: "/dev/log"
|
|
|
+struct log_entry
|
|
|
+{
|
|
|
+ char *file;
|
|
|
+ int line;
|
|
|
+ int level;
|
|
|
+ char str[128];
|
|
|
+ struct log_entry *next;
|
|
|
};
|
|
|
|
|
|
+static struct log_entry *head;
|
|
|
+static struct log_entry *tail;
|
|
|
+
|
|
|
static int logger_init (const char *ident, int tags, int level, int mode)
|
|
|
{
|
|
|
int i;
|
|
|
|
|
|
- for (i = 0; i < MAX_LOGGERS; i++) {
|
|
|
+ for (i = 0; i < MAX_LOGGERS; i++) {
|
|
|
if (strcmp (loggers[i].ident, ident) == 0) {
|
|
|
+ loggers[i].tags |= tags;
|
|
|
+ if (level > loggers[i].level) {
|
|
|
+ loggers[i].level = level;
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -91,8 +101,6 @@ static int logger_init (const char *ident, int tags, int level, int mode)
|
|
|
strncpy (loggers[i].ident, ident, sizeof(loggers[i].ident));
|
|
|
loggers[i].tags = tags;
|
|
|
loggers[i].level = level;
|
|
|
- if (logmode & LOG_MODE_DEBUG)
|
|
|
- loggers[i].level = LOG_LEVEL_DEBUG;
|
|
|
loggers[i].mode = mode;
|
|
|
break;
|
|
|
}
|
|
|
@@ -104,58 +112,45 @@ static int logger_init (const char *ident, int tags, int level, int mode)
|
|
|
return i;
|
|
|
}
|
|
|
|
|
|
-int _log_init (const char *ident)
|
|
|
-{
|
|
|
- assert (ident != NULL);
|
|
|
-
|
|
|
- return logger_init (ident, TAG_LOG, LOG_LEVEL_INFO, 0);
|
|
|
-}
|
|
|
-
|
|
|
-int log_setup (char **error_string, struct main_config *config)
|
|
|
+static void buffered_log_printf (char *file, int line, int level,
|
|
|
+ char *format, va_list ap)
|
|
|
{
|
|
|
- int i;
|
|
|
- static char error_string_response[512];
|
|
|
-
|
|
|
- logmode = config->logmode;
|
|
|
- logfile = config->logfile;
|
|
|
+ struct log_entry *entry = malloc(sizeof(struct log_entry));
|
|
|
|
|
|
- if (config->logmode & LOG_MODE_FILE) {
|
|
|
- log_file_fp = fopen (config->logfile, "a+");
|
|
|
- if (log_file_fp == 0) {
|
|
|
- sprintf (error_string_response,
|
|
|
- "Can't open logfile '%s' for reason (%s).\n",
|
|
|
- config->logfile, strerror (errno));
|
|
|
- *error_string = error_string_response;
|
|
|
- return (-1);
|
|
|
- }
|
|
|
+ entry->file = file;
|
|
|
+ entry->line = line;
|
|
|
+ entry->level = level;
|
|
|
+ entry->next = NULL;
|
|
|
+ if (head == NULL) {
|
|
|
+ head = tail = entry;
|
|
|
+ } else {
|
|
|
+ tail->next = entry;
|
|
|
+ tail = entry;
|
|
|
}
|
|
|
-
|
|
|
- for (i = 0; i < config->loggers; i++) {
|
|
|
- if (config->logger[i].level == 0)
|
|
|
- config->logger[i].level = LOG_LEVEL_INFO;
|
|
|
- config->logger[i].tags |= TAG_LOG;
|
|
|
- logger_init (config->logger[i].ident,
|
|
|
- config->logger[i].tags,
|
|
|
- config->logger[i].level,
|
|
|
- config->logger[i].mode);
|
|
|
- }
|
|
|
-
|
|
|
- return (0);
|
|
|
+ vsnprintf(entry->str, sizeof(entry->str), format, ap);
|
|
|
}
|
|
|
|
|
|
-static void _log_printf (char *file, int line, int priority,
|
|
|
- char *format, va_list ap)
|
|
|
+static void _log_printf (char *file, int line,
|
|
|
+ int level, int id,
|
|
|
+ char *format, va_list ap)
|
|
|
{
|
|
|
char newstring[4096];
|
|
|
char log_string[4096];
|
|
|
char char_time[512];
|
|
|
struct timeval tv;
|
|
|
- int level = LOG_LEVEL(priority);
|
|
|
- int id = LOG_ID(priority);
|
|
|
int i = 0;
|
|
|
+ int len;
|
|
|
|
|
|
assert (id < MAX_LOGGERS);
|
|
|
|
|
|
+ /*
|
|
|
+ ** Buffer before log_setup() has been called.
|
|
|
+ */
|
|
|
+ if (logmode & LOG_MODE_BUFFER) {
|
|
|
+ buffered_log_printf(file, line, level, format, ap);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (((logmode & LOG_MODE_FILE) || (logmode & LOG_MODE_STDERR)) &&
|
|
|
(logmode & LOG_MODE_TIMESTAMP)) {
|
|
|
gettimeofday (&tv, NULL);
|
|
|
@@ -169,7 +164,15 @@ static void _log_printf (char *file, int line, int priority,
|
|
|
} else {
|
|
|
sprintf (&newstring[i], "[%-5s] %s", loggers[id].ident, format);
|
|
|
}
|
|
|
- vsprintf (log_string, newstring, ap);
|
|
|
+ len = vsprintf (log_string, newstring, ap);
|
|
|
+
|
|
|
+ /*
|
|
|
+ ** add line feed if not done yet
|
|
|
+ */
|
|
|
+ if (log_string[len - 1] != '\n') {
|
|
|
+ log_string[len] = '\n';
|
|
|
+ log_string[len + 1] = '\0';
|
|
|
+ }
|
|
|
|
|
|
/*
|
|
|
* Output the log data
|
|
|
@@ -188,10 +191,81 @@ static void _log_printf (char *file, int line, int priority,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+int _log_init (const char *ident)
|
|
|
+{
|
|
|
+ assert (ident != NULL);
|
|
|
+
|
|
|
+ /*
|
|
|
+ ** do different things before and after log_setup() has been called
|
|
|
+ */
|
|
|
+ if (log_setup_called) {
|
|
|
+ return logger_init (ident, TAG_LOG, LOG_LEVEL_INFO, 0);
|
|
|
+ } else {
|
|
|
+ return logger_init (ident, ~0, LOG_LEVEL_DEBUG, 0);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+int log_setup (char **error_string, struct main_config *config)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ static char error_string_response[512];
|
|
|
+
|
|
|
+ logmode = config->logmode;
|
|
|
+ logfile = config->logfile;
|
|
|
+
|
|
|
+ if (config->logmode & LOG_MODE_FILE) {
|
|
|
+ log_file_fp = fopen (config->logfile, "a+");
|
|
|
+ if (log_file_fp == 0) {
|
|
|
+ sprintf (error_string_response,
|
|
|
+ "Can't open logfile '%s' for reason (%s).\n",
|
|
|
+ config->logfile, strerror (errno));
|
|
|
+ *error_string = error_string_response;
|
|
|
+ return (-1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ ** reinit all loggers that has initialised before log_setup() was called.
|
|
|
+ */
|
|
|
+ for (i = 0; i < MAX_LOGGERS; i++) {
|
|
|
+ loggers[i].tags = TAG_LOG;
|
|
|
+ if (config->logmode & LOG_MODE_DEBUG) {
|
|
|
+ loggers[i].level = LOG_LEVEL_DEBUG;
|
|
|
+ } else {
|
|
|
+ loggers[i].level = LOG_LEVEL_INFO;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ ** init all loggers that has configured level and tags
|
|
|
+ */
|
|
|
+ for (i = 0; i < config->loggers; i++) {
|
|
|
+ if (config->logger[i].level == 0)
|
|
|
+ config->logger[i].level = LOG_LEVEL_INFO;
|
|
|
+ config->logger[i].tags |= TAG_LOG;
|
|
|
+ logger_init (config->logger[i].ident,
|
|
|
+ config->logger[i].tags,
|
|
|
+ config->logger[i].level,
|
|
|
+ config->logger[i].mode);
|
|
|
+ }
|
|
|
+
|
|
|
+ log_setup_called = 1;
|
|
|
+
|
|
|
+ /*
|
|
|
+ ** Flush what we have buffered
|
|
|
+ */
|
|
|
+ log_flush();
|
|
|
+
|
|
|
+ internal_log_printf(__FILE__, __LINE__, LOG_LEVEL_DEBUG, "log setup\n");
|
|
|
+
|
|
|
+ return (0);
|
|
|
+}
|
|
|
+
|
|
|
void internal_log_printf (char *file, int line, int priority,
|
|
|
char *format, ...)
|
|
|
{
|
|
|
int id = LOG_ID(priority);
|
|
|
+ int level = LOG_LEVEL(priority);
|
|
|
va_list ap;
|
|
|
|
|
|
assert (id < MAX_LOGGERS);
|
|
|
@@ -201,15 +275,50 @@ void internal_log_printf (char *file, int line, int priority,
|
|
|
}
|
|
|
|
|
|
va_start (ap, format);
|
|
|
- _log_printf (file, line, priority, format, ap);
|
|
|
+ _log_printf (file, line, level, id, format, ap);
|
|
|
va_end(ap);
|
|
|
}
|
|
|
|
|
|
-void internal_log_printf2 (char *file, int line, int priority,
|
|
|
- char *format, ...)
|
|
|
+void internal_log_printf2 (char *file, int line, int level, int id,
|
|
|
+ char *format, ...)
|
|
|
{
|
|
|
va_list ap;
|
|
|
+
|
|
|
+ assert (id < MAX_LOGGERS);
|
|
|
+
|
|
|
va_start (ap, format);
|
|
|
- _log_printf (file, line, priority, format, ap);
|
|
|
+ _log_printf (file, line, level, id, format, ap);
|
|
|
va_end(ap);
|
|
|
}
|
|
|
+
|
|
|
+void trace (char *file, int line, int tag, int id, char *format, ...)
|
|
|
+{
|
|
|
+ assert (id < MAX_LOGGERS);
|
|
|
+
|
|
|
+ if (tag & loggers[id].tags) {
|
|
|
+ va_list ap;
|
|
|
+
|
|
|
+ va_start (ap, format);
|
|
|
+ _log_printf (file, line, LOG_LEVEL_DEBUG, id, format, ap);
|
|
|
+ va_end(ap);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void log_flush(void)
|
|
|
+{
|
|
|
+ struct log_entry *entry = head;
|
|
|
+ struct log_entry *tmp;
|
|
|
+
|
|
|
+ /* do not buffer these printouts */
|
|
|
+ logmode &= ~LOG_MODE_BUFFER;
|
|
|
+
|
|
|
+ while (entry) {
|
|
|
+ internal_log_printf(entry->file, entry->line,
|
|
|
+ entry->level, entry->str);
|
|
|
+ tmp = entry;
|
|
|
+ entry = entry->next;
|
|
|
+ free(tmp);
|
|
|
+ }
|
|
|
+
|
|
|
+ head = tail = NULL;
|
|
|
+}
|