]> arthur.barton.de Git - netdata.git/blobdiff - src/log.c
Merge pull request #1132 from x4FF3/master
[netdata.git] / src / log.c
index fbb172b0a05450c6de2f608379766b1289094464..86716987aeba7eab4dbab471732d5dcafa4a9da3 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -90,22 +90,22 @@ int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog)
 
 void reopen_all_log_files() {
     if(stdout_filename)
-        open_log_file(STDOUT_FILENO, &stdout, stdout_filename, &output_log_syslog);
+        open_log_file(STDOUT_FILENO, (FILE **)&stdout, stdout_filename, &output_log_syslog);
 
     if(stderr_filename)
-        open_log_file(STDERR_FILENO, &stderr, stderr_filename, &error_log_syslog);
+        open_log_file(STDERR_FILENO, (FILE **)&stderr, stderr_filename, &error_log_syslog);
 
     if(stdaccess_filename)
-        stdaccess_fd = open_log_file(stdaccess_fd, &stdaccess, stdaccess_filename, &access_log_syslog);
+        stdaccess_fd = open_log_file(stdaccess_fd, (FILE **)&stdaccess, stdaccess_filename, &access_log_syslog);
 }
 
 void open_all_log_files() {
     // disable stdin
-    open_log_file(STDIN_FILENO, &stdin, "/dev/null", NULL);
+    open_log_file(STDIN_FILENO, (FILE **)&stdin, "/dev/null", NULL);
 
-    open_log_file(STDOUT_FILENO, &stdout, stdout_filename, &output_log_syslog);
-    open_log_file(STDERR_FILENO, &stderr, stderr_filename, &error_log_syslog);
-    stdaccess_fd = open_log_file(stdaccess_fd, &stdaccess, stdaccess_filename, &access_log_syslog);
+    open_log_file(STDOUT_FILENO, (FILE **)&stdout, stdout_filename, &output_log_syslog);
+    open_log_file(STDERR_FILENO, (FILE **)&stderr, stderr_filename, &error_log_syslog);
+    stdaccess_fd = open_log_file(stdaccess_fd, (FILE **)&stdaccess, stdaccess_filename, &access_log_syslog);
 }
 
 // ----------------------------------------------------------------------------
@@ -200,7 +200,7 @@ int error_log_limit(int reset) {
 
 void log_date(FILE *out)
 {
-        char outstr[24];
+        char outstr[26];
         time_t t;
         struct tm *tmp, tmbuf;
 
@@ -208,7 +208,7 @@ void log_date(FILE *out)
         tmp = localtime_r(&t, &tmbuf);
 
         if (tmp == NULL) return;
-        if (unlikely(strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0)) return;
+        if (unlikely(strftime(outstr, sizeof(outstr), "%Y-%m-%d %H:%M:%S", tmp) == 0)) return;
 
         fprintf(out, "%s: ", outstr);
 }
@@ -222,7 +222,7 @@ void debug_int( const char *file, const char *function, const unsigned long line
 
     log_date(stdout);
     va_start( args, fmt );
-    printf("DEBUG (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
+    printf("%s: DEBUG (%04lu@%-10.10s:%-15.15s): ", program_name, line, file, function);
     vprintf(fmt, args);
     va_end( args );
     putchar('\n');
@@ -249,8 +249,8 @@ void info_int( const char *file, const char *function, const unsigned long line,
     log_date(stderr);
 
     va_start( args, fmt );
-    if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
-    else            fprintf(stderr, "INFO: %s: ", program_name);
+    if(debug_flags) fprintf(stderr, "%s: INFO: (%04lu@%-10.10s:%-15.15s):", program_name, line, file, function);
+    else            fprintf(stderr, "%s: INFO: ", program_name);
     vfprintf( stderr, fmt, args );
     va_end( args );
 
@@ -266,6 +266,28 @@ void info_int( const char *file, const char *function, const unsigned long line,
 // ----------------------------------------------------------------------------
 // error log
 
+#if defined(STRERROR_R_CHAR_P)
+// GLIBC version of strerror_r
+static const char *strerror_result(const char *a, const char *b) { (void)b; return a; }
+#elif defined(HAVE_STRERROR_R)
+// POSIX version of strerror_r
+static const char *strerror_result(int a, const char *b) { (void)a; return b; }
+#elif defined(HAVE_C__GENERIC)
+
+// what a trick!
+// http://stackoverflow.com/questions/479207/function-overloading-in-c
+static const char *strerror_result_int(int a, const char *b) { (void)a; return b; }
+static const char *strerror_result_string(const char *a, const char *b) { (void)b; return a; }
+
+#define strerror_result(a, b) _Generic((a), \
+    int: strerror_result_int, \
+    char *: strerror_result_string \
+    )(a, b)
+
+#else
+#error "cannot detect the format of function strerror_r()"
+#endif
+
 void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
 {
     va_list args;
@@ -276,14 +298,14 @@ void error_int( const char *prefix, const char *file, const char *function, cons
     log_date(stderr);
 
     va_start( args, fmt );
-    if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
-    else            fprintf(stderr, "%s: %s: ", prefix, program_name);
+    if(debug_flags) fprintf(stderr, "%s: %s: (%04lu@%-10.10s:%-15.15s): ", program_name, prefix, line, file, function);
+    else            fprintf(stderr, "%s: %s: ", program_name, prefix);
     vfprintf( stderr, fmt, args );
     va_end( args );
 
     if(errno) {
         char buf[1024];
-        fprintf(stderr, " (errno %d, %s)\n", errno, strerror_r(errno, buf, 1023));
+        fprintf(stderr, " (errno %d, %s)\n", errno, strerror_result(strerror_r(errno, buf, 1023), buf));
         errno = 0;
     }
     else
@@ -303,8 +325,8 @@ void fatal_int( const char *file, const char *function, const unsigned long line
     log_date(stderr);
 
     va_start( args, fmt );
-    if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
-    else            fprintf(stderr, "FATAL: %s: ", program_name);
+    if(debug_flags) fprintf(stderr, "%s: FATAL: (%04lu@%-10.10s:%-15.15s): ", program_name, line, file, function);
+    else            fprintf(stderr, "%s: FATAL: ", program_name);
     vfprintf( stderr, fmt, args );
     va_end( args );