2007-07-17 [paul] 2.10.0cvs35
[claws.git] / src / common / log.c
index 0e8ea6db5dbe85f269f783105788c80dc803a09d..e44b86f2c30e2284844806226460f2d9e42415ec 100644 (file)
@@ -4,7 +4,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 
 #include "defs.h"
 
+#ifdef G_OS_WIN32
+#  include <w32lib.h>
+#endif
+
 #include <stdio.h>
 #include <glib.h>
+#include <glib/gi18n.h>
 
 #include "utils.h"
 #include "log.h"
 #include "hooks.h"
 
-static FILE *log_fp = NULL;
+static FILE *log_fp[LOG_INSTANCE_MAX] = {
+       NULL,
+       NULL
+};
+
+typedef struct _LogInstanceData LogInstanceData;
+
+struct _LogInstanceData {
+       const char *hook;
+       gchar *title;
+       int *prefs_logwin_width;
+       int *prefs_logwin_height;
+};
+
+static LogInstanceData log_instances[LOG_INSTANCE_MAX] = {
+       { LOG_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL },
+       { DEBUG_FILTERING_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL }
+};
+
+gboolean prefs_common_enable_log_standard(void);
+gboolean prefs_common_enable_log_warning(void);
+gboolean prefs_common_enable_log_error(void);
+gboolean prefs_common_enable_log_status(void);
 
-gboolean invoke_hook_cb (gpointer data)
+static gboolean invoke_hook_cb (gpointer data)
 {
        LogText *logtext = (LogText *)data;
-       hooks_invoke(LOG_APPEND_TEXT_HOOKLIST, logtext);
+       hooks_invoke(get_log_hook(logtext->instance), logtext);
        g_free(logtext->text);
        g_free(logtext);
        return FALSE;
 }
 
-void set_log_file(const gchar *filename)
+void set_log_file(LogInstance instance, const gchar *filename)
 {
-       if (log_fp)
+       if (log_fp[instance])
                return;
 
        /* backup old logfile if existing */
@@ -56,28 +83,58 @@ void set_log_file(const gchar *filename)
                g_free(backupname);
        }
 
-       log_fp = g_fopen(filename, "wb");
-       if (!log_fp)
+       log_fp[instance] = g_fopen(filename, "wb");
+       if (!log_fp[instance])
                FILE_OP_ERROR(filename, "fopen");
 }
 
