- added thread-id to log for some contexes
authorMartin Willi <martin@strongswan.org>
Mon, 21 Nov 2005 11:47:46 +0000 (11:47 -0000)
committerMartin Willi <martin@strongswan.org>
Mon, 21 Nov 2005 11:47:46 +0000 (11:47 -0000)
- cleaned up logger output

Source/charon/utils/logger.c
Source/charon/utils/logger.h
Source/charon/utils/logger_manager.c
Source/charon/utils/logger_manager.h

index ab68362..ec892f2 100644 (file)
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <time.h>
+#include <pthread.h>
 
 #include "logger.h"
 
@@ -59,14 +60,81 @@ struct private_logger_s {
         */
        FILE *output;
        
+       /**
+        * should a thread_id be included in the log?
+        */
+       bool log_thread_id;
+       
        /* 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
+        * 
+        * @warning: buffer must be at least MAX_LOG long
+        */
+       void (*prepend_prefix) (private_logger_t *this, logger_level_t loglevel, char *string, char *buffer);
 };
 
 /**
+ * Implements private_logger_t.prepend_prefix
+ */
+static void prepend_prefix(private_logger_t *this, logger_level_t loglevel, char *string, char *buffer)
+{
+       char log_type, log_details;
+       if (loglevel & CONTROL)
+       {
+               log_type = '~';
+       }
+       else if (loglevel & ERROR)
+       {
+               log_type = '!';
+       }
+       else if (loglevel & RAW)
+       {
+               log_type = '#';
+       }
+       else if (loglevel & PRIVATE)
+       {
+               log_type = '?';
+       }
+       else
+       {
+               log_type = '-';
+       }
+       
+       if (loglevel & ALL)
+       {
+               log_details = '3';
+       }
+       else if (loglevel & MOST)
+       {
+               log_details = '2';
+       }
+       else if (loglevel & MORE)
+       {
+               log_details = '1';
+       }
+       else
+       {
+               log_details = '0';
+       }
+       
+       
+       if (this->log_thread_id)
+       {
+               snprintf(buffer, MAX_LOG, "[%c%c] [%s] @%u %s", log_type, log_details, this->name, (int)pthread_self(), string);
+       }
+       else
+       {
+               snprintf(buffer, MAX_LOG, "[%c%c] [%s] %s", log_type, log_details, this->name, string);
+       }
+       
+}
+
+/**
  * Implements logger_t-function log.
  * @see logger_s.log.
  * 
@@ -78,11 +146,12 @@ static status_t logg(private_logger_t *this, logger_level_t loglevel, char *form
        {
                char buffer[MAX_LOG];
                va_list args;
+               
 
                if (this->output == NULL)
                {
                        /* syslog */
-                       snprintf(buffer, MAX_LOG, "%s: %s", this->name, format);
+                       this->prepend_prefix(this, loglevel, format, buffer);
                        va_start(args, format);
                        vsyslog(LOG_INFO, buffer, args);
                        va_end(args);
