YAZ_LOG environment variable.
[yaz-moved-to-github.git] / src / log.c
index f3db8b5..fda75d5 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2005, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: log.c,v 1.28 2005-09-16 21:13:54 adam Exp $
+ * $Id: log.c,v 1.30 2006-03-13 17:33:19 mike Exp $
  */
 
 /**
@@ -53,7 +53,17 @@ char *strerror(int n)
 
 #endif
 
-static int l_level = YLOG_DEFAULT_LEVEL;
+
+static int default_log_level() {
+    char *env = getenv("YAZ_LOG");
+    if (env != 0)
+        return yaz_log_mask_str_x(env, YLOG_DEFAULT_LEVEL);
+    else
+        return YLOG_DEFAULT_LEVEL;
+}
+
+
+static int l_level = -1;        /* will be set from default_log_level() */
 static FILE *l_file = NULL;
 static char l_prefix[512] = "";
 static char l_prefix2[512] = "";
@@ -142,6 +152,7 @@ static void rotate_log(const char *cur_fname)
 void yaz_log_init_level(int level)
 {
     init_mutex();
+    if (l_level < 0) l_level = default_log_level();
     if ( (l_level & YLOG_FLUSH) != (level & YLOG_FLUSH) )
     {
         l_level = level;
@@ -224,32 +235,21 @@ void log_event_end(void (*func)(int, const char *, void *), void *info)
     end_hook_info = info;
 }
 
-static void yaz_log_open_check(int force)
+static void yaz_log_open_check(struct tm *tm, int force)
 {
     char new_filename[512];
     static char cur_filename[512] = "";
-    time_t new_time;
-    static time_t cur_time = 0;
-    struct tm tm;
 
-    nmem_mutex_enter(log_mutex);
     if (!l_file)
         l_file = stderr;
 
     if (l_fname && *l_fname)
     {
-        time(&new_time);
-        if (new_time != cur_time)
+        strftime(new_filename, sizeof(new_filename)-1, l_fname, tm);
+        if (strcmp(new_filename, cur_filename))
         {
-            cur_time = new_time;
-            localtime_r(&new_time, &tm);
-            
-            strftime(new_filename, sizeof(new_filename)-1, l_fname, &tm);
-            if (strcmp(new_filename, cur_filename))
-            {
-                strcpy(cur_filename, new_filename);
-                force = 1;
-            }
+            strcpy(cur_filename, new_filename);
+            force = 1;
         }
         if (l_max_size > 0 && (l_file && l_file != stderr))
         {
@@ -265,16 +265,30 @@ static void yaz_log_open_check(int force)
             if (l_file && l_file != stderr)
                 fclose(l_file);
             l_file = fopen(cur_filename, "a");
+            if (l_level < 0) l_level = default_log_level();
             if (l_level & YLOG_FLUSH)
                 setvbuf(l_file, 0, _IONBF, 0);
         }
     }
-    nmem_mutex_leave(log_mutex);
 }
 
 void yaz_log_reopen()
 {
-    yaz_log_open_check(1);
+    time_t cur_time = time(0);
+#if HAVE_LOCALTIME_R
+    struct tm tm0, *tm = &tm0;
+#else
+    struct tm *tm;
+#endif
+
+    nmem_mutex_enter(log_mutex);
+#if HAVE_LOCALTIME_R
+    localtime_r(&cur_time, tm);
+#else
+    tm = localtime(&cur_time);
+#endif
+    yaz_log_open_check(tm, 1);
+    nmem_mutex_leave(log_mutex);
 }
 
 void yaz_log(int level, const char *fmt, ...)
@@ -283,17 +297,29 @@ void yaz_log(int level, const char *fmt, ...)
     char buf[4096], flags[1024];
     int i;
     time_t ti;
-    struct tm *tim;
+#if HAVE_LOCALTIME_R
+    struct tm tm0, *tm = &tm0;
+#else
+    struct tm *tm;
+#endif
     char tbuf[TIMEFORMAT_LEN] = "";
     int o_level = level;
 
+    if (l_level < 0) l_level = default_log_level();
     if (!(level & l_level))
         return;
     init_mutex();
 
-    yaz_log_open_check(0);
-
     nmem_mutex_enter(log_mutex);
+    ti = time(0);
+#if HAVE_LOCALTIME_R
+    localtime_r(&ti, tm);
+#else
+    tm = localtime(&ti);
+#endif
+
+    yaz_log_open_check(tm, 0);
+
     if (!l_file)
         l_file = stderr;
 
@@ -330,12 +356,10 @@ void yaz_log(int level, const char *fmt, ...)
     va_end (ap);
     if (start_hook_func)
         (*start_hook_func)(o_level, buf, start_hook_info);
-    ti = time(0);
-    tim = localtime(&ti);
     if (l_level & YLOG_NOTIME)
         tbuf[0] = '\0';
     else
-        strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tim);
+        strftime(tbuf, TIMEFORMAT_LEN-1, l_actual_format, tm);
     tbuf[TIMEFORMAT_LEN-1] = '\0';
     fprintf(l_file, "%s %s%s %s%s\n", tbuf, l_prefix, flags,
             l_prefix2, buf);
@@ -432,7 +456,7 @@ int yaz_log_module_level(const char *name)
 
 int yaz_log_mask_str(const char *str)
 {
-    return yaz_log_mask_str_x(str, YLOG_DEFAULT_LEVEL);
+    return yaz_log_mask_str_x(str, default_log_level());
 }
 
 int yaz_log_mask_str_x(const char *str, int level)
@@ -478,4 +502,3 @@ int yaz_log_mask_str_x(const char *str, int level)
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
-