- lovlevels specified for each context

- threadsave log_bytes
- default logging to stdout, easier for debugging
- fixed logging to file
This commit is contained in:
Martin Willi
2005-11-26 15:43:08 +00:00
parent 24035375d4
commit d62099006d
3 changed files with 56 additions and 47 deletions
+34 -40
View File
@@ -26,6 +26,7 @@
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include "logger.h"
@@ -62,15 +63,10 @@ struct private_logger_t {
FILE *output;
/**
* Should a thread_id be included in the log?
* Should a pid be included in the log?
*/
bool log_thread_id;
bool log_pid;
/* private functions */
/**
* Logs a message to the associated log file.
*/
void (*log_to_file) (private_logger_t *this, char *format, ...);
/**
* Applies a prefix to string and stores it in buffer.
*
@@ -124,9 +120,9 @@ static void prepend_prefix(private_logger_t *this, logger_level_t loglevel, char
}
if (this->log_thread_id)
if (this->log_pid)
{
snprintf(buffer, MAX_LOG, "[%c%c] [%s] @%u %s", log_type, log_details, this->name, (int)pthread_self(), string);
snprintf(buffer, MAX_LOG, "[%c%c] [%s] @%d %s", log_type, log_details, this->name, getpid(), string);
}
else
{
@@ -161,35 +157,28 @@ static status_t logg(private_logger_t *this, logger_level_t loglevel, char *form
/* File output */
this->prepend_prefix(this, loglevel, format, buffer);
va_start(args, format);
this->log_to_file(this, buffer, args);
vfprintf(this->output, buffer, args);
va_end(args);
fprintf(this->output, "\n");
}
}
return SUCCESS;
}
/**
* Implementation of private_logger_t.log_to_file.
*/
static void log_to_file(private_logger_t *this,char *format, ...)
{
char buffer[MAX_LOG];
va_list args;
time_t current_time;
current_time = time(NULL);
snprintf(buffer, MAX_LOG, "%s\n", format);
va_start(args, format);
vfprintf(this->output, buffer, args);
va_end(args);
}
/**
* Implementation of logger_t.log_bytes.
*/
static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char *label, char *bytes, size_t len)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/* since me can't do multi-line output to syslog,
* we must do multiple syslogs. To avoid
* problems in output order, lock this by a mutex.
*/
pthread_mutex_lock(&mutex);
if ((this->level & loglevel) == loglevel)
{
char buffer[MAX_LOG];
@@ -205,9 +194,11 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
if (this->output == NULL)
{
syslog(LOG_INFO, buffer, label, len);
}else
}
else
{
this->log_to_file(this, buffer, label, len);
fprintf(this->output, buffer, label, len);
fprintf(this->output, "\n");
}
bytes_pos = bytes;
@@ -216,7 +207,7 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
for (i = 1; bytes_pos < bytes_roof; i++)
{
static const char hexdig[] = "0123456789ABCDEF";
static char hexdig[] = "0123456789ABCDEF";
*buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
*buffer_pos++ = hexdig[ *bytes_pos & 0xF];
if ((i % 16) == 0)
@@ -229,7 +220,7 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
}
else
{
this->log_to_file(this, "| %s", buffer);
fprintf(this->output, "| %s\n", buffer);
}
}
else if ((i % 8) == 0)
@@ -252,17 +243,21 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
}
*buffer_pos++ = '\0';
buffer_pos = buffer;
if (this->output == NULL)
{
syslog(LOG_INFO, "| %s", buffer);
}
else
if (buffer_pos > buffer + 1)
{
this->log_to_file(this, "| %s", buffer);
buffer_pos = buffer;
if (this->output == NULL)
{
syslog(LOG_INFO, "| %s", buffer);
}
else
{
fprintf(this->output, "| %s\n", buffer);
}
}
}
pthread_mutex_unlock(&mutex);
return SUCCESS;
}
@@ -308,7 +303,7 @@ static status_t destroy(private_logger_t *this)
/*
* Described in header.
*/
logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_thread_id, FILE * output)
logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_pid, FILE * output)
{
private_logger_t *this = allocator_alloc_thing(private_logger_t);
@@ -329,12 +324,11 @@ logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_th
this->public.disable_level = (status_t(*)(logger_t*,logger_level_t))disable_level;
this->public.destroy = (status_t(*)(logger_t*))destroy;
this->log_to_file = log_to_file;
this->prepend_prefix = prepend_prefix;
/* private variables */
this->level = log_level;
this->log_thread_id = log_thread_id;
this->log_pid = log_pid;
this->name = allocator_alloc(strlen(logger_name) + 1);
if (this->name == NULL)
{
+3 -3
View File
@@ -164,15 +164,15 @@ struct logger_t {
*
* @param logger_name name for the logger_t object
* @param log_level or'ed set of log_levels to assign to the new logger_t object
* @param log_thread_id TRUE if thread id should also be logged
* @param output FILE * if log has to go on a file output, NULL for syslog
* @param log_pid TRUE if thread id should also be logged
* @param output FILE * if log has to go on a file output, NULL for syslog
* @return
* - logger_t object
* - NULL if out of ressources
*
* @ingroup utils
*/
logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_thread_id, FILE * output);
logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_pid, FILE * output);
#endif /*LOGGER_H_*/
+19 -4
View File
@@ -133,27 +133,42 @@ static logger_t *create_logger(private_logger_manager_t *this, logger_context_t
char buffer[MAX_LOGGER_NAME];
loggers_entry_t *entry;
logger_t *logger;
logger_level_t logger_level = this->public.get_logger_level(&(this->public),context);
logger_level_t logger_level;
context_name = mapping_find(logger_context_t_mappings,context);
/* output to stdout, since we are debugging all days */
output = stdout;
switch(context)
{
case TESTER:
log_thread_ids = FALSE;
output = stdout;
logger_level = FULL;
break;
case PARSER:
case GENERATOR:
case IKE_SA:
case IKE_SA_MANAGER:
case MESSAGE:
case THREAD_POOL:
case WORKER:
case SCHEDULER:
case SENDER:
case RECEIVER:
case THREAD_POOL:
case SOCKET:
case DAEMON:
case CONFIGURATION_MANAGER:
log_thread_ids = FALSE;
break;
default:
logger_level = ERROR|CONTROL;
break;
}
/* reduce to global definiton of loglevel */
logger_level &= this->public.get_logger_level(&(this->public),context);
/* logger manager is thread save */
pthread_mutex_lock(&(this->mutex));
if (name != NULL)