@@ -90,7 +159,7 @@ static status_t logg(private_logger_t *this, logger_level_t loglevel, char *form
                else
                {
                        /* File output */
-                       snprintf(buffer, MAX_LOG, "File %s: %s", this->name, format);
+                       this->prepend_prefix(this, loglevel, format, buffer);
                        va_start(args, format);
                        this->log_to_file(this, buffer, args);
                        va_end(args);
@@ -125,17 +194,22 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
 {
        if ((this->level & loglevel) == loglevel)
        {
-               char buffer[64];
+               char buffer[MAX_LOG];
+               char *format;
                char *buffer_pos;
                char *bytes_pos, *bytes_roof;
                int i;
+               
+               
+               format = "%s (%d bytes)";
+               this->prepend_prefix(this, loglevel, format, buffer);
 
                if (this->output == NULL)
                {
-                       syslog(LOG_INFO, "%s: %s (%d bytes)", this->name, label, len);  
+                       syslog(LOG_INFO, buffer, label, len);   
                }else
                {
-                       this->log_to_file(this,"%s: %s (%d bytes)", this->name, label, len);
+                       this->log_to_file(this, buffer, label, len);
                }
        
                bytes_pos = bytes;
@@ -240,7 +314,7 @@ static status_t destroy(private_logger_t *this)
 /*
  * Described in Header
  */    
-logger_t *logger_create(char *logger_name, logger_level_t log_level,FILE * output)
+logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_thread_id, FILE * output)
 {
        private_logger_t *this = allocator_alloc_thing(private_logger_t);
                
@@ -262,9 +336,11 @@ logger_t *logger_create(char *logger_name, logger_level_t log_level,FILE * outpu
        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->name = allocator_alloc(strlen(logger_name) + 1);
        if (this->name == NULL)
        {
index 04fa8cd..286491f 100644 (file)
@@ -164,7 +164,7 @@ struct logger_s {
  * @param output                       FILE * if log has to go on a file output, NULL for syslog
  * @return                             logger_t object or NULL if failed
  */
-logger_t *logger_create(char *logger_name, logger_level_t log_level,FILE * output);
+logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_thread_id, FILE * output);
 
 
 #endif /*LOGGER_H_*/
index 657d2e3..d00761d 100644 (file)
 
 mapping_t logger_context_t_mappings[] = {
        {PARSER, "PARSER"},
-       {GENERATOR, "GENERATOR"},
+       {GENERATOR, "GENRAT"},
        {IKE_SA, "IKE_SA"},
-       {IKE_SA_MANAGER, "IKE_SA_MANAGER"},
-       {MESSAGE, "MESSAGE"},
-       {THREAD_POOL, "THREAD_POOL"},
-       {WORKER_THREAD, "WORKER_THREAD"},
-       {SCHEDULER_THREAD, "SCHEDULER_THREAD"},
-       {SENDER_THREAD, "SENDER_THREAD"},
-       {RECEIVER_THREAD, "RECEIVER_THREAD"},
+       {IKE_SA_MANAGER, "ISAMGR"},
+       {MESSAGE, "MESSAG"},
+       {THREAD_POOL, "THPOOL"},
+       {WORKER, "WORKER"},
+       {SCHEDULER, "SCHEDU"},
+       {SENDER, "SENDER"},
+       {RECEIVER, "RECEVR"},
+       {SOCKET, "SOCKET"},
        {TESTER, "TESTER"},
        {DAEMON, "DAEMON"},
 };
@@ -114,21 +115,31 @@ static logger_t *create_logger(private_logger_manager_t *this, logger_context_t
 {
        
        char * context_name;
+       bool log_thread_ids = TRUE;
        FILE * output = NULL;
        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);
+       
+       context_name = mapping_find(logger_context_t_mappings,context);
 
        switch(context)
        {
                case TESTER:
-                       context_name = "TESTER";
+                       log_thread_ids = FALSE;
                        output = stdout;
                        break;
+               case SCHEDULER:
+               case SENDER:
+               case RECEIVER:
+               case THREAD_POOL:
+               case SOCKET:
+               case DAEMON:
+                       log_thread_ids = FALSE;
+                       break;
                default:
-                       context_name = mapping_find(logger_context_t_mappings,context);
-                       break;          
+                       break;
        }
        /* logger manager is thread save */
        pthread_mutex_lock(&(this->mutex));
@@ -136,11 +147,11 @@ static logger_t *create_logger(private_logger_manager_t *this, logger_context_t
        {
                snprintf(buffer, MAX_LOGGER_NAME, "%s - %s",context_name,name);
                        /* create logger with default log_level */
-               logger = logger_create(buffer,logger_level,output);
+               logger = logger_create(buffer,logger_level,log_thread_ids,output);
        }
        else
        {
-               logger = logger_create(context_name,logger_level,output);
+               logger = logger_create(context_name,logger_level,log_thread_ids,output);
        }
        
        
index 71c693a..2bd3059 100644 (file)
@@ -39,10 +39,11 @@ enum logger_context_e{
        IKE_SA_MANAGER,
        MESSAGE,
        THREAD_POOL,
-       WORKER_THREAD,
-       SCHEDULER_THREAD,
-       SENDER_THREAD,
-       RECEIVER_THREAD,
+       WORKER,
+       SCHEDULER,
+       SENDER,
+       RECEIVER,
+       SOCKET,
        TESTER,
        DAEMON,
        };