Don't modify the message string passed to logger, as it gets reused
authorMartin Willi <martin@revosec.ch>
Fri, 13 Jul 2012 13:42:14 +0000 (15:42 +0200)
committerMartin Willi <martin@revosec.ch>
Fri, 13 Jul 2012 13:43:04 +0000 (15:43 +0200)
src/libcharon/bus/listeners/file_logger.c
src/libcharon/bus/listeners/logger.h
src/libcharon/bus/listeners/sys_logger.c
src/libcharon/control/controller.c
src/libcharon/control/controller.h
src/libcharon/plugins/android/android_logger.c
src/libcharon/plugins/sql/sql_logger.c

index 77af9e4..9c8458e 100644 (file)
@@ -62,10 +62,10 @@ struct private_file_logger_t {
 
 METHOD(logger_t, log_, void,
        private_file_logger_t *this, debug_t group, level_t level, int thread,
-       ike_sa_t* ike_sa, char *message)
+       ike_sa_t* ike_sa, const char *message)
 {
        char timestr[128], namestr[128] = "";
-       char *current = message, *next;
+       const char *current = message, *next;
        struct tm tm;
        time_t t;
 
@@ -95,24 +95,26 @@ METHOD(logger_t, log_, void,
 
        /* prepend a prefix in front of every line */
        this->mutex->lock(this->mutex);
-       while (current)
+       while (TRUE)
        {
                next = strchr(current, '\n');
-               if (next)
-               {
-                       *(next++) = '\0';
-               }
                if (this->time_format)
                {
-                       fprintf(this->out, "%s %.2d[%N]%s %s\n",
-                                       timestr, thread, debug_names, group, namestr, current);
+                       fprintf(this->out, "%s %.2d[%N]%s ",
+                                       timestr, thread, debug_names, group, namestr);
                }
                else
                {
-                       fprintf(this->out, "%.2d[%N]%s %s\n",
-                                       thread, debug_names, group, namestr, current);
+                       fprintf(this->out, "%.2d[%N]%s ",
+                                       thread, debug_names, group, namestr);
+               }
+               if (next == NULL)
+               {
+                       fprintf(this->out, "%s\n", current);
+                       break;
                }
-               current = next;
+               fprintf(this->out, "%.*s\n", (int)(next - current), current);
+               current = next + 1;
        }
        this->mutex->unlock(this->mutex);
 }
index 0306536..3b99e7d 100644 (file)
@@ -45,7 +45,7 @@ struct logger_t {
         * @param message       log message
         */
        void (*log)(logger_t *this, debug_t group, level_t level, int thread,
-                               ike_sa_t *ike_sa, char* message);
+                               ike_sa_t *ike_sa, const char *message);
 
        /**
         * Get the desired log level for a debug group.  This is called during
index 9962fe0..53fdefe 100644 (file)
@@ -57,10 +57,10 @@ struct private_sys_logger_t {
 
 METHOD(logger_t, log_, void,
        private_sys_logger_t *this, debug_t group, level_t level, int thread,
-       ike_sa_t* ike_sa, char *message)
+       ike_sa_t* ike_sa, const char *message)
 {
        char groupstr[4], namestr[128] = "";
-       char *current = message, *next;
+       const char *current = message, *next;
 
        /* cache group name and optional name string */
        snprintf(groupstr, sizeof(groupstr), "%N", debug_names, group);
@@ -81,16 +81,18 @@ METHOD(logger_t, log_, void,
 
        /* do a syslog for every line */
        this->mutex->lock(this->mutex);
-       while (current)
+       while (TRUE)
        {
                next = strchr(current, '\n');
-               if (next)
+               if (next == NULL)
                {
-                       *(next++) = '\0';
+                       syslog(this->facility | LOG_INFO, "%.2d[%s]%s %s\n",
+                                  thread, groupstr, namestr, current);
+                       break;
                }
-               syslog(this->facility|LOG_INFO, "%.2d[%s]%s %s\n",
-                          thread, groupstr, namestr, current);
-               current = next;
+               syslog(this->facility | LOG_INFO, "%.2d[%s]%s %.*s\n",
+                          thread, groupstr, namestr, (int)(next - current), current);
+               current = next + 1;
        }
        this->mutex->unlock(this->mutex);
 }
index 0f98eb8..77d73db 100644 (file)
@@ -209,7 +209,7 @@ static bool wait_for_listener(interface_job_t *job, u_int timeout)
 
 METHOD(logger_t, listener_log, void,
        interface_logger_t *this, debug_t group, level_t level, int thread,
-       ike_sa_t *ike_sa, char* message)
+       ike_sa_t *ike_sa, const char *message)
 {
        ike_sa_t *target;
 
@@ -627,7 +627,7 @@ METHOD(controller_t, terminate_child, status_t,
  * See header
  */
 bool controller_cb_empty(void *param, debug_t group, level_t level,
-                                                ike_sa_t *ike_sa, char *message)
+                                                ike_sa_t *ike_sa, const char *message)
 {
        return TRUE;
 }
index 26614af..222285c 100644 (file)
@@ -34,7 +34,7 @@
  * @return                             FALSE to return from called controller method
  */
 typedef bool (*controller_cb_t)(void* param, debug_t group, level_t level,
-                                                               ike_sa_t* ike_sa, char* message);
+                                                               ike_sa_t* ike_sa, const char *message);
 
 /**
  * Empty callback function for controller_t methods.
@@ -43,7 +43,7 @@ typedef bool (*controller_cb_t)(void* param, debug_t group, level_t level,
  * this function to the controller methods.
  */
 bool controller_cb_empty(void *param, debug_t group, level_t level,
-                                                ike_sa_t *ike_sa, char *message);
+                                                ike_sa_t *ike_sa, const char *message);
 
 typedef struct controller_t controller_t;
 
index 1f6042c..0c5f609 100644 (file)
@@ -45,26 +45,27 @@ struct private_android_logger_t {
        mutex_t *mutex;
 };
 
-
 METHOD(logger_t, log_, void,
        private_android_logger_t *this, debug_t group, level_t level,
-       int thread, ike_sa_t* ike_sa, char *message)
+       int thread, ike_sa_t* ike_sa, const char *message)
 {
        int prio = level > 1 ? ANDROID_LOG_DEBUG : ANDROID_LOG_INFO;
        char sgroup[16];
-       char *current = message, *next;
+       const char *current = message, *next;
        snprintf(sgroup, sizeof(sgroup), "%N", debug_names, group);
        this->mutex->lock(this->mutex);
-       while (current)
+       while (TRUE)
        {       /* log each line separately */
                next = strchr(current, '\n');
-               if (next)
+               if (next == NULL)
                {
-                       *(next++) = '\0';
+                       __android_log_print(prio, "charon", "%.2d[%s] %s\n",
+                                                               thread, sgroup, current);
+                       break;
                }
-               __android_log_print(prio, "charon", "%.2d[%s] %s\n",
-                                                       thread, sgroup, current);
-               current = next;
+               __android_log_print(prio, "charon", "%.2d[%s] %.*s\n",
+                                                       thread, sgroup, (int)(next - current), current);
+               current = next + 1;
        }
        this->mutex->unlock(this->mutex);
 }
index 0930535..6db3258 100644 (file)
@@ -50,7 +50,7 @@ struct private_sql_logger_t {
 
 METHOD(logger_t, log_, void,
        private_sql_logger_t *this, debug_t group, level_t level, int thread,
-       ike_sa_t* ike_sa, char *message)
+       ike_sa_t* ike_sa, const char *message)
 {
        if (this->recursive->get(this->recursive))
        {