Added a ike_name logger option to prefix the IKE_SA name on each line
authorMartin Willi <martin@revosec.ch>
Wed, 25 Aug 2010 07:53:43 +0000 (09:53 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 25 Aug 2010 07:55:37 +0000 (09:55 +0200)
src/libcharon/bus/listeners/file_logger.c
src/libcharon/bus/listeners/file_logger.h
src/libcharon/bus/listeners/sys_logger.c
src/libcharon/bus/listeners/sys_logger.h
src/libcharon/daemon.c

index 87db532..157436a 100644 (file)
@@ -46,6 +46,11 @@ struct private_file_logger_t {
         * strftime() format of time prefix, if any
         */
        char *time_format;
+
+       /**
+        * Print the name/# of the IKE_SA?
+        */
+       bool ike_name;
 };
 
 /**
@@ -56,7 +61,7 @@ static bool log_(private_file_logger_t *this, debug_t group, level_t level,
 {
        if (level <= this->levels[group])
        {
-               char buffer[8192], timestr[128];
+               char buffer[8192], timestr[128], namestr[128] = "";
                char *current = buffer, *next;
                struct tm tm;
                time_t t;
@@ -67,6 +72,23 @@ static bool log_(private_file_logger_t *this, debug_t group, level_t level,
                        localtime_r(&t, &tm);
                        strftime(timestr, sizeof(timestr), this->time_format, &tm);
                }
+               if (this->ike_name && ike_sa)
+               {
+                       if (ike_sa->get_peer_cfg(ike_sa))
+                       {
+                               snprintf(namestr, sizeof(namestr), " <%s|%d>",
+                                       ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa));
+                       }
+                       else
+                       {
+                               snprintf(namestr, sizeof(namestr), " <%d>",
+                                       ike_sa->get_unique_id(ike_sa));
+                       }
+               }
+               else
+               {
+                       namestr[0] = '\0';
+               }
 
                /* write in memory buffer first */
                vsnprintf(buffer, sizeof(buffer), format, args);
@@ -81,13 +103,13 @@ static bool log_(private_file_logger_t *this, debug_t group, level_t level,
                        }
                        if (this->time_format)
                        {
-                               fprintf(this->out, "%s %.2d[%N] %s\n",
-                                               timestr, thread, debug_names, group, current);
+                               fprintf(this->out, "%s %.2d[%N]%s %s\n",
+                                               timestr, thread, debug_names, group, namestr, current);
                        }
                        else
                        {
-                               fprintf(this->out, "%.2d[%N] %s\n",
-                                               thread, debug_names, group, current);
+                               fprintf(this->out, "%.2d[%N]%s %s\n",
+                                               thread, debug_names, group, namestr, current);
                        }
                        current = next;
                }
@@ -129,7 +151,7 @@ static void destroy(private_file_logger_t *this)
 /*
  * Described in header.
  */
