Merge commit 'upstream/0.3.2'
[debian/uanytun.git] / src / log_targets.h
index 0ea8e83..8db3812 100644 (file)
@@ -20,8 +20,9 @@
  *  This file is part of uAnytun.
  *
  *  uAnytun is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License version 3 as
- *  published by the Free Software Foundation.
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  any later version.
  *
  *  uAnytun is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#ifndef UANYTUN_log_targets_h_INCLUDED
+#define UANYTUN_log_targets_h_INCLUDED
+
+#include <time.h>
+
+static char* get_time_formatted()
+{
+  char* time_string;
+  time_t t = time(NULL);
+  if(t < 0) 
+    time_string = "<time read error>";
+  else {
+    time_string = ctime(&t);
+    if(!time_string)
+      time_string = "<time format error>";
+    else {
+      char* newline = strchr(time_string, '\n');
+      if(newline)
+        newline[0] = 0;
+    }
+  }
+  return time_string;
+}
+
 enum syslog_facility_enum { USER = LOG_USER, MAIL = LOG_MAIL,
                             DAEMON = LOG_DAEMON, AUTH = LOG_AUTH,
                             SYSLOG = LOG_SYSLOG, LPR = LOG_LPR,
@@ -235,7 +260,7 @@ void log_target_file_log(log_target_t* self, log_prio_t prio, const char* msg)
   if(!self || !self->param_ || !self->opened_)
     return;
 
-  fprintf(((log_target_file_param_t*)(self->param_))->file_, "%s-%s\n", log_prio_to_string(prio), msg);
+  fprintf(((log_target_file_param_t*)(self->param_))->file_, "%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
   fflush(((log_target_file_param_t*)(self->param_))->file_);
 }
 
@@ -284,7 +309,7 @@ log_target_t* log_target_file_new()
 
 void log_target_stdout_log(log_target_t* self, log_prio_t prio, const char* msg)
 {
-  printf("%s-%s\n", log_prio_to_string(prio), msg);
+  printf("%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
 }
 
 log_target_t* log_target_stdout_new()
@@ -311,7 +336,7 @@ log_target_t* log_target_stdout_new()
 
 void log_target_stderr_log(log_target_t* self, log_prio_t prio, const char* msg)
 {
-  fprintf(stderr, "%s-%s\n", log_prio_to_string(prio), msg);
+  fprintf(stderr, "%s %s: %s\n", get_time_formatted(), log_prio_to_string(prio), msg);
 }
 
 log_target_t* log_target_stderr_new()
@@ -334,3 +359,5 @@ log_target_t* log_target_stderr_new()
 
   return tmp;
 }
+
+#endif