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

Add config file logger.conf (replaces old logger options in plugins.conf)

This commit is contained in:
Sebastien Helleu
2008-10-08 15:50:59 +02:00
parent d5b78f0d8f
commit 4f9843f84c
21 changed files with 793 additions and 163 deletions
+10
View File
@@ -1155,3 +1155,13 @@ irc_config_write ()
{
return weechat_config_write (irc_config_file);
}
/*
* irc_config_free: free IRC configuration
*/
void
irc_config_free ()
{
weechat_config_free (irc_config_file);
}
+1
View File
@@ -108,5 +108,6 @@ struct t_config_option *irc_config_server_new_option (struct t_config_file *conf
extern int irc_config_init ();
extern int irc_config_read ();
extern int irc_config_write ();
extern void irc_config_free ();
#endif /* irc-config.h */
+3 -1
View File
@@ -110,7 +110,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
if (!irc_config_init ())
return WEECHAT_RC_ERROR;
if (irc_config_read () < 0)
return WEECHAT_RC_ERROR;
@@ -205,5 +205,7 @@ weechat_plugin_end (struct t_weechat_plugin *plugin)
irc_server_free_all ();
irc_config_free ();
return WEECHAT_RC_OK;
}
+1
View File
@@ -17,6 +17,7 @@
ADD_LIBRARY(logger MODULE
logger.c logger.h
logger-buffer.c logger-buffer.h
logger-config.c logger-config.h
logger-info.c logger-info.h
logger-tail.c logger-tail.h)
SET_TARGET_PROPERTIES(logger PROPERTIES PREFIX "")
+2
View File
@@ -24,6 +24,8 @@ logger_la_SOURCES = logger.c \
logger.h \
logger-buffer.c \
logger-buffer.h \
logger-config.c \
logger-config.h \
logger-info.c \
logger-info.h \
logger-tail.c \
+150
View File
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2003-2008 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/* logger-config.c: logger configuration options */
#include <stdlib.h>
#include <limits.h>
#include "../weechat-plugin.h"
#include "logger.h"
#include "logger-config.h"
struct t_config_file *logger_config_file = NULL;
/* logger config, look section */
struct t_config_option *logger_config_look_backlog;
/* logger config, file section */
struct t_config_option *logger_config_file_auto_log;
struct t_config_option *logger_config_file_name_lower_case;
struct t_config_option *logger_config_file_path;
struct t_config_option *logger_config_file_info_lines;
struct t_config_option *logger_config_file_time_format;
/*
* logger_config_init: init logger configuration file
* return: 1 if ok, 0 if error
*/
int
logger_config_init ()
{
struct t_config_section *ptr_section;
logger_config_file = weechat_config_new (LOGGER_CONFIG_NAME,
NULL, NULL);
if (!logger_config_file)
return 0;
/* look */
ptr_section = weechat_config_new_section (logger_config_file, "look",
0, 0,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL);
if (!ptr_section)
{
weechat_config_free (logger_config_file);
return 0;
}
logger_config_look_backlog = weechat_config_new_option (
logger_config_file, ptr_section,
"backlog", "integer",
N_("maximum number of lines to display from log file when creating "
"new buffer (0 = no backlog)"),
NULL, 0, INT_MAX, "20", NULL, NULL, NULL, NULL, NULL, NULL);
/* file */
ptr_section = weechat_config_new_section (logger_config_file, "file",
0, 0,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL);
if (!ptr_section)
{
weechat_config_free (logger_config_file);
return 0;
}
logger_config_file_auto_log = weechat_config_new_option (
logger_config_file, ptr_section,
"auto_log", "boolean",
N_("automatically save content of buffers to files (unless a buffer "
"disables log)"),
NULL, 0, 0, "on", NULL, NULL, NULL, NULL, NULL, NULL);
logger_config_file_name_lower_case = weechat_config_new_option (
logger_config_file, ptr_section,
"name_lower_case", "boolean",
N_("use only lower case for log filenames"),
NULL, 0, 0, "on", NULL, NULL, NULL, NULL, NULL, NULL);
logger_config_file_path = weechat_config_new_option (
logger_config_file, ptr_section,
"path", "string",
N_("path for WeeChat log files ('%h' will be replaced by WeeChat "
"home, ~/.weechat by default)"),
NULL, 0, 0, "%h/logs/", NULL, NULL, NULL, NULL, NULL, NULL);
logger_config_file_info_lines = weechat_config_new_option (
logger_config_file, ptr_section,
"info_lines", "boolean",
N_("write information line in log file when log starts or ends for a "
"buffer"),
NULL, 0, 0, "off", NULL, NULL, NULL, NULL, NULL, NULL);
logger_config_file_time_format = weechat_config_new_option (
logger_config_file, ptr_section,
"time_format", "string",
N_("timestamp used in log files (see man strftime for date/time "
"specifiers)"),
NULL, 0, 0, "%Y-%m-%d %H:%M:%S", NULL, NULL, NULL, NULL, NULL, NULL);
return 1;
}
/*
* logger_config_read: read logger configuration file
*/
int
logger_config_read ()
{
return weechat_config_read (logger_config_file);
}
/*
* logger_config_write: write logger configuration file
*/
int
logger_config_write ()
{
return weechat_config_write (logger_config_file);
}
/*
* logger_config_free: free logger configuration
*/
void
logger_config_free ()
{
weechat_config_free (logger_config_file);
}
+41
View File
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2003-2008 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef __WEECHAT_LOGGER_CONFIG_H
#define __WEECHAT_LOGGER_CONFIG_H 1
#define LOGGER_CONFIG_NAME "logger"
extern struct t_config_file *logger_config_file;
extern struct t_config_option *logger_config_look_backlog;
extern struct t_config_option *logger_config_file_auto_log;
extern struct t_config_option *logger_config_file_name_lower_case;
extern struct t_config_option *logger_config_file_path;
extern struct t_config_option *logger_config_file_info_lines;
extern struct t_config_option *logger_config_file_time_format;
extern int logger_config_init ();
extern int logger_config_read ();
extern int logger_config_write ();
extern void logger_config_free ();
#endif /* logger-config.h */
+39 -154
View File
@@ -37,6 +37,7 @@
#include "../weechat-plugin.h"
#include "logger.h"
#include "logger-buffer.h"
#include "logger-config.h"
#include "logger-info.h"
#include "logger-tail.h"
@@ -50,145 +51,13 @@ WEECHAT_PLUGIN_LICENSE("GPL3");
struct t_weechat_plugin *weechat_logger_plugin = NULL;
#define LOGGER_OPTION_AUTO_LOG "auto_log"
#define LOGGER_OPTION_PATH "path"
#define LOGGER_OPTION_NAME_LOWER_CASE "name_lower_case"
#define LOGGER_OPTION_TIME_FORMAT "time_format"
#define LOGGER_OPTION_INFO_LINES "info_lines"
#define LOGGER_OPTION_BACKLOG "backlog"
#define LOGGER_DEFAULT_OPTION_AUTO_LOG "on"
#define LOGGER_DEFAULT_OPTION_PATH "%h/logs/"
#define LOGGER_DEFAULT_OPTION_NAME_LOWER_CASE "on"
#define LOGGER_DEFAULT_OPTION_TIME_FORMAT "%Y-%m-%d %H:%M:%S"
#define LOGGER_DEFAULT_OPTION_INFO_LINES "off"
#define LOGGER_DEFAULT_OPTION_BACKLOG "20"
int logger_option_auto_log = 0;
char *logger_option_path = NULL;
int logger_option_name_lower_case = 0;
char *logger_option_time_format = NULL;
int logger_option_info_lines = 0;
int logger_option_backlog = 0;
char *logger_buf_write = NULL; /* buffer for writing a line */
/*
* logger_config_read: read config options for logger plugin
* return: 1 if ok
* 0 if error
*/
int
logger_config_read ()
{
long number;
char *string, *error;
int old_auto_log;
/* option "auto_log" */
old_auto_log = logger_option_auto_log;
string = weechat_config_get_plugin (LOGGER_OPTION_AUTO_LOG);
if (!string)
{
weechat_config_set_plugin (LOGGER_OPTION_AUTO_LOG,
LOGGER_DEFAULT_OPTION_AUTO_LOG);
string = weechat_config_get_plugin (LOGGER_OPTION_AUTO_LOG);
}
if (string && (weechat_config_string_to_boolean (string) > 0))
logger_option_auto_log = 1;
else
logger_option_auto_log = 0;
/* option "path" */
if (logger_option_path)
free (logger_option_path);
logger_option_path = weechat_config_get_plugin (LOGGER_OPTION_PATH);
if (!logger_option_path)
{
weechat_config_set_plugin (LOGGER_OPTION_PATH,
LOGGER_DEFAULT_OPTION_PATH);
logger_option_path = weechat_config_get_plugin ("path");
}
if (logger_option_path)
logger_option_path = strdup (logger_option_path);
/* option "name_lower_case" */
string = weechat_config_get_plugin (LOGGER_OPTION_NAME_LOWER_CASE);
if (!string)
{
weechat_config_set_plugin (LOGGER_OPTION_NAME_LOWER_CASE,
LOGGER_DEFAULT_OPTION_NAME_LOWER_CASE);
string = weechat_config_get_plugin (LOGGER_OPTION_NAME_LOWER_CASE);
}
if (string && (weechat_config_string_to_boolean (string) > 0))
logger_option_name_lower_case = 1;
else
logger_option_name_lower_case = 0;
/* option "time_format" */
if (logger_option_time_format)
free (logger_option_time_format);
logger_option_time_format = weechat_config_get_plugin (LOGGER_OPTION_TIME_FORMAT);
if (!logger_option_time_format)
{
weechat_config_set_plugin (LOGGER_OPTION_TIME_FORMAT,
LOGGER_DEFAULT_OPTION_TIME_FORMAT);
logger_option_time_format = weechat_config_get_plugin (LOGGER_OPTION_TIME_FORMAT);
}
if (logger_option_time_format)
logger_option_time_format = strdup (logger_option_time_format);
/* option "info_lines" */
string = weechat_config_get_plugin (LOGGER_OPTION_INFO_LINES);
if (!string)
{
weechat_config_set_plugin (LOGGER_OPTION_INFO_LINES,
LOGGER_DEFAULT_OPTION_INFO_LINES);
string = weechat_config_get_plugin (LOGGER_OPTION_INFO_LINES);
}
if (string && (weechat_config_string_to_boolean (string) > 0))
logger_option_info_lines = 1;
else
logger_option_info_lines = 0;
/* option "backlog" */
string = weechat_config_get_plugin (LOGGER_OPTION_BACKLOG);
if (!string)
{
weechat_config_set_plugin (LOGGER_OPTION_BACKLOG,
LOGGER_DEFAULT_OPTION_BACKLOG);
string = weechat_config_get_plugin (LOGGER_OPTION_BACKLOG);
}
logger_option_backlog = 20;
if (string)
{
error = NULL;
number = strtol (string, &error, 10);
if (error && !error[0])
logger_option_backlog = number;
}
/* start/stop logging for all buffers if option "auto_log" is changed */
if (old_auto_log != logger_option_auto_log)
{
if (logger_option_auto_log)
logger_start_buffer_all ();
else
logger_stop_all ();
}
/* return 1 (ok) if path and time format are defined */
if (logger_option_path && logger_option_time_format)
return 1;
else
return 0;
}
/*
* logger_create_directory: create logger directory
* return 1 if success, 0 if failed
* return 1 if success (directory created or already
* exists), 0 if failed
*/
int
@@ -199,7 +68,8 @@ logger_create_directory ()
rc = 1;
dir1 = weechat_string_replace (logger_option_path, "~", getenv ("HOME"));
dir1 = weechat_string_replace (weechat_config_string (logger_config_file_path),
"~", getenv ("HOME"));
if (dir1)
{
weechat_dir = weechat_info_get ("weechat_dir", "");
@@ -247,8 +117,8 @@ logger_get_filename (struct t_gui_buffer *buffer)
dir_separator = weechat_info_get ("dir_separator", "");
weechat_dir = weechat_info_get ("weechat_dir", "");
log_path = weechat_string_replace (logger_option_path, "~",
getenv ("HOME"));
log_path = weechat_string_replace (weechat_config_string (logger_config_file_path),
"~", getenv ("HOME"));
log_path2 = weechat_string_replace (log_path, "%h", weechat_dir);
if (dir_separator && weechat_dir && log_path && log_path2)
@@ -278,14 +148,14 @@ logger_get_filename (struct t_gui_buffer *buffer)
strcpy (res, log_path2);
if (plugin_name2)
{
if (logger_option_name_lower_case)
if (weechat_config_boolean (logger_config_file_name_lower_case))
weechat_string_tolower (plugin_name2);
strcat (res, plugin_name2);
strcat (res, ".");
}
if (name2)
{
if (logger_option_name_lower_case)
if (weechat_config_boolean (logger_config_file_name_lower_case))
weechat_string_tolower (name2);
strcat (res, name2);
strcat (res, ".");
@@ -331,6 +201,17 @@ logger_write_line (struct t_logger_buffer *logger_buffer,
if (!logger_buffer->log_file)
{
if (!logger_create_directory ())
{
weechat_printf (NULL,
_("%s%s: unable to create directory for logs "
"(\"%s\")"),
weechat_prefix ("error"), LOGGER_PLUGIN_NAME,
weechat_config_string (logger_config_file_path));
free (logger_buffer->log_filename);
logger_buffer->log_filename = NULL;
return;
}
logger_buffer->log_file =
fopen (logger_buffer->log_filename, "a");
if (!logger_buffer->log_file)
@@ -344,14 +225,15 @@ logger_write_line (struct t_logger_buffer *logger_buffer,
return;
}
if (logger_option_info_lines)
if (weechat_config_boolean (logger_config_file_info_lines))
{
seconds = time (NULL);
date_tmp = localtime (&seconds);
buf_time[0] = '\0';
if (date_tmp)
strftime (buf_time, sizeof (buf_time) - 1,
logger_option_time_format, date_tmp);
weechat_config_string (logger_config_file_time_format),
date_tmp);
snprintf (logger_buf_write, LOGGER_BUF_WRITE_SIZE,
_("%s\t**** Beginning of log ****"),
buf_time);
@@ -389,7 +271,7 @@ logger_start_buffer (struct t_gui_buffer *buffer)
struct t_logger_buffer *ptr_logger_buffer;
char *log_filename;
if (!buffer || !logger_option_auto_log)
if (!buffer || !weechat_config_boolean (logger_config_file_auto_log))
return;
ptr_logger_buffer = logger_buffer_search (buffer);
@@ -451,14 +333,15 @@ logger_stop (struct t_logger_buffer *logger_buffer, int write_info_line)
if (logger_buffer->log_file)
{
if (write_info_line && logger_option_info_lines)
if (write_info_line && weechat_config_boolean (logger_config_file_info_lines))
{
seconds = time (NULL);
date_tmp = localtime (&seconds);
buf_time[0] = '\0';
if (date_tmp)
strftime (buf_time, sizeof (buf_time) - 1,
logger_option_time_format, date_tmp);
weechat_config_string (logger_config_file_time_format),
date_tmp);
logger_write_line (logger_buffer,
_("%s\t**** End of log ****"),
buf_time);
@@ -544,7 +427,8 @@ logger_backlog (struct t_gui_buffer *buffer, const char *filename, int lines)
if (pos_message)
{
pos_message[0] = '\0';
error = strptime (ptr_lines->data, logger_option_time_format,
error = strptime (ptr_lines->data,
weechat_config_string (logger_config_file_time_format),
&tm_line);
if (error && !error[0])
datetime = mktime (&tm_line);
@@ -587,7 +471,7 @@ logger_backlog_signal_cb (void *data, const char *signal,
(void) signal;
(void) type_data;
if (logger_option_backlog >= 0)
if (weechat_config_integer (logger_config_look_backlog) >= 0)
{
ptr_logger_buffer = logger_buffer_search (signal_data);
if (ptr_logger_buffer && ptr_logger_buffer->log_filename
@@ -597,7 +481,7 @@ logger_backlog_signal_cb (void *data, const char *signal,
logger_backlog (signal_data,
ptr_logger_buffer->log_filename,
logger_option_backlog);
weechat_config_integer (logger_config_look_backlog));
ptr_logger_buffer->log_enabled = 1;
}
@@ -674,7 +558,8 @@ logger_print_cb (void *data, struct t_gui_buffer *buffer, time_t date,
if (date_tmp)
{
strftime (buf_time, sizeof (buf_time) - 1,
logger_option_time_format, date_tmp);
weechat_config_string (logger_config_file_time_format),
date_tmp);
}
logger_write_line (ptr_logger_buffer,
@@ -717,9 +602,10 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
weechat_plugin = plugin;
if (!logger_config_read ())
if (!logger_config_init ())
return WEECHAT_RC_ERROR;
if (!logger_create_directory ())
if (logger_config_read () < 0)
return WEECHAT_RC_ERROR;
logger_start_buffer_all ();
@@ -749,12 +635,11 @@ weechat_plugin_end (struct t_weechat_plugin *plugin)
/* make C compiler happy */
(void) plugin;
logger_config_write ();
logger_stop_all ();
if (logger_option_path)
free (logger_option_path);
if (logger_option_time_format)
free (logger_option_time_format);
logger_config_free ();
if (logger_buf_write)
free (logger_buf_write);