1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-06 17:53:13 +02:00

Log server/channel/private to file

This commit is contained in:
Sebastien Helleu
2004-09-19 20:26:15 +00:00
parent 0f718bf2c0
commit ce7f24ecc1
28 changed files with 1026 additions and 594 deletions
+3 -1
View File
@@ -30,4 +30,6 @@ lib_weechat_main_a_SOURCES = weechat.c \
history.c \
history.h \
hotlist.c \
hotlist.h
hotlist.h \
log.c \
log.h
+149
View File
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2004 by FlashCode <flashcode@flashtux.org>
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* log.c: log buffers to files */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "weechat.h"
#include "log.h"
#include "weeconfig.h"
/*
* log_write_date: writes date to log file
*/
void
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);
}
}
}
/*
* log_write: writes a message to log file
*/
void
log_write (t_gui_buffer *buffer, char *message)
{
if (buffer->log_file)
{
fprintf (buffer->log_file, "%s", message);
fflush (buffer->log_file);
}
}
/*
* log_start: starts a log
*/
void
log_start (t_gui_buffer *buffer)
{
int length;
char *ptr_home;
ptr_home = getenv ("HOME");
length = strlen (cfg_log_path) +
((cfg_log_path[0] == '~') ? strlen (ptr_home) : 0) +
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)
{
wee_log_printf (_("Not enough memory to write log file for a buffer\n"));
return;
}
if (cfg_log_path[0] == '~')
{
strcpy (buffer->log_filename, ptr_home);
strcat (buffer->log_filename, cfg_log_path + 1);
}
else
strcpy (buffer->log_filename, cfg_log_path);
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)
{
wee_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");
}
/*
* log_end: ends a log
*/
void
log_end (t_gui_buffer *buffer)
{
if (buffer->log_file)
{
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);
}
+31
View File
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2004 by FlashCode <flashcode@flashtux.org>
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __WEECHAT_LOG_H
#define __WEECHAT_LOG_H 1
#include "../irc/irc.h"
extern void log_write_date (t_gui_buffer *);
extern void log_write (t_gui_buffer *, char *);
extern void log_start (t_gui_buffer *);
extern void log_end (t_gui_buffer *);
#endif /* log.h */
+17 -8
View File
@@ -60,7 +60,7 @@
int quit_weechat; /* = 1 if quit request from user... why ? :'( */
char *weechat_home; /* WeeChat home dir. (example: /home/toto/.weechat) */
FILE *log_file; /* WeeChat log file (~/.weechat/weechat.log) */
FILE *weechat_log_file; /* WeeChat log file (~/.weechat/weechat.log) */
int server_cmd_line; /* at least one server on WeeChat command line */
@@ -88,7 +88,7 @@ wee_log_printf (char *message, ...)
static time_t seconds;
struct tm *date_tmp;
if (!log_file)
if (!weechat_log_file)
return;
va_start (argptr, message);
@@ -98,13 +98,13 @@ wee_log_printf (char *message, ...)
seconds = time (NULL);
date_tmp = localtime (&seconds);
if (date_tmp)
fprintf (log_file, "[%04d-%02d-%02d %02d:%02d:%02d] %s",
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 (log_file, "%s", buffer);
fflush (log_file);
fprintf (weechat_log_file, "%s", buffer);
fflush (weechat_log_file);
}
/*
@@ -354,6 +354,15 @@ wee_create_home_dirs ()
}
#endif
/* create "~/.weechat/logs" */
snprintf (dir_name, dir_length, "%s%s%s", weechat_home, DIR_SEPARATOR,
"logs");
if (!wee_create_dir (dir_name))
{
fprintf (stderr, _("%s unable to create ~/.weechat/logs directory\n"),
WEECHAT_WARNING);
}
free (dir_name);
}
@@ -383,7 +392,7 @@ wee_init_log ()
filename =
(char *) malloc (filename_length * sizeof (char));
snprintf (filename, filename_length, "%s/" WEECHAT_LOG_NAME, weechat_home);
if ((log_file = fopen (filename, "wt")) == NULL)
if ((weechat_log_file = fopen (filename, "wt")) == NULL)
fprintf (stderr,
_("%s unable to create/append to log file (~/.weechat/%s)"),
WEECHAT_WARNING, WEECHAT_LOG_NAME);
@@ -440,8 +449,8 @@ wee_shutdown ()
dcc_end ();
server_free_all ();
gui_end ();
if (log_file)
fclose (log_file);
if (weechat_log_file)
fclose (weechat_log_file);
exit (EXIT_SUCCESS);
}
+10 -23
View File
@@ -412,19 +412,21 @@ t_config_option weechat_options_history[] =
/* config, log section */
int cfg_log_auto_channels;
int cfg_log_auto_server;
int cfg_log_auto_channel;
int cfg_log_auto_private;
char *cfg_log_path;
char *cfg_log_name;
char *cfg_log_timestamp;
char *cfg_log_start_string;
char *cfg_log_end_string;
t_config_option weechat_options_log[] =
{ { "log_auto_channels", N_("automatically log channel chats"),
{ { "log_auto_server", N_("automatically log server messages"),
N_("automatically log server messages"),
OPTION_TYPE_BOOLEAN, BOOL_FALSE, BOOL_TRUE, BOOL_TRUE,
NULL, NULL, &cfg_log_auto_server, NULL, NULL },
{ "log_auto_channel", N_("automatically log channel chats"),
N_("automatically log channel chats"),
OPTION_TYPE_BOOLEAN, BOOL_FALSE, BOOL_TRUE, BOOL_TRUE,
NULL, NULL, &cfg_log_auto_channels, NULL, NULL },
NULL, NULL, &cfg_log_auto_channel, NULL, NULL },
{ "log_auto_private", N_("automatically log private chats"),
N_("automatically log private chats"),
OPTION_TYPE_BOOLEAN, BOOL_FALSE, BOOL_TRUE, BOOL_TRUE,
@@ -433,25 +435,10 @@ t_config_option weechat_options_log[] =
N_("path for WeeChat log files"),
OPTION_TYPE_STRING, 0, 0, 0,
"~/.weechat/logs/", NULL, NULL, &cfg_log_path, NULL },
{ "log_name", N_("name for log files"),
N_("name for log files (%S == irc server name, "
"%N == channel name (or nickname if private chat)"),
OPTION_TYPE_STRING, 0, 0, 0,
"%S,%N.weechatlog", NULL, NULL, &cfg_log_name, NULL },
{ "log_timestamp", N_("timestamp for log"),
N_("timestamp for log (see man strftime for date/time specifiers)"),
OPTION_TYPE_STRING, 0, 0, 0,
"~", NULL, NULL, &cfg_log_timestamp, NULL },
{ "log_start_string", N_("start string for log files"),
N_("text written when starting new log file "
"(see man strftime for date/time specifiers)"),
OPTION_TYPE_STRING, 0, 0, 0,
"--- Log started %a %b %d %Y %H:%M:%s", NULL, NULL, &cfg_log_start_string, NULL },
{ "log_end_string", N_("end string for log files"),
N_("text written when ending log file "
"(see man strftime for date/time specifiers)"),
OPTION_TYPE_STRING, 0, 0, 0,
"--- Log ended %a %b %d %Y %H:%M:%s", NULL, NULL, &cfg_log_end_string, NULL },
"%Y %b %d %H:%M:%S", NULL, NULL, &cfg_log_timestamp, NULL },
{ NULL, NULL, NULL, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL }
};
@@ -1305,7 +1292,7 @@ config_write (char *config_name)
if ((i != CONFIG_SECTION_ALIAS) && (i != CONFIG_SECTION_SERVER))
{
fprintf (file, "\n[%s]\n", config_sections[i].section_name);
if ((i == CONFIG_SECTION_LOG) || (i == CONFIG_SECTION_PROXY))
if (i == CONFIG_SECTION_PROXY)
fprintf (file,
"# WARNING!!! Options for section \"%s\" are not developed!\n",
config_sections[i].section_name);
+2 -4
View File
@@ -139,13 +139,11 @@ extern int cfg_col_dcc_aborted;
extern int cfg_history_max_lines;
extern int cfg_history_max_commands;
extern int cfg_log_auto_channels;
extern int cfg_log_auto_server;
extern int cfg_log_auto_channel;
extern int cfg_log_auto_private;
extern char *cfg_log_path;
extern char *cfg_log_name;
extern char *cfg_log_timestamp;
extern char *cfg_log_start_string;
extern char *cfg_log_end_string;
extern int cfg_dcc_auto_accept_files;
extern int cfg_dcc_auto_accept_chats;
+7 -2
View File
@@ -36,6 +36,7 @@
#include "../gui.h"
#include "../../common/weeconfig.h"
#include "../../common/hotlist.h"
#include "../../common/log.h"
#include "../../irc/irc.h"
@@ -1409,7 +1410,7 @@ gui_get_dcc_buffer ()
/* check if dcc buffer exists */
for (ptr_buffer = gui_buffers; ptr_buffer; ptr_buffer = ptr_buffer->next_buffer)
{
if (BUFFER_IS_DCC (ptr_buffer))
if (ptr_buffer->dcc)
break;
}
if (ptr_buffer)
@@ -1433,7 +1434,7 @@ gui_switch_to_dcc_buffer ()
/* check if dcc buffer exists */
for (ptr_buffer = gui_buffers; ptr_buffer; ptr_buffer = ptr_buffer->next_buffer)
{
if (BUFFER_IS_DCC (ptr_buffer))
if (ptr_buffer->dcc)
break;
}
if (ptr_buffer)
@@ -2032,8 +2033,12 @@ gui_printf_color_type (t_gui_buffer *buffer, int type, int color, char *message,
snprintf (timestamp, 16, "%02d", date_tmp->tm_sec);
gui_add_message (buffer, MSG_TYPE_TIME, COLOR_WIN_CHAT_TIME, timestamp);
gui_add_message (buffer, MSG_TYPE_TIME, COLOR_WIN_CHAT_DARK, "] ");
if (buffer->log_file)
log_write_date (buffer);
}
gui_add_message (buffer, type, color, pos + 1);
if (buffer->log_file)
log_write (buffer, pos + 1);
pos = strchr (pos + 1, '\n');
if (pos && !pos[1])
pos = NULL;
+16
View File
@@ -36,6 +36,7 @@
#include "gui.h"
#include "../common/weeconfig.h"
#include "../common/hotlist.h"
#include "../common/log.h"
#include "../irc/irc.h"
@@ -146,6 +147,8 @@ gui_buffer_new (t_gui_window *window, void *server, void *channel, int dcc,
((t_irc_channel *)(channel))->buffer = gui_buffers;
SERVER(gui_buffers) = server;
CHANNEL(gui_buffers) = channel;
if (cfg_log_auto_server)
log_start (gui_buffers);
return gui_buffers;
}
@@ -179,6 +182,14 @@ gui_buffer_new (t_gui_window *window, void *server, void *channel, int dcc,
new_buffer->num_lines = 0;
new_buffer->line_complete = 1;
/* create/append to log file */
new_buffer->log_filename = NULL;
new_buffer->log_file = NULL;
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);
/* init input buffer */
new_buffer->input_buffer_alloc = INPUT_BUFFER_BLOCK_SIZE;
new_buffer->input_buffer = (char *) malloc (INPUT_BUFFER_BLOCK_SIZE);
@@ -390,6 +401,11 @@ gui_buffer_free (t_gui_buffer *buffer, int switch_to_another)
gui_line_free (buffer->lines);
buffer->lines = ptr_line;
}
/* close log if opened */
if (buffer->log_file)
log_end (buffer);
if (buffer->input_buffer)
free (buffer->input_buffer);
+4 -3
View File
@@ -73,7 +73,6 @@
#define BUFFER_IS_SERVER(buffer) (SERVER(buffer) && !CHANNEL(buffer))
#define BUFFER_IS_CHANNEL(buffer) (CHANNEL(buffer) && (CHANNEL(buffer)->type == CHAT_CHANNEL))
#define BUFFER_IS_PRIVATE(buffer) (CHANNEL(buffer) && (CHANNEL(buffer)->type == CHAT_PRIVATE))
#define BUFFER_IS_DCC(buffer) (!SERVER(buffer) && !CHANNEL(buffer))
#define MSG_TYPE_TIME 1
#define MSG_TYPE_NICK 2
@@ -143,8 +142,6 @@ struct t_gui_buffer
/* server/channel */
void *server; /* buffer's server */
void *channel; /* buffer's channel */
/* dcc buffer */
int dcc; /* buffer is dcc status */
/* chat content (lines, line is composed by many messages) */
@@ -153,6 +150,10 @@ struct t_gui_buffer
int num_lines; /* number of lines in the window */
int line_complete; /* current line complete ? (\n ending) */
/* file to save buffer content */
char *log_filename; /* filename for saving buffer content */
FILE *log_file; /* for logging buffer to file */
/* inupt buffer */
char *input_buffer; /* input buffer */
int input_buffer_alloc; /* input buffer: allocated size in mem */