- lovlevels specified for each context
- threadsave log_bytes - default logging to stdout, easier for debugging - fixed logging to file
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
|
||||||
@@ -62,15 +63,10 @@ struct private_logger_t {
|
|||||||
FILE *output;
|
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.
|
* 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
|
else
|
||||||
{
|
{
|
||||||
@@ -161,35 +157,28 @@ static status_t logg(private_logger_t *this, logger_level_t loglevel, char *form
|
|||||||
/* File output */
|
/* File output */
|
||||||
this->prepend_prefix(this, loglevel, format, buffer);
|
this->prepend_prefix(this, loglevel, format, buffer);
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
this->log_to_file(this, buffer, args);
|
vfprintf(this->output, buffer, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
fprintf(this->output, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return SUCCESS;
|
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.
|
* 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 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)
|
if ((this->level & loglevel) == loglevel)
|
||||||
{
|
{
|
||||||
char buffer[MAX_LOG];
|
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)
|
if (this->output == NULL)
|
||||||
{
|
{
|
||||||
syslog(LOG_INFO, buffer, label, len);
|
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;
|
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++)
|
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 >> 4) & 0xF];
|
||||||
*buffer_pos++ = hexdig[ *bytes_pos & 0xF];
|
*buffer_pos++ = hexdig[ *bytes_pos & 0xF];
|
||||||
if ((i % 16) == 0)
|
if ((i % 16) == 0)
|
||||||
@@ -229,7 +220,7 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this->log_to_file(this, "| %s", buffer);
|
fprintf(this->output, "| %s\n", buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((i % 8) == 0)
|
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++ = '\0';
|
||||||
buffer_pos = buffer;
|
if (buffer_pos > buffer + 1)
|
||||||
if (this->output == NULL)
|
|
||||||
{
|
|
||||||
syslog(LOG_INFO, "| %s", buffer);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
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;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,7 +303,7 @@ static status_t destroy(private_logger_t *this)
|
|||||||
/*
|
/*
|
||||||
* Described in header.
|
* 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);
|
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.disable_level = (status_t(*)(logger_t*,logger_level_t))disable_level;
|
||||||
this->public.destroy = (status_t(*)(logger_t*))destroy;
|
this->public.destroy = (status_t(*)(logger_t*))destroy;
|
||||||
|
|
||||||
this->log_to_file = log_to_file;
|
|
||||||
this->prepend_prefix = prepend_prefix;
|
this->prepend_prefix = prepend_prefix;
|
||||||
|
|
||||||
/* private variables */
|
/* private variables */
|
||||||
this->level = log_level;
|
this->level = log_level;
|
||||||
this->log_thread_id = log_thread_id;
|
this->log_pid = log_pid;
|
||||||
this->name = allocator_alloc(strlen(logger_name) + 1);
|
this->name = allocator_alloc(strlen(logger_name) + 1);
|
||||||
if (this->name == NULL)
|
if (this->name == NULL)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -164,15 +164,15 @@ struct logger_t {
|
|||||||
*
|
*
|
||||||
* @param logger_name name for the logger_t object
|
* @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_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 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
|
* @param output FILE * if log has to go on a file output, NULL for syslog
|
||||||
* @return
|
* @return
|
||||||
* - logger_t object
|
* - logger_t object
|
||||||
* - NULL if out of ressources
|
* - NULL if out of ressources
|
||||||
*
|
*
|
||||||
* @ingroup utils
|
* @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_*/
|
#endif /*LOGGER_H_*/
|
||||||
|
|||||||
@@ -133,27 +133,42 @@ static logger_t *create_logger(private_logger_manager_t *this, logger_context_t
|
|||||||
char buffer[MAX_LOGGER_NAME];
|
char buffer[MAX_LOGGER_NAME];
|
||||||
loggers_entry_t *entry;
|
loggers_entry_t *entry;
|
||||||
logger_t *logger;
|
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);
|
context_name = mapping_find(logger_context_t_mappings,context);
|
||||||
|
|
||||||
|
/* output to stdout, since we are debugging all days */
|
||||||
|
output = stdout;
|
||||||
|
|
||||||
switch(context)
|
switch(context)
|
||||||
{
|
{
|
||||||
case TESTER:
|
case TESTER:
|
||||||
log_thread_ids = FALSE;
|
log_thread_ids = FALSE;
|
||||||
output = stdout;
|
output = stdout;
|
||||||
|
logger_level = FULL;
|
||||||
break;
|
break;
|
||||||
|
case PARSER:
|
||||||
|
case GENERATOR:
|
||||||
|
case IKE_SA:
|
||||||
|
case IKE_SA_MANAGER:
|
||||||
|
case MESSAGE:
|
||||||
|
case THREAD_POOL:
|
||||||
|
case WORKER:
|
||||||
case SCHEDULER:
|
case SCHEDULER:
|
||||||
case SENDER:
|
case SENDER:
|
||||||
case RECEIVER:
|
case RECEIVER:
|
||||||
case THREAD_POOL:
|
|
||||||
case SOCKET:
|
case SOCKET:
|
||||||
case DAEMON:
|
case DAEMON:
|
||||||
|
case CONFIGURATION_MANAGER:
|
||||||
log_thread_ids = FALSE;
|
log_thread_ids = FALSE;
|
||||||
break;
|
logger_level = ERROR|CONTROL;
|
||||||
default:
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* reduce to global definiton of loglevel */
|
||||||
|
logger_level &= this->public.get_logger_level(&(this->public),context);
|
||||||
|
|
||||||
/* logger manager is thread save */
|
/* logger manager is thread save */
|
||||||
pthread_mutex_lock(&(this->mutex));
|
pthread_mutex_lock(&(this->mutex));
|
||||||
if (name != NULL)
|
if (name != NULL)
|
||||||
|
|||||||
Reference in New Issue
Block a user