-file_logger_t *file_logger_create(FILE *out, char *time_format)
+file_logger_t *file_logger_create(FILE *out, char *time_format, bool ike_name)
 {
        private_file_logger_t *this = malloc_thing(private_file_logger_t);
 
@@ -142,6 +164,7 @@ file_logger_t *file_logger_create(FILE *out, char *time_format)
        /* private variables */
        this->out = out;
        this->time_format = time_format;
+       this->ike_name = ike_name;
        set_level(this, DBG_ANY, LEVEL_SILENT);
 
        return &this->public;
index e02a12c..d02f170 100644 (file)
@@ -54,8 +54,9 @@ struct file_logger_t {
  *
  * @param out                  FILE to write to
  * @param time_format  format of timestamp prefix, as in strftime()
+ * @param ike_name             TRUE to prefix the name of the IKE_SA
  * @return                             file_logger_t object
  */
-file_logger_t *file_logger_create(FILE *out, char *time_format);
+file_logger_t *file_logger_create(FILE *out, char *time_format, bool ike_name);
 
 #endif /** FILE_LOGGER_H_ @}*/
index 5bc1d58..fa394ba 100644 (file)
@@ -41,6 +41,11 @@ struct private_sys_logger_t {
         * Maximum level to log, for each group
         */
        level_t levels[DBG_MAX];
+
+       /**
+        * Print the name/# of the IKE_SA?
+        */
+       bool ike_name;
 };
 
 /**
@@ -51,12 +56,26 @@ static bool log_(private_sys_logger_t *this, debug_t group, level_t level,
 {
        if (level <= this->levels[group])
        {
-               char buffer[8192];
+               char buffer[8192], namestr[128] = "";
                char *current = buffer, *next;
 
                /* write in memory buffer first */
                vsnprintf(buffer, sizeof(buffer), format, args);
 
+               if (this->ike_name && ike_sa)
+               {
+                       if (ike_sa->get_peer_cfg(ike_sa))
+                       {
+                               snprintf(namestr, sizeof(namestr), " <%s|%d>",
+                                       ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa));
+                       }
+                       else
+                       {
+                               snprintf(namestr, sizeof(namestr), " <%d>",
+                                       ike_sa->get_unique_id(ike_sa));
+                       }
+               }
+
                /* do a syslog with every line */
                while (current)
                {
@@ -65,8 +84,8 @@ static bool log_(private_sys_logger_t *this, debug_t group, level_t level,
                        {
                                *(next++) = '\0';
                        }
-                       syslog(this->facility|LOG_INFO, "%.2d[%N] %s\n",
-                                  thread, debug_names, group, current);
+                       syslog(this->facility|LOG_INFO, "%.2d[%N]%s %s\n",
+                                  thread, debug_names, group, namestr, current);
                        current = next;
                }
        }
@@ -104,7 +123,7 @@ static void destroy(private_sys_logger_t *this)
 /*
  * Described in header.
  */
-sys_logger_t *sys_logger_create(int facility)
+sys_logger_t *sys_logger_create(int facility, bool ike_name)
 {
        private_sys_logger_t *this = malloc_thing(private_sys_logger_t);
 
@@ -116,6 +135,7 @@ sys_logger_t *sys_logger_create(int facility)
 
        /* private variables */
        this->facility = facility;
+       this->ike_name = ike_name;
        set_level(this, DBG_ANY, LEVEL_SILENT);
 
        return &this->public;
index 58d4de5..d83715a 100644 (file)
@@ -53,8 +53,9 @@ struct sys_logger_t {
  * Constructor to create a sys_logger_t object.
  *
  * @param facility     syslog facility to use
+ * @param ike_name     TRUE to prefix the name of the IKE_SA
  * @return                     sys_logger_t object
  */
-sys_logger_t *sys_logger_create(int facility);
+sys_logger_t *sys_logger_create(int facility, bool ike_name);
 
 #endif /** SYS_LOGGER_H_ @}*/
index c022702..252c155 100644 (file)
@@ -213,7 +213,7 @@ static void initialize_loggers(private_daemon_t *this, bool use_stderr,
        int loggers_defined = 0;
        debug_t group;
        level_t  def;
-       bool append;
+       bool append, ike_name;
        FILE *file;
 
        /* setup sysloggers */
@@ -222,13 +222,16 @@ static void initialize_loggers(private_daemon_t *this, bool use_stderr,
        while (enumerator->enumerate(enumerator, &facility))
        {
                loggers_defined++;
+
+               ike_name = lib->settings->get_bool(lib->settings,
+                                                               "charon.syslog.%s.ike_name", FALSE, facility);
                if (streq(facility, "daemon"))
                {
-                       sys_logger = sys_logger_create(LOG_DAEMON);
+                       sys_logger = sys_logger_create(LOG_DAEMON, ike_name);
                }
                else if (streq(facility, "auth"))
                {
-                       sys_logger = sys_logger_create(LOG_AUTHPRIV);
+                       sys_logger = sys_logger_create(LOG_AUTHPRIV, ike_name);
                }
                else
                {
@@ -282,7 +285,9 @@ static void initialize_loggers(private_daemon_t *this, bool use_stderr,
                }
                file_logger = file_logger_create(file,
                                                lib->settings->get_str(lib->settings,
-                                                       "charon.filelog.%s.time_format", NULL, filename));
+                                                       "charon.filelog.%s.time_format", NULL, filename),
+                                               lib->settings->get_bool(lib->settings,
+                                                       "charon.filelog.%s.ike_name", FALSE, filename));
                def = lib->settings->get_int(lib->settings,
                                                                         "charon.filelog.%s.default", 1, filename);
                for (group = 0; group < DBG_MAX; group++)
@@ -303,12 +308,12 @@ static void initialize_loggers(private_daemon_t *this, bool use_stderr,
        if (!loggers_defined)
        {
                /* set up default stdout file_logger */
-               file_logger = file_logger_create(stdout, NULL);
+               file_logger = file_logger_create(stdout, NULL, FALSE);
                this->public.bus->add_listener(this->public.bus, &file_logger->listener);
                this->public.file_loggers->insert_last(this->public.file_loggers,
                                                                                           file_logger);
                /* set up default daemon sys_logger */
-               sys_logger = sys_logger_create(LOG_DAEMON);
+               sys_logger = sys_logger_create(LOG_DAEMON, FALSE);
                this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
                this->public.sys_loggers->insert_last(this->public.sys_loggers,
                                                                                          sys_logger);
@@ -322,7 +327,7 @@ static void initialize_loggers(private_daemon_t *this, bool use_stderr,
                }
 
                /* set up default auth sys_logger */
-               sys_logger = sys_logger_create(LOG_AUTHPRIV);
+               sys_logger = sys_logger_create(LOG_AUTHPRIV, FALSE);
                this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
                this->public.sys_loggers->insert_last(this->public.sys_loggers,
                                                                                          sys_logger);