mirror of
https://github.com/weechat/weechat.git
synced 2026-07-06 09:43:13 +02:00
Moved content of src/common/log.c to src/gui/gui-log.c, log.c is now used for WeeChat log file
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include "weechat.h"
|
||||
#include "fifo.h"
|
||||
#include "command.h"
|
||||
#include "log.h"
|
||||
#include "weeconfig.h"
|
||||
#include "../gui/gui.h"
|
||||
|
||||
|
||||
+84
-133
@@ -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 <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/file.h>
|
||||
|
||||
#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_home>/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);
|
||||
}
|
||||
|
||||
+5
-7
@@ -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 */
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
|
||||
#include "weechat.h"
|
||||
#include "session.h"
|
||||
#include "log.h"
|
||||
#include "../irc/irc.h"
|
||||
#include "../gui/gui.h"
|
||||
|
||||
|
||||
+18
-95
@@ -42,9 +42,7 @@
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
@@ -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_home>/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 */
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user