Added a way to get log back to stderr
[yaz-moved-to-github.git] / util / log.c
index 46412af..5056d01 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 1995-2002, Index Data
+ * Copyright (c) 1995-2003, Index Data
  * See the file LICENSE for details.
  *
- * $Id: log.c,v 1.27 2002-06-18 21:30:39 adam Exp $
+ * $Id: log.c,v 1.34 2003-02-07 14:39:00 heikki Exp $
  */
 
 #if HAVE_CONFIG_H
@@ -20,6 +20,7 @@
 #include <stdarg.h>
 #include <errno.h>
 #include <time.h>
+#include <yaz/nmem.h>
 #include <yaz/log.h>
 
 #define HAS_STRERROR 1
@@ -38,6 +39,7 @@ char *strerror(int n)
 static int l_level = LOG_DEFAULT_LEVEL;
 static FILE *l_file = NULL;
 static char l_prefix[512] = "";
+static char l_prefix2[512] = "";
 
 static struct {
     int mask;
@@ -68,8 +70,8 @@ void yaz_log_init_file (const char *fname)
     if (!l_file)
         l_file = stderr;
     if (!fname || !*fname)
-        return;
-    if (!(new_file = fopen(fname, "a")))
+       new_file=stderr;
+    else if (!(new_file = fopen(fname, "a")))
         return;
     if (l_file != stderr)
     {
@@ -92,11 +94,20 @@ void yaz_log_init_prefix (const char *prefix)
         *l_prefix = 0;
 }
 
+void yaz_log_init_prefix2 (const char *prefix)
+{
+    if (prefix && *prefix)
+       sprintf(l_prefix2, "%.511s ", prefix);
+    else
+        *l_prefix2 = 0;
+}
+
 void yaz_log_init(int level, const char *prefix, const char *fname)
 {
     yaz_log_init_level (level);
     yaz_log_init_prefix (prefix);
-    yaz_log_init_file (fname);
+    if (fname && *fname)
+        yaz_log_init_file (fname);
 }
 
 static void (*start_hook_func)(int, const char *, void *) = NULL;
@@ -152,31 +163,18 @@ void yaz_log(int level, const char *fmt, ...)
 /* WIN32 */
     if (o_level & LOG_ERRNO)
     {
-#ifdef WIN32
-       DWORD err = GetLastError();
-       if (err)
-       {
-           strcat(buf, " [");
-           FormatMessage(
-               FORMAT_MESSAGE_FROM_SYSTEM,
-               NULL,
-               err,
-               MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
-               (LPTSTR) buf + strlen(buf),
-               2048,
-               NULL);
-           strcat(buf, "]");
-       }
-#else
-        sprintf(buf + strlen(buf), " [%s]", strerror(errno));
-#endif
+       strcat(buf, " [");
+       yaz_strerror(buf+strlen(buf), 2048);
+       strcat(buf, "]");
     }
+    va_end (ap);
     if (start_hook_func)
         (*start_hook_func)(o_level, buf, start_hook_info);
     ti = time(0);
     tim = localtime(&ti);
     strftime(tbuf, 50, "%H:%M:%S-%d/%m", tim);
-    fprintf(l_file, "%s: %s%s %s\n", tbuf, l_prefix, flags, buf);
+    fprintf(l_file, "%s: %s%s %s%s\n", tbuf, l_prefix, flags,
+            l_prefix2, buf);
     fflush(l_file);
     if (end_hook_func)
        (*end_hook_func)(o_level, buf, end_hook_info);