diff --git a/po/POTFILES.in b/po/POTFILES.in index fd94eef1c..9b55f49d2 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -40,6 +40,7 @@ ./src/gui/gui-buffer.c ./src/gui/gui-common.c ./src/gui/gui-keyboard.c +./src/gui/gui-log.c ./src/gui/gui-panel.c ./src/gui/gui-window.c ./src/gui/gui.h diff --git a/src/common/command.c b/src/common/command.c index 7fe8207ee..101cb26a2 100644 --- a/src/common/command.c +++ b/src/common/command.c @@ -30,11 +30,12 @@ #include "weechat.h" #include "command.h" +#include "fifo.h" +#include "log.h" +#include "session.h" +#include "utf8.h" #include "weelist.h" #include "weeconfig.h" -#include "session.h" -#include "fifo.h" -#include "utf8.h" #include "../irc/irc.h" #include "../gui/gui.h" @@ -3872,8 +3873,7 @@ weechat_cmd_upgrade (t_irc_server *server, t_irc_channel *channel, (void) config_write (NULL); gui_main_end (); fifo_remove (); - if (weechat_log_file) - fclose (weechat_log_file); + weechat_log_close (); execvp (exec_args[0], exec_args); diff --git a/src/common/fifo.c b/src/common/fifo.c index 78beee0aa..b6f8c6698 100644 --- a/src/common/fifo.c +++ b/src/common/fifo.c @@ -34,6 +34,7 @@ #include "weechat.h" #include "fifo.h" #include "command.h" +#include "log.h" #include "weeconfig.h" #include "../gui/gui.h" diff --git a/src/common/log.c b/src/common/log.c index 292fc1387..3403c7d87 100644 --- a/src/common/log.c +++ b/src/common/log.c @@ -17,7 +17,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -/* log.c: log buffers to files */ +/* log.c: WeeChat log file */ #ifdef HAVE_CONFIG_H @@ -25,164 +25,115 @@ #endif #include -#include #include +#include +#include #include "weechat.h" #include "log.h" -#include "weeconfig.h" -#include "../gui/gui.h" + + +FILE *weechat_log_file = NULL; /* WeeChat log file (~/.weechat/weechat.log) */ /* - * log_write_date: writes date to log file + * weechat_log_init: initialize log file */ void -log_write_date (t_gui_buffer *buffer) +weechat_log_init () { - static char buf_time[256]; + int filename_length; + char *filename; + + filename_length = strlen (weechat_home) + 64; + filename = + (char *) malloc (filename_length * sizeof (char)); + snprintf (filename, filename_length, "%s/%s", weechat_home, WEECHAT_LOG_NAME); + + weechat_log_file = fopen (filename, "wt"); + if (!weechat_log_file + || (flock (fileno (weechat_log_file), LOCK_EX | LOCK_NB) != 0)) + { + fprintf (stderr, + _("%s unable to create/append to log file (%s/%s)\n" + "If another WeeChat process is using this file, try to run WeeChat\n" + "with another home using \"--dir\" command line option.\n"), + WEECHAT_ERROR, weechat_home, WEECHAT_LOG_NAME); + exit (1); + } + free (filename); +} + +/* + * weechat_log_printf: write a message in WeeChat log (/weechat.log) + */ + +void +weechat_log_printf (char *message, ...) +{ + static char buffer[4096]; + char *ptr_buffer; + va_list argptr; static time_t seconds; struct tm *date_tmp; - if (buffer->log_file) - { - seconds = time (NULL); - date_tmp = localtime (&seconds); - if (date_tmp) - { - strftime (buf_time, sizeof (buf_time) - 1, cfg_log_timestamp, date_tmp); - fprintf (buffer->log_file, "%s ", buf_time); - fflush (buffer->log_file); - } - } -} - -/* - * log_write_line: writes a line to log file - */ - -void -log_write_line (t_gui_buffer *buffer, char *message) -{ - char *msg_no_color; - - if (buffer->log_file) - { - msg_no_color = (char *)gui_color_decode ((unsigned char *)message, 0); - fprintf (buffer->log_file, "%s\n", - (msg_no_color) ? msg_no_color : message); - fflush (buffer->log_file); - if (msg_no_color) - free (msg_no_color); - } -} - -/* - * log_write: writes a message to log file - */ - -void -log_write (t_gui_buffer *buffer, char *message) -{ - char *msg_no_color; - - if (buffer->log_file) - { - msg_no_color = (char *)gui_color_decode ((unsigned char *)message, 0); - fprintf (buffer->log_file, "%s", - (msg_no_color) ? msg_no_color : message); - fflush (buffer->log_file); - if (msg_no_color) - free (msg_no_color); - } -} - -/* - * log_start: starts a log - */ - -void -log_start (t_gui_buffer *buffer) -{ - int length; - char *log_path, *log_path2; - - log_path = weechat_strreplace (cfg_log_path, "~", getenv ("HOME")); - log_path2 = weechat_strreplace (log_path, "%h", weechat_home); - if (!log_path || !log_path2) - { - weechat_log_printf (_("Not enough memory to write log file for a buffer\n")); - if (log_path) - free (log_path); - if (log_path2) - free (log_path2); + if (!weechat_log_file) return; - } - length = strlen (log_path2) + 64; - if (SERVER(buffer)) - length += strlen (SERVER(buffer)->name); - if (CHANNEL(buffer)) - length += strlen (CHANNEL(buffer)->name); - buffer->log_filename = (char *) malloc (length); - if (!buffer->log_filename) + va_start (argptr, message); + vsnprintf (buffer, sizeof (buffer) - 1, message, argptr); + va_end (argptr); + + /* keep only valid chars */ + ptr_buffer = buffer; + while (ptr_buffer[0]) { - weechat_log_printf (_("Not enough memory to write log file for a buffer\n")); - if (log_path) - free (log_path); - if (log_path2) - free (log_path2); - return; + if ((ptr_buffer[0] != '\n') + && (ptr_buffer[0] != '\r') + && ((unsigned char)(ptr_buffer[0]) < 32)) + ptr_buffer[0] = '.'; + ptr_buffer++; } - strcpy (buffer->log_filename, log_path2); - if (log_path) - free (log_path); - if (log_path2) - free (log_path2); - if (buffer->log_filename[strlen (buffer->log_filename) - 1] != DIR_SEPARATOR_CHAR) - strcat (buffer->log_filename, DIR_SEPARATOR); - - if (SERVER(buffer)) - { - strcat (buffer->log_filename, SERVER(buffer)->name); - strcat (buffer->log_filename, "."); - } - if (CHANNEL(buffer)) - { - strcat (buffer->log_filename, CHANNEL(buffer)->name); - strcat (buffer->log_filename, "."); - } - strcat (buffer->log_filename, "weechatlog"); - - buffer->log_file = fopen (buffer->log_filename, "a"); - if (!buffer->log_file) - { - weechat_log_printf (_("Unable to write log file for a buffer\n")); - free (buffer->log_filename); - return; - } - log_write (buffer, _("**** Beginning of log ")); - log_write_date (buffer); - log_write (buffer, "****\n"); + seconds = time (NULL); + date_tmp = localtime (&seconds); + if (date_tmp) + fprintf (weechat_log_file, "[%04d-%02d-%02d %02d:%02d:%02d] %s", + date_tmp->tm_year + 1900, date_tmp->tm_mon + 1, date_tmp->tm_mday, + date_tmp->tm_hour, date_tmp->tm_min, date_tmp->tm_sec, + buffer); + else + fprintf (weechat_log_file, "%s", buffer); + fflush (weechat_log_file); } /* - * log_end: ends a log + * weechat_log_close: close log file */ void -log_end (t_gui_buffer *buffer) +weechat_log_close () { - if (buffer->log_file) + if (weechat_log_file) + { + flock (fileno (weechat_log_file), LOCK_UN); + fclose (weechat_log_file); + } +} + +/* + * weechat_log_crash_rename: rename log file when crashing + */ + +void +weechat_log_crash_rename () +{ + char *oldname, *newname; + + oldname = (char *) malloc (strlen (weechat_home) + 64); + newname = (char *) malloc (strlen (weechat_home) + 64); + if (oldname && newname) { - log_write (buffer, _("**** End of log ")); - log_write_date (buffer); - log_write (buffer, "****\n"); - fclose (buffer->log_file); - buffer->log_file = NULL; } - if (buffer->log_filename) - free (buffer->log_filename); } diff --git a/src/common/log.h b/src/common/log.h index 76741f48d..53095dbfd 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -21,13 +21,11 @@ #ifndef __WEECHAT_LOG_H #define __WEECHAT_LOG_H 1 -#include "../irc/irc.h" -#include "../gui/gui.h" +extern FILE *weechat_log_file; -extern void log_write_date (t_gui_buffer *); -extern void log_write_line (t_gui_buffer *, char *); -extern void log_write (t_gui_buffer *, char *); -extern void log_start (t_gui_buffer *); -extern void log_end (t_gui_buffer *); +extern void weechat_log_init (); +extern void weechat_log_close (); +extern void weechat_log_printf (char *, ...); +extern void weechat_log_crash_rename (); #endif /* log.h */ diff --git a/src/common/session.c b/src/common/session.c index 3f24dc58a..2867c8c48 100644 --- a/src/common/session.c +++ b/src/common/session.c @@ -36,6 +36,7 @@ #include "weechat.h" #include "session.h" +#include "log.h" #include "../irc/irc.h" #include "../gui/gui.h" diff --git a/src/common/weechat.c b/src/common/weechat.c index f82a622e7..9cedef9d1 100644 --- a/src/common/weechat.c +++ b/src/common/weechat.c @@ -42,9 +42,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -63,11 +61,12 @@ #include "weechat.h" #include "backtrace.h" -#include "weeconfig.h" #include "command.h" #include "fifo.h" -#include "utf8.h" +#include "log.h" #include "session.h" +#include "utf8.h" +#include "weeconfig.h" #include "../irc/irc.h" #include "../gui/gui.h" @@ -76,19 +75,18 @@ #endif -char *weechat_argv0 = NULL; /* WeeChat binary file name (argv[0]) */ -char *weechat_session = NULL; /* WeeChat session file (for /upgrade command) */ -time_t weechat_start_time; /* WeeChat start time (used by /uptime command) */ -int quit_weechat; /* = 1 if quit request from user... why ? :'( */ -int sigsegv = 0; /* SIGSEGV received? */ -char *weechat_home = NULL; /* WeeChat home dir. (example: /home/toto/.weechat) */ -FILE *weechat_log_file = NULL; /* WeeChat log file (~/.weechat/weechat.log) */ +char *weechat_argv0 = NULL; /* WeeChat binary file name (argv[0]) */ +char *weechat_session = NULL; /* WeeChat session file (for /upgrade cmd) */ +time_t weechat_start_time; /* WeeChat start time (used by /uptime cmd) */ +int quit_weechat; /* = 1 if quit request from user... why ? :'( */ +int sigsegv = 0; /* SIGSEGV received? */ +char *weechat_home = NULL; /* WeeChat home dir. (default: ~/.weechat) */ -char *local_charset = NULL; /* local charset, for example: ISO-8859-1, UTF-8 */ +char *local_charset = NULL; /* local charset, for ex.: ISO-8859-1, UTF-8 */ -int server_cmd_line; /* at least one server on WeeChat command line */ -int auto_connect; /* enabled by default, can by disabled on cmd line */ -int auto_load_plugins; /* enabled by default, can by disabled on cmd line */ +int server_cmd_line; /* at least 1 server on WeeChat command line */ +int auto_connect; /* enabled by default (cmd option to disable) */ +int auto_load_plugins; /* enabled by default (cmd option to disable) */ #ifdef HAVE_GNUTLS gnutls_certificate_credentials gnutls_xcred; /* gnutls client credentials */ @@ -197,49 +195,6 @@ ascii_strncasecmp (char *string1, char *string2, int max) return (string1[0]) ? 1 : ((string2[0]) ? -1 : 0); } -/* - * weechat_log_printf: displays a message in WeeChat log (/weechat.log) - */ - -void -weechat_log_printf (char *message, ...) -{ - static char buffer[4096]; - char *ptr_buffer; - va_list argptr; - static time_t seconds; - struct tm *date_tmp; - - if (!weechat_log_file) - return; - - va_start (argptr, message); - vsnprintf (buffer, sizeof (buffer) - 1, message, argptr); - va_end (argptr); - - /* keep only valid chars */ - ptr_buffer = buffer; - while (ptr_buffer[0]) - { - if ((ptr_buffer[0] != '\n') - && (ptr_buffer[0] != '\r') - && ((unsigned char)(ptr_buffer[0]) < 32)) - ptr_buffer[0] = '.'; - ptr_buffer++; - } - - seconds = time (NULL); - date_tmp = localtime (&seconds); - if (date_tmp) - fprintf (weechat_log_file, "[%04d-%02d-%02d %02d:%02d:%02d] %s", - date_tmp->tm_year + 1900, date_tmp->tm_mon + 1, date_tmp->tm_mday, - date_tmp->tm_hour, date_tmp->tm_min, date_tmp->tm_sec, - buffer); - else - fprintf (weechat_log_file, "%s", buffer); - fflush (weechat_log_file); -} - /* * weechat_iconv: convert string to another charset */ @@ -834,35 +789,6 @@ weechat_init_vars () #endif } -/* - * weechat_init_log: initialize log file - */ - -void -weechat_init_log () -{ - int filename_length; - char *filename; - - filename_length = strlen (weechat_home) + 64; - filename = - (char *) malloc (filename_length * sizeof (char)); - snprintf (filename, filename_length, "%s/" WEECHAT_LOG_NAME, weechat_home); - - weechat_log_file = fopen (filename, "wt"); - if (!weechat_log_file - || (flock (fileno (weechat_log_file), LOCK_EX | LOCK_NB) != 0)) - { - fprintf (stderr, - _("%s unable to create/append to log file (%s/%s)\n" - "If another WeeChat process is using this file, try to run WeeChat\n" - "with another home using \"--dir\" command line option.\n"), - WEECHAT_ERROR, weechat_home, WEECHAT_LOG_NAME); - exit (1); - } - free (filename); -} - /* * weechat_config_read: read WeeChat config file */ @@ -948,11 +874,7 @@ weechat_shutdown (int return_code, int crash) fifo_remove (); if (weechat_home) free (weechat_home); - if (weechat_log_file) - { - flock (fileno (weechat_log_file), LOCK_UN); - fclose (weechat_log_file); - } + weechat_log_close (); if (local_charset) free (local_charset); alias_free_all (); @@ -1085,9 +1007,10 @@ weechat_sigsegv () weechat_home); fprintf (stderr, "***\n"); fprintf (stderr, "*** Please help WeeChat developers to fix this bug:\n"); - fprintf (stderr, "*** 1. if you have a core file, please run: gdb weechat-curses core\n"); + fprintf (stderr, "*** 1. If you have a core file, please run: gdb weechat-curses core\n"); fprintf (stderr, "*** then issue \"bt\" command and send result to developers\n"); - fprintf (stderr, "*** 2. otherwise send backtrace displayed below and weechat.log\n"); + fprintf (stderr, "*** To enable core files with bash shell: ulimit -c 10000\n"); + fprintf (stderr, "*** 2. Otherwise send backtrace displayed below and weechat.log\n"); fprintf (stderr, "*** (be careful, private info may be in this file since\n"); fprintf (stderr, "*** part of chats are displayed, so remove lines if needed)\n\n"); @@ -1125,7 +1048,7 @@ main (int argc, char *argv[]) gui_keyboard_init (); /* init keyb. (default key bindings)*/ weechat_parse_args (argc, argv); /* parse command line args */ weechat_create_home_dirs (); /* create WeeChat directories */ - weechat_init_log (); /* init log file */ + weechat_log_init (); /* init log file */ command_index_build (); /* build cmd index for completion */ weechat_config_read (); /* read configuration */ weechat_create_config_dirs (); /* create config directories */ diff --git a/src/common/weechat.h b/src/common/weechat.h index 6206cee37..5c08702e4 100644 --- a/src/common/weechat.h +++ b/src/common/weechat.h @@ -98,7 +98,6 @@ extern char *weechat_argv0; extern time_t weechat_start_time; extern int quit_weechat; extern char *weechat_home; -extern FILE *weechat_log_file; extern char *local_charset; #ifdef HAVE_GNUTLS @@ -109,7 +108,6 @@ extern void ascii_tolower (char *); extern void ascii_toupper (char *); extern int ascii_strcasecmp (char *, char *); extern int ascii_strncasecmp (char *, char *, int); -extern void weechat_log_printf (char *, ...); extern char *weechat_iconv (char *, char *, char *); extern char *weechat_strreplace (char *, char *, char *); extern void weechat_dump (int); diff --git a/src/common/weeconfig.c b/src/common/weeconfig.c index 606596c0c..55da0944a 100644 --- a/src/common/weeconfig.c +++ b/src/common/weeconfig.c @@ -1282,23 +1282,23 @@ config_change_log () if (BUFFER_IS_SERVER(ptr_buffer)) { if (cfg_log_auto_server && !ptr_buffer->log_file) - log_start (ptr_buffer); + gui_log_start (ptr_buffer); else if (!cfg_log_auto_server && ptr_buffer->log_file) - log_end (ptr_buffer); + gui_log_end (ptr_buffer); } if (BUFFER_IS_CHANNEL(ptr_buffer)) { if (cfg_log_auto_channel && !ptr_buffer->log_file) - log_start (ptr_buffer); + gui_log_start (ptr_buffer); else if (!cfg_log_auto_channel && ptr_buffer->log_file) - log_end (ptr_buffer); + gui_log_end (ptr_buffer); } if (BUFFER_IS_PRIVATE(ptr_buffer)) { if (cfg_log_auto_private && !ptr_buffer->log_file) - log_start (ptr_buffer); + gui_log_start (ptr_buffer); else if (!cfg_log_auto_private && ptr_buffer->log_file) - log_end (ptr_buffer); + gui_log_end (ptr_buffer); } } } diff --git a/src/gui/Makefile.am b/src/gui/Makefile.am index 6a1ea4fc1..12c3da33d 100644 --- a/src/gui/Makefile.am +++ b/src/gui/Makefile.am @@ -23,6 +23,7 @@ lib_weechat_gui_common_a_SOURCES = gui-buffer.c \ gui-common.c \ gui-action.c \ gui-keyboard.c \ + gui-log.c \ gui-window.c \ gui-panel.c \ gui.h \ diff --git a/src/gui/curses/Makefile.am b/src/gui/curses/Makefile.am index 283d89bfe..2dd1d2678 100644 --- a/src/gui/curses/Makefile.am +++ b/src/gui/curses/Makefile.am @@ -20,9 +20,7 @@ INCLUDES = -DLOCALEDIR=\"$(datadir)/locale\" bin_PROGRAMS = weechat-curses if PLUGINS -weechat_curses_LDADD = ../gui-common.o ../gui-buffer.o \ - ../gui-window.o ../gui-panel.o \ - ../gui-keyboard.o ../gui-action.o \ +weechat_curses_LDADD = ../lib_weechat_gui_common.a \ ../../common/lib_weechat_main.a \ ../../irc/lib_weechat_irc.a \ ../../plugins/lib_weechat_plugins.a \ @@ -30,9 +28,7 @@ weechat_curses_LDADD = ../gui-common.o ../gui-buffer.o \ $(NCURSES_LIBS) \ $(GNUTLS_LFLAGS) else -weechat_curses_LDADD = ../gui-common.o ../gui-buffer.o \ - ../gui-window.o ../gui-panel.o \ - ../gui-keyboard.o ../gui-action.o \ +weechat_curses_LDADD = ../lib_weechat_gui_common.a \ ../../common/lib_weechat_main.a \ ../../irc/lib_weechat_irc.a \ $(PLUGINS_LIBS) \ diff --git a/src/gui/curses/gui-curses-window.c b/src/gui/curses/gui-curses-window.c index 006f41f47..f450a65c0 100644 --- a/src/gui/curses/gui-curses-window.c +++ b/src/gui/curses/gui-curses-window.c @@ -32,6 +32,7 @@ #include "../../common/weechat.h" #include "../gui.h" #include "../../common/hotlist.h" +#include "../../common/log.h" #include "../../common/weeconfig.h" #include "gui-curses.h" diff --git a/src/gui/gtk/Makefile.am b/src/gui/gtk/Makefile.am index d4e09550c..a20bc721b 100644 --- a/src/gui/gtk/Makefile.am +++ b/src/gui/gtk/Makefile.am @@ -20,9 +20,7 @@ INCLUDES = -DLOCALEDIR=\"$(datadir)/locale\" $(GTK_CFLAGS) bin_PROGRAMS = weechat-gtk if PLUGINS -weechat_gtk_LDADD = ../gui-common.o ../gui-buffer.o \ - ../gui-window.o ../gui-panel.o \ - ../gui-keyboard.o ../gui-action.o \ +weechat_gtk_LDADD = ../lib_weechat_gui_common.a \ ../../common/lib_weechat_main.a \ ../../irc/lib_weechat_irc.a \ ../../plugins/lib_weechat_plugins.a \ @@ -30,9 +28,7 @@ weechat_gtk_LDADD = ../gui-common.o ../gui-buffer.o \ $(GTK_LIBS) \ $(GNUTLS_LFLAGS) else -weechat_gtk_LDADD = ../gui-common.o ../gui-buffer.o \ - ../gui-window.o ../gui-panel.o \ - ../gui-keyboard.o ../gui-action.o \ +weechat_gtk_LDADD = ../lib_weechat_gui_common.a \ ../../common/lib_weechat_main.a \ ../../irc/lib_weechat_irc.a \ $(PLUGINS_LIBS) \ diff --git a/src/gui/gtk/gui-gtk-window.c b/src/gui/gtk/gui-gtk-window.c index 427b2627e..7107cd660 100644 --- a/src/gui/gtk/gui-gtk-window.c +++ b/src/gui/gtk/gui-gtk-window.c @@ -30,6 +30,7 @@ #include "../../common/weechat.h" #include "../gui.h" #include "../../common/hotlist.h" +#include "../../common/log.h" #include "../../common/weeconfig.h" #include "gui-gtk.h" diff --git a/src/gui/gui-buffer.c b/src/gui/gui-buffer.c index a3fdefd30..a57299af3 100644 --- a/src/gui/gui-buffer.c +++ b/src/gui/gui-buffer.c @@ -98,7 +98,7 @@ gui_buffer_new (t_gui_window *window, void *server, void *channel, int type, if (cfg_look_one_server_buffer && server && !channel) gui_buffers->all_servers = 1; if (cfg_log_auto_server) - log_start (gui_buffers); + gui_log_start (gui_buffers); return gui_buffers; } @@ -165,7 +165,7 @@ gui_buffer_new (t_gui_window *window, void *server, void *channel, int type, if ((cfg_log_auto_server && BUFFER_IS_SERVER(new_buffer)) || (cfg_log_auto_channel && BUFFER_IS_CHANNEL(new_buffer)) || (cfg_log_auto_private && BUFFER_IS_PRIVATE(new_buffer))) - log_start (new_buffer); + gui_log_start (new_buffer); /* init input buffer */ new_buffer->has_input = (new_buffer->type == BUFFER_TYPE_STANDARD) ? 1 : 0; @@ -479,7 +479,7 @@ gui_buffer_free (t_gui_buffer *buffer, int switch_to_another) /* close log if opened */ if (buffer->log_file) - log_end (buffer); + gui_log_end (buffer); if (buffer->input_buffer) free (buffer->input_buffer); diff --git a/src/gui/gui-common.c b/src/gui/gui-common.c index 9d63e7d4b..9dc09d5af 100644 --- a/src/gui/gui-common.c +++ b/src/gui/gui-common.c @@ -193,18 +193,18 @@ gui_add_to_line (t_gui_buffer *buffer, int type, char *nick, char *message) } if (buffer->line_complete && buffer->log_file && buffer->last_line->log_write) { - log_write_date (buffer); + gui_log_write_date (buffer); if (buffer->last_line->nick) { - log_write (buffer, "<"); - log_write (buffer, buffer->last_line->nick); - log_write (buffer, "> "); + gui_log_write (buffer, "<"); + gui_log_write (buffer, buffer->last_line->nick); + gui_log_write (buffer, "> "); } if (buffer->last_line->ofs_start_message >= 0) - log_write_line (buffer, - buffer->last_line->data + buffer->last_line->ofs_start_message); + gui_log_write_line (buffer, + buffer->last_line->data + buffer->last_line->ofs_start_message); else - log_write_line (buffer, buffer->last_line->data); + gui_log_write_line (buffer, buffer->last_line->data); } } diff --git a/src/gui/gui-keyboard.c b/src/gui/gui-keyboard.c index 253c997b4..5325e8a1f 100644 --- a/src/gui/gui-keyboard.c +++ b/src/gui/gui-keyboard.c @@ -31,6 +31,7 @@ #include "../common/weechat.h" #include "gui.h" #include "../common/command.h" +#include "../common/log.h" #ifdef PLUGINS #include "../plugins/plugins.h" diff --git a/src/gui/gui-log.c b/src/gui/gui-log.c new file mode 100644 index 000000000..086528c30 --- /dev/null +++ b/src/gui/gui-log.c @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2003-2006 by FlashCode + * See README for License detail, AUTHORS for developers list. + * + * 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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* gui-log.c: log buffers to files */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "../common/weechat.h" +#include "gui.h" +#include "../common/log.h" +#include "../common/weeconfig.h" + + +/* + * gui_log_write_date: writes date to log file + */ + +void +gui_log_write_date (t_gui_buffer *buffer) +{ + static char buf_time[256]; + static time_t seconds; + struct tm *date_tmp; + + if (buffer->log_file) + { + seconds = time (NULL); + date_tmp = localtime (&seconds); + if (date_tmp) + { + strftime (buf_time, sizeof (buf_time) - 1, cfg_log_timestamp, date_tmp); + fprintf (buffer->log_file, "%s ", buf_time); + fflush (buffer->log_file); + } + } +} + +/* + * gui_log_write_line: writes a line to log file + */ + +void +gui_log_write_line (t_gui_buffer *buffer, char *message) +{ + char *msg_no_color; + + if (buffer->log_file) + { + msg_no_color = (char *)gui_color_decode ((unsigned char *)message, 0); + fprintf (buffer->log_file, "%s\n", + (msg_no_color) ? msg_no_color : message); + fflush (buffer->log_file); + if (msg_no_color) + free (msg_no_color); + } +} + +/* + * gui_log_write: writes a message to log file + */ + +void +gui_log_write (t_gui_buffer *buffer, char *message) +{ + char *msg_no_color; + + if (buffer->log_file) + { + msg_no_color = (char *)gui_color_decode ((unsigned char *)message, 0); + fprintf (buffer->log_file, "%s", + (msg_no_color) ? msg_no_color : message); + fflush (buffer->log_file); + if (msg_no_color) + free (msg_no_color); + } +} + +/* + * gui_log_start: starts a log + */ + +void +gui_log_start (t_gui_buffer *buffer) +{ + int length; + char *log_path, *log_path2; + + log_path = weechat_strreplace (cfg_log_path, "~", getenv ("HOME")); + log_path2 = weechat_strreplace (log_path, "%h", weechat_home); + if (!log_path || !log_path2) + { + weechat_log_printf (_("Not enough memory to write log file for a buffer\n")); + if (log_path) + free (log_path); + if (log_path2) + free (log_path2); + return; + } + length = strlen (log_path2) + 64; + if (SERVER(buffer)) + length += strlen (SERVER(buffer)->name); + if (CHANNEL(buffer)) + length += strlen (CHANNEL(buffer)->name); + + buffer->log_filename = (char *) malloc (length); + if (!buffer->log_filename) + { + weechat_log_printf (_("Not enough memory to write log file for a buffer\n")); + if (log_path) + free (log_path); + if (log_path2) + free (log_path2); + return; + } + + strcpy (buffer->log_filename, log_path2); + if (log_path) + free (log_path); + if (log_path2) + free (log_path2); + if (buffer->log_filename[strlen (buffer->log_filename) - 1] != DIR_SEPARATOR_CHAR) + strcat (buffer->log_filename, DIR_SEPARATOR); + + if (SERVER(buffer)) + { + strcat (buffer->log_filename, SERVER(buffer)->name); + strcat (buffer->log_filename, "."); + } + if (CHANNEL(buffer)) + { + strcat (buffer->log_filename, CHANNEL(buffer)->name); + strcat (buffer->log_filename, "."); + } + strcat (buffer->log_filename, "weechatlog"); + + buffer->log_file = fopen (buffer->log_filename, "a"); + if (!buffer->log_file) + { + weechat_log_printf (_("Unable to write log file for a buffer\n")); + free (buffer->log_filename); + return; + } + gui_log_write (buffer, _("**** Beginning of log ")); + gui_log_write_date (buffer); + gui_log_write (buffer, "****\n"); +} + +/* + * gui_log_end: ends a log + */ + +void +gui_log_end (t_gui_buffer *buffer) +{ + if (buffer->log_file) + { + gui_log_write (buffer, _("**** End of log ")); + gui_log_write_date (buffer); + gui_log_write (buffer, "****\n"); + fclose (buffer->log_file); + buffer->log_file = NULL; + } + if (buffer->log_filename) + free (buffer->log_filename); +} diff --git a/src/gui/gui.h b/src/gui/gui.h index ff3036a65..fa544939d 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -27,6 +27,7 @@ #include "gui-window.h" #include "gui-keyboard.h" + #define gui_printf(buffer, fmt, argz...) \ gui_printf_internal(buffer, 1, MSG_TYPE_INFO, NULL, fmt, ##argz) @@ -42,6 +43,7 @@ #define gui_printf_nolog_notime(buffer, fmt, argz...) \ gui_printf_internal(buffer, 0, MSG_TYPE_NOLOG, NULL, fmt, ##argz) + typedef struct t_gui_infobar t_gui_infobar; struct t_gui_infobar @@ -163,6 +165,13 @@ extern int gui_keyboard_pressed (char *); extern void gui_keyboard_free (t_gui_key *); extern void gui_keyboard_free_all (); +/* log */ +extern void gui_log_write_date (t_gui_buffer *); +extern void gui_log_write_line (t_gui_buffer *, char *); +extern void gui_log_write (t_gui_buffer *, char *); +extern void gui_log_start (t_gui_buffer *); +extern void gui_log_end (t_gui_buffer *); + /* other */ extern void gui_infobar_printf (int, int, char *, ...); extern void gui_infobar_printf_from_buffer (t_gui_buffer *, int, int, char *, char *, ...); diff --git a/src/irc/irc-channel.c b/src/irc/irc-channel.c index 27938510c..e5c6a0d51 100644 --- a/src/irc/irc-channel.c +++ b/src/irc/irc-channel.c @@ -30,6 +30,7 @@ #include "../common/weechat.h" #include "irc.h" +#include "../common/log.h" #include "../common/utf8.h" #include "../common/weeconfig.h" #include "../gui/gui.h" diff --git a/src/irc/irc-dcc.c b/src/irc/irc-dcc.c index 378625b8e..f68274762 100644 --- a/src/irc/irc-dcc.c +++ b/src/irc/irc-dcc.c @@ -39,8 +39,9 @@ #include "../common/weechat.h" #include "irc.h" -#include "../common/weeconfig.h" +#include "../common/log.h" #include "../common/hotlist.h" +#include "../common/weeconfig.h" #include "../gui/gui.h" diff --git a/src/irc/irc-ignore.c b/src/irc/irc-ignore.c index a0b4a4948..cb80bd52b 100644 --- a/src/irc/irc-ignore.c +++ b/src/irc/irc-ignore.c @@ -30,6 +30,7 @@ #include "../common/weechat.h" #include "irc.h" #include "../common/command.h" +#include "../common/log.h" char *ignore_types[] = diff --git a/src/irc/irc-nick.c b/src/irc/irc-nick.c index 61023b398..3e6f231cb 100644 --- a/src/irc/irc-nick.c +++ b/src/irc/irc-nick.c @@ -30,6 +30,7 @@ #include "../common/weechat.h" #include "irc.h" +#include "../common/log.h" #include "../common/weeconfig.h" diff --git a/src/irc/irc-server.c b/src/irc/irc-server.c index 685979c2d..3ab6978fe 100644 --- a/src/irc/irc-server.c +++ b/src/irc/irc-server.c @@ -45,6 +45,7 @@ #include "../common/weechat.h" #include "irc.h" +#include "../common/log.h" #include "../common/weeconfig.h" #include "../gui/gui.h" diff --git a/src/plugins/plugins-interface.c b/src/plugins/plugins-interface.c index 72c2b6a5c..3cd18eba0 100644 --- a/src/plugins/plugins-interface.c +++ b/src/plugins/plugins-interface.c @@ -258,7 +258,7 @@ weechat_plugin_log (t_weechat_plugin *plugin, va_start (argptr, message); vsnprintf (buf, sizeof (buf) - 1, message, argptr); va_end (argptr); - log_write_line (ptr_buffer, buf); + gui_log_write_line (ptr_buffer, buf); } } diff --git a/weechat/po/POTFILES.in b/weechat/po/POTFILES.in index fd94eef1c..9b55f49d2 100644 --- a/weechat/po/POTFILES.in +++ b/weechat/po/POTFILES.in @@ -40,6 +40,7 @@ ./src/gui/gui-buffer.c ./src/gui/gui-common.c ./src/gui/gui-keyboard.c +./src/gui/gui-log.c ./src/gui/gui-panel.c ./src/gui/gui-window.c ./src/gui/gui.h diff --git a/weechat/src/common/command.c b/weechat/src/common/command.c index 7fe8207ee..101cb26a2 100644 --- a/weechat/src/common/command.c +++ b/weechat/src/common/command.c @@ -30,11 +30,12 @@ #include "weechat.h" #include "command.h" +#include "fifo.h" +#include "log.h" +#include "session.h" +#include "utf8.h" #include "weelist.h" #include "weeconfig.h" -#include "session.h" -#include "fifo.h" -#include "utf8.h" #include "../irc/irc.h" #include "../gui/gui.h" @@ -3872,8 +3873,7 @@ weechat_cmd_upgrade (t_irc_server *server, t_irc_channel *channel, (void) config_write (NULL); gui_main_end (); fifo_remove (); - if (weechat_log_file) - fclose (weechat_log_file); + weechat_log_close (); execvp (exec_args[0], exec_args); diff --git a/weechat/src/common/fifo.c b/weechat/src/common/fifo.c index 78beee0aa..b6f8c6698 100644 --- a/weechat/src/common/fifo.c +++ b/weechat/src/common/fifo.c @@ -34,6 +34,7 @@ #include "weechat.h" #include "fifo.h" #include "command.h" +#include "log.h" #include "weeconfig.h" #include "../gui/gui.h" diff --git a/weechat/src/common/log.c b/weechat/src/common/log.c index 292fc1387..3403c7d87 100644 --- a/weechat/src/common/log.c +++ b/weechat/src/common/log.c @@ -17,7 +17,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -/* log.c: log buffers to files */ +/* log.c: WeeChat log file */ #ifdef HAVE_CONFIG_H @@ -25,164 +25,115 @@ #endif #include -#include #include +#include +#include #include "weechat.h" #include "log.h" -#include "weeconfig.h" -#include "../gui/gui.h" + + +FILE *weechat_log_file = NULL; /* WeeChat log file (~/.weechat/weechat.log) */ /* - * log_write_date: writes date to log file + * weechat_log_init: initialize log file */ void -log_write_date (t_gui_buffer *buffer) +weechat_log_init () { - static char buf_time[256]; + int filename_length; + char *filename; + + filename_length = strlen (weechat_home) + 64; + filename = + (char *) malloc (filename_length * sizeof (char)); + snprintf (filename, filename_length, "%s/%s", weechat_home, WEECHAT_LOG_NAME); + + weechat_log_file = fopen (filename, "wt"); + if (!weechat_log_file + || (flock (fileno (weechat_log_file), LOCK_EX | LOCK_NB) != 0)) + { + fprintf (stderr, + _("%s unable to create/append to log file (%s/%s)\n" + "If another WeeChat process is using this file, try to run WeeChat\n" + "with another home using \"--dir\" command line option.\n"), + WEECHAT_ERROR, weechat_home, WEECHAT_LOG_NAME); + exit (1); + } + free (filename); +} + +/* + * weechat_log_printf: write a message in WeeChat log (/weechat.log) + */ + +void +weechat_log_printf (char *message, ...) +{ + static char buffer[4096]; + char *ptr_buffer; + va_list argptr; static time_t seconds; struct tm *date_tmp; - if (buffer->log_file) - { - seconds = time (NULL); - date_tmp = localtime (&seconds); - if (date_tmp) - { - strftime (buf_time, sizeof (buf_time) - 1, cfg_log_timestamp, date_tmp); - fprintf (buffer->log_file, "%s ", buf_time); - fflush (buffer->log_file); - } - } -} - -/* - * log_write_line: writes a line to log file - */ - -void -log_write_line (t_gui_buffer *buffer, char *message) -{ - char *msg_no_color; - - if (buffer->log_file) - { - msg_no_color = (char *)gui_color_decode ((unsigned char *)message, 0); - fprintf (buffer->log_file, "%s\n", - (msg_no_color) ? msg_no_color : message); - fflush (buffer->log_file); - if (msg_no_color) - free (msg_no_color); - } -} - -/* - * log_write: writes a message to log file - */ - -void -log_write (t_gui_buffer *buffer, char *message) -{ - char *msg_no_color; - - if (buffer->log_file) - { - msg_no_color = (char *)gui_color_decode ((unsigned char *)message, 0); - fprintf (buffer->log_file, "%s", - (msg_no_color) ? msg_no_color : message); - fflush (buffer->log_file); - if (msg_no_color) - free (msg_no_color); - } -} - -/* - * log_start: starts a log - */ - -void -log_start (t_gui_buffer *buffer) -{ - int length; - char *log_path, *log_path2; - - log_path = weechat_strreplace (cfg_log_path, "~", getenv ("HOME")); - log_path2 = weechat_strreplace (log_path, "%h", weechat_home); - if (!log_path || !log_path2) - { - weechat_log_printf (_("Not enough memory to write log file for a buffer\n")); - if (log_path) - free (log_path); - if (log_path2) - free (log_path2); + if (!weechat_log_file) return; - } - length = strlen (log_path2) + 64; - if (SERVER(buffer)) - length += strlen (SERVER(buffer)->name); - if (CHANNEL(buffer)) - length += strlen (CHANNEL(buffer)->name); - buffer->log_filename = (char *) malloc (length); - if (!buffer->log_filename) + va_start (argptr, message); + vsnprintf (buffer, sizeof (buffer) - 1, message, argptr); + va_end (argptr); + + /* keep only valid chars */ + ptr_buffer = buffer; + while (ptr_buffer[0]) { - weechat_log_printf (_("Not enough memory to write log file for a buffer\n")); - if (log_path) - free (log_path); - if (log_path2) - free (log_path2); - return; + if ((ptr_buffer[0] != '\n') + && (ptr_buffer[0] != '\r') + && ((unsigned char)(ptr_buffer[0]) < 32)) + ptr_buffer[0] = '.'; + ptr_buffer++; } - strcpy (buffer->log_filename, log_path2); - if (log_path) - free (log_path); - if (log_path2) - free (log_path2); - if (buffer->log_filename[strlen (buffer->log_filename) - 1] != DIR_SEPARATOR_CHAR) - strcat (buffer->log_filename, DIR_SEPARATOR); - - if (SERVER(buffer)) - { - strcat (buffer->log_filename, SERVER(buffer)->name); - strcat (buffer->log_filename, "."); - } - if (CHANNEL(buffer)) - { - strcat (buffer->log_filename, CHANNEL(buffer)->name); - strcat (buffer->log_filename, "."); - } - strcat (buffer->log_filename, "weechatlog"); - - buffer->log_file = fopen (buffer->log_filename, "a"); - if (!buffer->log_file) - { - weechat_log_printf (_("Unable to write log file for a buffer\n")); - free (buffer->log_filename); - return; - } - log_write (buffer, _("**** Beginning of log ")); - log_write_date (buffer); - log_write (buffer, "****\n"); + seconds = time (NULL); + date_tmp = localtime (&seconds); + if (date_tmp) + fprintf (weechat_log_file, "[%04d-%02d-%02d %02d:%02d:%02d] %s", + date_tmp->tm_year + 1900, date_tmp->tm_mon + 1, date_tmp->tm_mday, + date_tmp->tm_hour, date_tmp->tm_min, date_tmp->tm_sec, + buffer); + else + fprintf (weechat_log_file, "%s", buffer); + fflush (weechat_log_file); } /* - * log_end: ends a log + * weechat_log_close: close log file */ void -log_end (t_gui_buffer *buffer) +weechat_log_close () { - if (buffer->log_file) + if (weechat_log_file) + { + flock (fileno (weechat_log_file), LOCK_UN); + fclose (weechat_log_file); + } +} + +/* + * weechat_log_crash_rename: rename log file when crashing + */ + +void +weechat_log_crash_rename () +{ + char *oldname, *newname; + + oldname = (char *) malloc (strlen (weechat_home) + 64); + newname = (char *) malloc (strlen (weechat_home) + 64); + if (oldname && newname) { - log_write (buffer, _("**** End of log ")); - log_write_date (buffer); - log_write (buffer, "****\n"); - fclose (buffer->log_file); - buffer->log_file = NULL; } - if (buffer->log_filename) - free (buffer->log_filename); } diff --git a/weechat/src/common/log.h b/weechat/src/common/log.h index 76741f48d..53095dbfd 100644 --- a/weechat/src/common/log.h +++ b/weechat/src/common/log.h @@ -21,13 +21,11 @@ #ifndef __WEECHAT_LOG_H #define __WEECHAT_LOG_H 1 -#include "../irc/irc.h" -#include "../gui/gui.h" +extern FILE *weechat_log_file; -extern void log_write_date (t_gui_buffer *); -extern void log_write_line (t_gui_buffer *, char *); -extern void log_write (t_gui_buffer *, char *); -extern void log_start (t_gui_buffer *); -extern void log_end (t_gui_buffer *); +extern void weechat_log_init (); +extern void weechat_log_close (); +extern void weechat_log_printf (char *, ...); +extern void weechat_log_crash_rename (); #endif /* log.h */ diff --git a/weechat/src/common/session.c b/weechat/src/common/session.c index 3f24dc58a..2867c8c48 100644 --- a/weechat/src/common/session.c +++ b/weechat/src/common/session.c @@ -36,6 +36,7 @@ #include "weechat.h" #include "session.h" +#include "log.h" #include "../irc/irc.h" #include "../gui/gui.h" diff --git a/weechat/src/common/weechat.c b/weechat/src/common/weechat.c index f82a622e7..9cedef9d1 100644 --- a/weechat/src/common/weechat.c +++ b/weechat/src/common/weechat.c @@ -42,9 +42,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -63,11 +61,12 @@ #include "weechat.h" #include "backtrace.h" -#include "weeconfig.h" #include "command.h" #include "fifo.h" -#include "utf8.h" +#include "log.h" #include "session.h" +#include "utf8.h" +#include "weeconfig.h" #include "../irc/irc.h" #include "../gui/gui.h" @@ -76,19 +75,18 @@ #endif -char *weechat_argv0 = NULL; /* WeeChat binary file name (argv[0]) */ -char *weechat_session = NULL; /* WeeChat session file (for /upgrade command) */ -time_t weechat_start_time; /* WeeChat start time (used by /uptime command) */ -int quit_weechat; /* = 1 if quit request from user... why ? :'( */ -int sigsegv = 0; /* SIGSEGV received? */ -char *weechat_home = NULL; /* WeeChat home dir. (example: /home/toto/.weechat) */ -FILE *weechat_log_file = NULL; /* WeeChat log file (~/.weechat/weechat.log) */ +char *weechat_argv0 = NULL; /* WeeChat binary file name (argv[0]) */ +char *weechat_session = NULL; /* WeeChat session file (for /upgrade cmd) */ +time_t weechat_start_time; /* WeeChat start time (used by /uptime cmd) */ +int quit_weechat; /* = 1 if quit request from user... why ? :'( */ +int sigsegv = 0; /* SIGSEGV received? */ +char *weechat_home = NULL; /* WeeChat home dir. (default: ~/.weechat) */ -char *local_charset = NULL; /* local charset, for example: ISO-8859-1, UTF-8 */ +char *local_charset = NULL; /* local charset, for ex.: ISO-8859-1, UTF-8 */ -int server_cmd_line; /* at least one server on WeeChat command line */ -int auto_connect; /* enabled by default, can by disabled on cmd line */ -int auto_load_plugins; /* enabled by default, can by disabled on cmd line */ +int server_cmd_line; /* at least 1 server on WeeChat command line */ +int auto_connect; /* enabled by default (cmd option to disable) */ +int auto_load_plugins; /* enabled by default (cmd option to disable) */ #ifdef HAVE_GNUTLS gnutls_certificate_credentials gnutls_xcred; /* gnutls client credentials */ @@ -197,49 +195,6 @@ ascii_strncasecmp (char *string1, char *string2, int max) return (string1[0]) ? 1 : ((string2[0]) ? -1 : 0); } -/* - * weechat_log_printf: displays a message in WeeChat log (/weechat.log) - */ - -void -weechat_log_printf (char *message, ...) -{ - static char buffer[4096]; - char *ptr_buffer; - va_list argptr; - static time_t seconds; - struct tm *date_tmp; - - if (!weechat_log_file) - return; - - va_start (argptr, message); - vsnprintf (buffer, sizeof (buffer) - 1, message, argptr); - va_end (argptr); - - /* keep only valid chars */ - ptr_buffer = buffer; - while (ptr_buffer[0]) - { - if ((ptr_buffer[0] != '\n') - && (ptr_buffer[0] != '\r') - && ((unsigned char)(ptr_buffer[0]) < 32)) - ptr_buffer[0] = '.'; - ptr_buffer++; - } - - seconds = time (NULL); - date_tmp = localtime (&seconds); - if (date_tmp) - fprintf (weechat_log_file, "[%04d-%02d-%02d %02d:%02d:%02d] %s", - date_tmp->tm_year + 1900, date_tmp->tm_mon + 1, date_tmp->tm_mday, - date_tmp->tm_hour, date_tmp->tm_min, date_tmp->tm_sec, - buffer); - else - fprintf (weechat_log_file, "%s", buffer); - fflush (weechat_log_file); -} - /* * weechat_iconv: convert string to another charset */ @@ -834,35 +789,6 @@ weechat_init_vars () #endif } -/* - * weechat_init_log: initialize log file - */ - -void -weechat_init_log () -{ - int filename_length; - char *filename; - - filename_length = strlen (weechat_home) + 64; - filename = - (char *) malloc (filename_length * sizeof (char)); - snprintf (filename, filename_length, "%s/" WEECHAT_LOG_NAME, weechat_home); - - weechat_log_file = fopen (filename, "wt"); - if (!weechat_log_file - || (flock (fileno (weechat_log_file), LOCK_EX | LOCK_NB) != 0)) - { - fprintf (stderr, - _("%s unable to create/append to log file (%s/%s)\n" - "If another WeeChat process is using this file, try to run WeeChat\n" - "with another home using \"--dir\" command line option.\n"), - WEECHAT_ERROR, weechat_home, WEECHAT_LOG_NAME); - exit (1); - } - free (filename); -} - /* * weechat_config_read: read WeeChat config file */ @@ -948,11 +874,7 @@ weechat_shutdown (int return_code, int crash) fifo_remove (); if (weechat_home) free (weechat_home); - if (weechat_log_file) - { - flock (fileno (weechat_log_file), LOCK_UN); - fclose (weechat_log_file); - } + weechat_log_close (); if (local_charset) free (local_charset); alias_free_all (); @@ -1085,9 +1007,10 @@ weechat_sigsegv () weechat_home); fprintf (stderr, "***\n"); fprintf (stderr, "*** Please help WeeChat developers to fix this bug:\n"); - fprintf (stderr, "*** 1. if you have a core file, please run: gdb weechat-curses core\n"); + fprintf (stderr, "*** 1. If you have a core file, please run: gdb weechat-curses core\n"); fprintf (stderr, "*** then issue \"bt\" command and send result to developers\n"); - fprintf (stderr, "*** 2. otherwise send backtrace displayed below and weechat.log\n"); + fprintf (stderr, "*** To enable core files with bash shell: ulimit -c 10000\n"); + fprintf (stderr, "*** 2. Otherwise send backtrace displayed below and weechat.log\n"); fprintf (stderr, "*** (be careful, private info may be in this file since\n"); fprintf (stderr, "*** part of chats are displayed, so remove lines if needed)\n\n"); @@ -1125,7 +1048,7 @@ main (int argc, char *argv[]) gui_keyboard_init (); /* init keyb. (default key bindings)*/ weechat_parse_args (argc, argv); /* parse command line args */ weechat_create_home_dirs (); /* create WeeChat directories */ - weechat_init_log (); /* init log file */ + weechat_log_init (); /* init log file */ command_index_build (); /* build cmd index for completion */ weechat_config_read (); /* read configuration */ weechat_create_config_dirs (); /* create config directories */ diff --git a/weechat/src/common/weechat.h b/weechat/src/common/weechat.h index 6206cee37..5c08702e4 100644 --- a/weechat/src/common/weechat.h +++ b/weechat/src/common/weechat.h @@ -98,7 +98,6 @@ extern char *weechat_argv0; extern time_t weechat_start_time; extern int quit_weechat; extern char *weechat_home; -extern FILE *weechat_log_file; extern char *local_charset; #ifdef HAVE_GNUTLS @@ -109,7 +108,6 @@ extern void ascii_tolower (char *); extern void ascii_toupper (char *); extern int ascii_strcasecmp (char *, char *); extern int ascii_strncasecmp (char *, char *, int); -extern void weechat_log_printf (char *, ...); extern char *weechat_iconv (char *, char *, char *); extern char *weechat_strreplace (char *, char *, char *); extern void weechat_dump (int); diff --git a/weechat/src/common/weeconfig.c b/weechat/src/common/weeconfig.c index 606596c0c..55da0944a 100644 --- a/weechat/src/common/weeconfig.c +++ b/weechat/src/common/weeconfig.c @@ -1282,23 +1282,23 @@ config_change_log () if (BUFFER_IS_SERVER(ptr_buffer)) { if (cfg_log_auto_server && !ptr_buffer->log_file) - log_start (ptr_buffer); + gui_log_start (ptr_buffer); else if (!cfg_log_auto_server && ptr_buffer->log_file) - log_end (ptr_buffer); + gui_log_end (ptr_buffer); } if (BUFFER_IS_CHANNEL(ptr_buffer)) { if (cfg_log_auto_channel && !ptr_buffer->log_file) - log_start (ptr_buffer); + gui_log_start (ptr_buffer); else if (!cfg_log_auto_channel && ptr_buffer->log_file) - log_end (ptr_buffer); + gui_log_end (ptr_buffer); } if (BUFFER_IS_PRIVATE(ptr_buffer)) { if (cfg_log_auto_private && !ptr_buffer->log_file) - log_start (ptr_buffer); + gui_log_start (ptr_buffer); else if (!cfg_log_auto_private && ptr_buffer->log_file) - log_end (ptr_buffer); + gui_log_end (ptr_buffer); } } } diff --git a/weechat/src/gui/Makefile.am b/weechat/src/gui/Makefile.am index 6a1ea4fc1..12c3da33d 100644 --- a/weechat/src/gui/Makefile.am +++ b/weechat/src/gui/Makefile.am @@ -23,6 +23,7 @@ lib_weechat_gui_common_a_SOURCES = gui-buffer.c \ gui-common.c \ gui-action.c \ gui-keyboard.c \ + gui-log.c \ gui-window.c \ gui-panel.c \ gui.h \ diff --git a/weechat/src/gui/curses/Makefile.am b/weechat/src/gui/curses/Makefile.am index 283d89bfe..2dd1d2678 100644 --- a/weechat/src/gui/curses/Makefile.am +++ b/weechat/src/gui/curses/Makefile.am @@ -20,9 +20,7 @@ INCLUDES = -DLOCALEDIR=\"$(datadir)/locale\" bin_PROGRAMS = weechat-curses if PLUGINS -weechat_curses_LDADD = ../gui-common.o ../gui-buffer.o \ - ../gui-window.o ../gui-panel.o \ - ../gui-keyboard.o ../gui-action.o \ +weechat_curses_LDADD = ../lib_weechat_gui_common.a \ ../../common/lib_weechat_main.a \ ../../irc/lib_weechat_irc.a \ ../../plugins/lib_weechat_plugins.a \ @@ -30,9 +28,7 @@ weechat_curses_LDADD = ../gui-common.o ../gui-buffer.o \ $(NCURSES_LIBS) \ $(GNUTLS_LFLAGS) else -weechat_curses_LDADD = ../gui-common.o ../gui-buffer.o \ - ../gui-window.o ../gui-panel.o \ - ../gui-keyboard.o ../gui-action.o \ +weechat_curses_LDADD = ../lib_weechat_gui_common.a \ ../../common/lib_weechat_main.a \ ../../irc/lib_weechat_irc.a \ $(PLUGINS_LIBS) \ diff --git a/weechat/src/gui/curses/gui-curses-window.c b/weechat/src/gui/curses/gui-curses-window.c index 006f41f47..f450a65c0 100644 --- a/weechat/src/gui/curses/gui-curses-window.c +++ b/weechat/src/gui/curses/gui-curses-window.c @@ -32,6 +32,7 @@ #include "../../common/weechat.h" #include "../gui.h" #include "../../common/hotlist.h" +#include "../../common/log.h" #include "../../common/weeconfig.h" #include "gui-curses.h" diff --git a/weechat/src/gui/gtk/Makefile.am b/weechat/src/gui/gtk/Makefile.am index d4e09550c..a20bc721b 100644 --- a/weechat/src/gui/gtk/Makefile.am +++ b/weechat/src/gui/gtk/Makefile.am @@ -20,9 +20,7 @@ INCLUDES = -DLOCALEDIR=\"$(datadir)/locale\" $(GTK_CFLAGS) bin_PROGRAMS = weechat-gtk if PLUGINS -weechat_gtk_LDADD = ../gui-common.o ../gui-buffer.o \ - ../gui-window.o ../gui-panel.o \ - ../gui-keyboard.o ../gui-action.o \ +weechat_gtk_LDADD = ../lib_weechat_gui_common.a \ ../../common/lib_weechat_main.a \ ../../irc/lib_weechat_irc.a \ ../../plugins/lib_weechat_plugins.a \ @@ -30,9 +28,7 @@ weechat_gtk_LDADD = ../gui-common.o ../gui-buffer.o \ $(GTK_LIBS) \ $(GNUTLS_LFLAGS) else -weechat_gtk_LDADD = ../gui-common.o ../gui-buffer.o \ - ../gui-window.o ../gui-panel.o \ - ../gui-keyboard.o ../gui-action.o \ +weechat_gtk_LDADD = ../lib_weechat_gui_common.a \ ../../common/lib_weechat_main.a \ ../../irc/lib_weechat_irc.a \ $(PLUGINS_LIBS) \ diff --git a/weechat/src/gui/gtk/gui-gtk-window.c b/weechat/src/gui/gtk/gui-gtk-window.c index 427b2627e..7107cd660 100644 --- a/weechat/src/gui/gtk/gui-gtk-window.c +++ b/weechat/src/gui/gtk/gui-gtk-window.c @@ -30,6 +30,7 @@ #include "../../common/weechat.h" #include "../gui.h" #include "../../common/hotlist.h" +#include "../../common/log.h" #include "../../common/weeconfig.h" #include "gui-gtk.h" diff --git a/weechat/src/gui/gui-buffer.c b/weechat/src/gui/gui-buffer.c index a3fdefd30..a57299af3 100644 --- a/weechat/src/gui/gui-buffer.c +++ b/weechat/src/gui/gui-buffer.c @@ -98,7 +98,7 @@ gui_buffer_new (t_gui_window *window, void *server, void *channel, int type, if (cfg_look_one_server_buffer && server && !channel) gui_buffers->all_servers = 1; if (cfg_log_auto_server) - log_start (gui_buffers); + gui_log_start (gui_buffers); return gui_buffers; } @@ -165,7 +165,7 @@ gui_buffer_new (t_gui_window *window, void *server, void *channel, int type, if ((cfg_log_auto_server && BUFFER_IS_SERVER(new_buffer)) || (cfg_log_auto_channel && BUFFER_IS_CHANNEL(new_buffer)) || (cfg_log_auto_private && BUFFER_IS_PRIVATE(new_buffer))) - log_start (new_buffer); + gui_log_start (new_buffer); /* init input buffer */ new_buffer->has_input = (new_buffer->type == BUFFER_TYPE_STANDARD) ? 1 : 0; @@ -479,7 +479,7 @@ gui_buffer_free (t_gui_buffer *buffer, int switch_to_another) /* close log if opened */ if (buffer->log_file) - log_end (buffer); + gui_log_end (buffer); if (buffer->input_buffer) free (buffer->input_buffer); diff --git a/weechat/src/gui/gui-common.c b/weechat/src/gui/gui-common.c index 9d63e7d4b..9dc09d5af 100644 --- a/weechat/src/gui/gui-common.c +++ b/weechat/src/gui/gui-common.c @@ -193,18 +193,18 @@ gui_add_to_line (t_gui_buffer *buffer, int type, char *nick, char *message) } if (buffer->line_complete && buffer->log_file && buffer->last_line->log_write) { - log_write_date (buffer); + gui_log_write_date (buffer); if (buffer->last_line->nick) { - log_write (buffer, "<"); - log_write (buffer, buffer->last_line->nick); - log_write (buffer, "> "); + gui_log_write (buffer, "<"); + gui_log_write (buffer, buffer->last_line->nick); + gui_log_write (buffer, "> "); } if (buffer->last_line->ofs_start_message >= 0) - log_write_line (buffer, - buffer->last_line->data + buffer->last_line->ofs_start_message); + gui_log_write_line (buffer, + buffer->last_line->data + buffer->last_line->ofs_start_message); else - log_write_line (buffer, buffer->last_line->data); + gui_log_write_line (buffer, buffer->last_line->data); } } diff --git a/weechat/src/gui/gui-keyboard.c b/weechat/src/gui/gui-keyboard.c index 253c997b4..5325e8a1f 100644 --- a/weechat/src/gui/gui-keyboard.c +++ b/weechat/src/gui/gui-keyboard.c @@ -31,6 +31,7 @@ #include "../common/weechat.h" #include "gui.h" #include "../common/command.h" +#include "../common/log.h" #ifdef PLUGINS #include "../plugins/plugins.h" diff --git a/weechat/src/gui/gui-log.c b/weechat/src/gui/gui-log.c new file mode 100644 index 000000000..086528c30 --- /dev/null +++ b/weechat/src/gui/gui-log.c @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2003-2006 by FlashCode + * See README for License detail, AUTHORS for developers list. + * + * 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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* gui-log.c: log buffers to files */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "../common/weechat.h" +#include "gui.h" +#include "../common/log.h" +#include "../common/weeconfig.h" + + +/* + * gui_log_write_date: writes date to log file + */ + +void +gui_log_write_date (t_gui_buffer *buffer) +{ + static char buf_time[256]; + static time_t seconds; + struct tm *date_tmp; + + if (buffer->log_file) + { + seconds = time (NULL); + date_tmp = localtime (&seconds); + if (date_tmp) + { + strftime (buf_time, sizeof (buf_time) - 1, cfg_log_timestamp, date_tmp); + fprintf (buffer->log_file, "%s ", buf_time); + fflush (buffer->log_file); + } + } +} + +/* + * gui_log_write_line: writes a line to log file + */ + +void +gui_log_write_line (t_gui_buffer *buffer, char *message) +{ + char *msg_no_color; + + if (buffer->log_file) + { + msg_no_color = (char *)gui_color_decode ((unsigned char *)message, 0); + fprintf (buffer->log_file, "%s\n", + (msg_no_color) ? msg_no_color : message); + fflush (buffer->log_file); + if (msg_no_color) + free (msg_no_color); + } +} + +/* + * gui_log_write: writes a message to log file + */ + +void +gui_log_write (t_gui_buffer *buffer, char *message) +{ + char *msg_no_color; + + if (buffer->log_file) + { + msg_no_color = (char *)gui_color_decode ((unsigned char *)message, 0); + fprintf (buffer->log_file, "%s", + (msg_no_color) ? msg_no_color : message); + fflush (buffer->log_file); + if (msg_no_color) + free (msg_no_color); + } +} + +/* + * gui_log_start: starts a log + */ + +void +gui_log_start (t_gui_buffer *buffer) +{ + int length; + char *log_path, *log_path2; + + log_path = weechat_strreplace (cfg_log_path, "~", getenv ("HOME")); + log_path2 = weechat_strreplace (log_path, "%h", weechat_home); + if (!log_path || !log_path2) + { + weechat_log_printf (_("Not enough memory to write log file for a buffer\n")); + if (log_path) + free (log_path); + if (log_path2) + free (log_path2); + return; + } + length = strlen (log_path2) + 64; + if (SERVER(buffer)) + length += strlen (SERVER(buffer)->name); + if (CHANNEL(buffer)) + length += strlen (CHANNEL(buffer)->name); + + buffer->log_filename = (char *) malloc (length); + if (!buffer->log_filename) + { + weechat_log_printf (_("Not enough memory to write log file for a buffer\n")); + if (log_path) + free (log_path); + if (log_path2) + free (log_path2); + return; + } + + strcpy (buffer->log_filename, log_path2); + if (log_path) + free (log_path); + if (log_path2) + free (log_path2); + if (buffer->log_filename[strlen (buffer->log_filename) - 1] != DIR_SEPARATOR_CHAR) + strcat (buffer->log_filename, DIR_SEPARATOR); + + if (SERVER(buffer)) + { + strcat (buffer->log_filename, SERVER(buffer)->name); + strcat (buffer->log_filename, "."); + } + if (CHANNEL(buffer)) + { + strcat (buffer->log_filename, CHANNEL(buffer)->name); + strcat (buffer->log_filename, "."); + } + strcat (buffer->log_filename, "weechatlog"); + + buffer->log_file = fopen (buffer->log_filename, "a"); + if (!buffer->log_file) + { + weechat_log_printf (_("Unable to write log file for a buffer\n")); + free (buffer->log_filename); + return; + } + gui_log_write (buffer, _("**** Beginning of log ")); + gui_log_write_date (buffer); + gui_log_write (buffer, "****\n"); +} + +/* + * gui_log_end: ends a log + */ + +void +gui_log_end (t_gui_buffer *buffer) +{ + if (buffer->log_file) + { + gui_log_write (buffer, _("**** End of log ")); + gui_log_write_date (buffer); + gui_log_write (buffer, "****\n"); + fclose (buffer->log_file); + buffer->log_file = NULL; + } + if (buffer->log_filename) + free (buffer->log_filename); +} diff --git a/weechat/src/gui/gui.h b/weechat/src/gui/gui.h index ff3036a65..fa544939d 100644 --- a/weechat/src/gui/gui.h +++ b/weechat/src/gui/gui.h @@ -27,6 +27,7 @@ #include "gui-window.h" #include "gui-keyboard.h" + #define gui_printf(buffer, fmt, argz...) \ gui_printf_internal(buffer, 1, MSG_TYPE_INFO, NULL, fmt, ##argz) @@ -42,6 +43,7 @@ #define gui_printf_nolog_notime(buffer, fmt, argz...) \ gui_printf_internal(buffer, 0, MSG_TYPE_NOLOG, NULL, fmt, ##argz) + typedef struct t_gui_infobar t_gui_infobar; struct t_gui_infobar @@ -163,6 +165,13 @@ extern int gui_keyboard_pressed (char *); extern void gui_keyboard_free (t_gui_key *); extern void gui_keyboard_free_all (); +/* log */ +extern void gui_log_write_date (t_gui_buffer *); +extern void gui_log_write_line (t_gui_buffer *, char *); +extern void gui_log_write (t_gui_buffer *, char *); +extern void gui_log_start (t_gui_buffer *); +extern void gui_log_end (t_gui_buffer *); + /* other */ extern void gui_infobar_printf (int, int, char *, ...); extern void gui_infobar_printf_from_buffer (t_gui_buffer *, int, int, char *, char *, ...); diff --git a/weechat/src/irc/irc-channel.c b/weechat/src/irc/irc-channel.c index 27938510c..e5c6a0d51 100644 --- a/weechat/src/irc/irc-channel.c +++ b/weechat/src/irc/irc-channel.c @@ -30,6 +30,7 @@ #include "../common/weechat.h" #include "irc.h" +#include "../common/log.h" #include "../common/utf8.h" #include "../common/weeconfig.h" #include "../gui/gui.h" diff --git a/weechat/src/irc/irc-dcc.c b/weechat/src/irc/irc-dcc.c index 378625b8e..f68274762 100644 --- a/weechat/src/irc/irc-dcc.c +++ b/weechat/src/irc/irc-dcc.c @@ -39,8 +39,9 @@ #include "../common/weechat.h" #include "irc.h" -#include "../common/weeconfig.h" +#include "../common/log.h" #include "../common/hotlist.h" +#include "../common/weeconfig.h" #include "../gui/gui.h" diff --git a/weechat/src/irc/irc-ignore.c b/weechat/src/irc/irc-ignore.c index a0b4a4948..cb80bd52b 100644 --- a/weechat/src/irc/irc-ignore.c +++ b/weechat/src/irc/irc-ignore.c @@ -30,6 +30,7 @@ #include "../common/weechat.h" #include "irc.h" #include "../common/command.h" +#include "../common/log.h" char *ignore_types[] = diff --git a/weechat/src/irc/irc-nick.c b/weechat/src/irc/irc-nick.c index 61023b398..3e6f231cb 100644 --- a/weechat/src/irc/irc-nick.c +++ b/weechat/src/irc/irc-nick.c @@ -30,6 +30,7 @@ #include "../common/weechat.h" #include "irc.h" +#include "../common/log.h" #include "../common/weeconfig.h" diff --git a/weechat/src/irc/irc-server.c b/weechat/src/irc/irc-server.c index 685979c2d..3ab6978fe 100644 --- a/weechat/src/irc/irc-server.c +++ b/weechat/src/irc/irc-server.c @@ -45,6 +45,7 @@ #include "../common/weechat.h" #include "irc.h" +#include "../common/log.h" #include "../common/weeconfig.h" #include "../gui/gui.h" diff --git a/weechat/src/plugins/plugins-interface.c b/weechat/src/plugins/plugins-interface.c index 72c2b6a5c..3cd18eba0 100644 --- a/weechat/src/plugins/plugins-interface.c +++ b/weechat/src/plugins/plugins-interface.c @@ -258,7 +258,7 @@ weechat_plugin_log (t_weechat_plugin *plugin, va_start (argptr, message); vsnprintf (buf, sizeof (buf) - 1, message, argptr); va_end (argptr); - log_write_line (ptr_buffer, buf); + gui_log_write_line (ptr_buffer, buf); } }