-void close_log_file(void)
+void close_log_file(LogInstance instance)
 {
-       if (log_fp) {
-               fclose(log_fp);
-               log_fp = NULL;
+       if (log_fp[instance]) {
+               fclose(log_fp[instance]);
+               log_fp[instance] = NULL;
        }
 }
 
-void log_print(const gchar *format, ...)
+const char *get_log_hook(LogInstance instance)
+{
+       return log_instances[instance].hook;
+}
+
+void set_log_title(LogInstance instance, gchar *title)
+{
+       log_instances[instance].title = title;
+}
+
+gchar *get_log_title(LogInstance instance)
+{
+       return log_instances[instance].title;
+}
+
+void set_log_prefs(LogInstance instance, int* logwin_width, int* logwin_height)
+{
+       log_instances[instance].prefs_logwin_width = logwin_width;
+       log_instances[instance].prefs_logwin_height = logwin_height;
+}
+
+void get_log_prefs(LogInstance instance, int** logwin_width, int** logwin_height)
+{
+       if (logwin_width)
+               *logwin_width = log_instances[instance].prefs_logwin_width;
+       if (logwin_height)
+               *logwin_height = log_instances[instance].prefs_logwin_height;
+}
+
+void log_print(LogInstance instance, const gchar *format, ...)
 {
        va_list args;
        gchar buf[BUFFSIZE + LOG_TIME_LEN];
        time_t t;
        LogText *logtext = g_new0(LogText, 1);
-       
+       struct tm buft;
+
        time(&t);
-       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
+       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
 
        va_start(args, format);
        g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
@@ -85,97 +142,200 @@ void log_print(const gchar *format, ...)
 
        if (debug_get_mode()) fputs(buf, stdout);
 
+       logtext->instance = instance;
        logtext->text = g_strdup(buf);
        logtext->type = LOG_NORMAL;
        
        g_timeout_add(0, invoke_hook_cb, logtext);
-       
-       if (log_fp) {
-               fputs(buf, log_fp);
-               fflush(log_fp);
+
+       if (log_fp[instance] && prefs_common_enable_log_standard()) {
+               fputs(buf, log_fp[instance]);
+               fflush(log_fp[instance]);
        }
 }
 
-void log_message(const gchar *format, ...)
+void log_message(LogInstance instance, const gchar *format, ...)
 {
        va_list args;
        gchar buf[BUFFSIZE + LOG_TIME_LEN];
        time_t t;
        LogText *logtext = g_new0(LogText, 1);
+       struct tm buft;
 
        time(&t);
-       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
+       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
 
        va_start(args, format);
        g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
        va_end(args);
 
        if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
+
+       logtext->instance = instance;
        logtext->text = g_strdup(buf + LOG_TIME_LEN);
        logtext->type = LOG_MSG;
        
        g_timeout_add(0, invoke_hook_cb, logtext);
 
-       if (log_fp) {
-               fwrite(buf, 1, LOG_TIME_LEN, log_fp);
-               fputs("* message: ", log_fp);
-               fputs(buf + LOG_TIME_LEN, log_fp);
-               fflush(log_fp);
+       if (log_fp[instance] && prefs_common_enable_log_standard()) {
+               fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
+               fputs("* message: ", log_fp[instance]);
+               fputs(buf + LOG_TIME_LEN, log_fp[instance]);
+               fflush(log_fp[instance]);
        }
 }
 
-void log_warning(const gchar *format, ...)
+void log_warning(LogInstance instance, const gchar *format, ...)
 {
        va_list args;
        gchar buf[BUFFSIZE + LOG_TIME_LEN];
        time_t t;
        LogText *logtext = g_new0(LogText, 1);
+       struct tm buft;
 
        time(&t);
-       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
+       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
 
        va_start(args, format);
        g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
        va_end(args);
 
        g_warning("%s", buf);
+
+       logtext->instance = instance;
        logtext->text = g_strdup(buf + LOG_TIME_LEN);
        logtext->type = LOG_WARN;
        
        g_timeout_add(0, invoke_hook_cb, logtext);
 
-       if (log_fp) {
-               fwrite(buf, 1, LOG_TIME_LEN, log_fp);
-               fputs("** warning: ", log_fp);
-               fputs(buf + LOG_TIME_LEN, log_fp);
-               fflush(log_fp);
+       if (log_fp[instance] && prefs_common_enable_log_warning()) {
+               fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
+               fputs("** warning: ", log_fp[instance]);
+               fputs(buf + LOG_TIME_LEN, log_fp[instance]);
+               fflush(log_fp[instance]);
        }
 }
 
-void log_error(const gchar *format, ...)
+void log_error(LogInstance instance, const gchar *format, ...)
 {
        va_list args;
        gchar buf[BUFFSIZE + LOG_TIME_LEN];
        time_t t;
        LogText *logtext = g_new0(LogText, 1);
+       struct tm buft;
 
        time(&t);
-       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
+       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
 
        va_start(args, format);
        g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
        va_end(args);
 
        g_warning("%s", buf);
+
+       logtext->instance = instance;
        logtext->text = g_strdup(buf + LOG_TIME_LEN);
        logtext->type = LOG_ERROR;
        
        g_timeout_add(0, invoke_hook_cb, logtext);
 
-       if (log_fp) {
-               fwrite(buf, 1, LOG_TIME_LEN, log_fp);
-               fputs("*** error: ", log_fp);
-               fputs(buf + LOG_TIME_LEN, log_fp);
-               fflush(log_fp);
+       if (log_fp[instance] && prefs_common_enable_log_error()) {
+               fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
+               fputs("*** error: ", log_fp[instance]);
+               fputs(buf + LOG_TIME_LEN, log_fp[instance]);
+               fflush(log_fp[instance]);
+       }
+}
+
+void log_status_ok(LogInstance instance, const gchar *format, ...)
+{
+       va_list args;
+       gchar buf[BUFFSIZE + LOG_TIME_LEN];
+       time_t t;
+       LogText *logtext = g_new0(LogText, 1);
+       struct tm buft;
+
+       time(&t);
+       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
+
+       va_start(args, format);
+       g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
+       va_end(args);
+
+       if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
+
+       logtext->instance = instance;
+       logtext->text = g_strdup(buf + LOG_TIME_LEN);
+       logtext->type = LOG_STATUS_OK;
+       
+       g_timeout_add(0, invoke_hook_cb, logtext);
+
+       if (log_fp[instance] && prefs_common_enable_log_status()) {
+               fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
+               fputs("* OK: ", log_fp[instance]);
+               fputs(buf + LOG_TIME_LEN, log_fp[instance]);
+               fflush(log_fp[instance]);
+       }
+}
+
+void log_status_nok(LogInstance instance, const gchar *format, ...)
+{
+       va_list args;
+       gchar buf[BUFFSIZE + LOG_TIME_LEN];
+       time_t t;
+       LogText *logtext = g_new0(LogText, 1);
+       struct tm buft;
+
+       time(&t);
+       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
+
+       va_start(args, format);
+       g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
+       va_end(args);
+
+       if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
+
+       logtext->instance = instance;
+       logtext->text = g_strdup(buf + LOG_TIME_LEN);
+       logtext->type = LOG_STATUS_NOK;
+       
+       g_timeout_add(0, invoke_hook_cb, logtext);
+
+       if (log_fp[instance] && prefs_common_enable_log_status()) {
+               fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
+               fputs("* NOT OK: ", log_fp[instance]);
+               fputs(buf + LOG_TIME_LEN, log_fp[instance]);
+               fflush(log_fp[instance]);
+       }
+}
+
+void log_status_skip(LogInstance instance, const gchar *format, ...)
+{
+       va_list args;
+       gchar buf[BUFFSIZE + LOG_TIME_LEN];
+       time_t t;
+       LogText *logtext = g_new0(LogText, 1);
+       struct tm buft;
+
+       time(&t);
+       strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
+
+       va_start(args, format);
+       g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
+       va_end(args);
+
+       if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
+
+       logtext->instance = instance;
+       logtext->text = g_strdup(buf + LOG_TIME_LEN);
+       logtext->type = LOG_STATUS_SKIP;
+       
+       g_timeout_add(0, invoke_hook_cb, logtext);
+
+       if (log_fp[instance] && prefs_common_enable_log_status()) {
+               fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
+               fputs("* SKIPPED: ", log_fp[instance]);
+               fputs(buf + LOG_TIME_LEN, log_fp[instance]);
+               fflush(log_fp[instance]);
        }
 }