1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-04 08:43:13 +02:00

logger: move logger backlog functions to logger-backlog.c

This commit is contained in:
Sébastien Helleu
2019-09-26 21:26:23 +02:00
parent 8ab6422520
commit 3f33b327b1
11 changed files with 271 additions and 185 deletions
+1
View File
@@ -297,6 +297,7 @@ WeeChat "core" is located in following directories:
|       weechat-js-v8.cpp | JavaScript v8 functions.
|    logger/ | Logger plugin.
|       logger.c | Main logger functions.
|       logger-backlog.c | Logger backlog functions.
|       logger-buffer.c | Logger buffer list management.
|       logger-command.c | Logger commands.
|       logger-config.c | Logger config options (file logger.conf).
+1
View File
@@ -299,6 +299,7 @@ Le cœur de WeeChat est situé dans les répertoires suivants :
|       weechat-js-v8.cpp | Fonctions JavaScript v8.
|    logger/ | Extension Logger.
|       logger.c | Fonctions principales pour Logger.
|       logger-backlog.c | Fonctions de backlog pour Logger.
|       logger-buffer.c | Gestion des listes de tampons pour Logger.
|       logger-command.c | Commandes de Logger.
|       logger-config.c | Options de configuration pour Logger (fichier logger.conf).
+2
View File
@@ -304,6 +304,8 @@ WeeChat "core" は以下のディレクトリに配置されています:
|       weechat-js-v8.cpp | JavaScript v8 関数
|    logger/ | logger プラグイン
|       logger.c | logger の主要関数
// TRANSLATION MISSING
|       logger-backlog.c | Logger backlog functions.
|       logger-buffer.c | logger バッファリスト管理
|       logger-command.c | logger コマンド
|       logger-config.c | logger 設定オプション (logger.conf ファイル)
+2
View File
@@ -257,6 +257,8 @@
./src/plugins/javascript/weechat-js-v8.h
./src/plugins/javascript/weechat-js.cpp
./src/plugins/javascript/weechat-js.h
./src/plugins/logger/logger-backlog.c
./src/plugins/logger/logger-backlog.h
./src/plugins/logger/logger-buffer.c
./src/plugins/logger/logger-buffer.h
./src/plugins/logger/logger.c
+2
View File
@@ -258,6 +258,8 @@ SET(WEECHAT_SOURCES
./src/plugins/javascript/weechat-js-v8.h
./src/plugins/javascript/weechat-js.cpp
./src/plugins/javascript/weechat-js.h
./src/plugins/logger/logger-backlog.c
./src/plugins/logger/logger-backlog.h
./src/plugins/logger/logger-buffer.c
./src/plugins/logger/logger-buffer.h
./src/plugins/logger/logger.c
+1
View File
@@ -19,6 +19,7 @@
add_library(logger MODULE
logger.c logger.h
logger-backlog.c logger-backlog.h
logger-buffer.c logger-buffer.h
logger-command.c logger-command.h
logger-config.c logger-config.h
+2
View File
@@ -25,6 +25,8 @@ lib_LTLIBRARIES = logger.la
logger_la_SOURCES = logger.c \
logger.h \
logger-backlog.c \
logger-backlog.h \
logger-buffer.c \
logger-buffer.h \
logger-command.c \
+227
View File
@@ -0,0 +1,227 @@
/*
* logger-backlog.c - display backlog of messages
*
* Copyright (C) 2003-2019 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat 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.
*
* WeeChat 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 WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
/* this define is needed for strptime() (not on OpenBSD/Sun) */
#if !defined(__OpenBSD__) && !defined(__sun)
#define _XOPEN_SOURCE 700
#endif
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <time.h>
#include "../weechat-plugin.h"
#include "logger.h"
#include "logger-buffer.h"
#include "logger-command.h"
#include "logger-config.h"
#include "logger-info.h"
#include "logger-tail.h"
/*
* Checks conditions to display the backlog.
*
* Returns:
* 1: conditions OK (backlog is displayed)
* 0: conditions not OK (backlog is NOT displayed)
*/
int
logger_backlog_check_conditions (struct t_gui_buffer *buffer)
{
struct t_hashtable *pointers, *options;
const char *ptr_condition;
char *result;
int condition_ok;
ptr_condition = weechat_config_string (logger_config_look_backlog_conditions);
/* empty condition displays the backlog everywhere */
if (!ptr_condition || !ptr_condition[0])
return 1;
pointers = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL,
NULL);
if (pointers)
{
weechat_hashtable_set (pointers, "window",
weechat_window_search_with_buffer (buffer));
weechat_hashtable_set (pointers, "buffer", buffer);
}
options = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
if (options)
weechat_hashtable_set (options, "type", "condition");
result = weechat_string_eval_expression (ptr_condition,
pointers, NULL, options);
condition_ok = (result && (strcmp (result, "1") == 0));
if (result)
free (result);
if (pointers)
weechat_hashtable_free (pointers);
if (options)
weechat_hashtable_free (options);
return condition_ok;
}
/*
* Displays backlog for a buffer (by reading end of log file).
*/
void
logger_backlog (struct t_gui_buffer *buffer, const char *filename, int lines)
{
struct t_logger_line *last_lines, *ptr_lines;
char *charset, *pos_message, *pos_tab, *error, *message;
time_t datetime, time_now;
struct tm tm_line;
int num_lines;
weechat_buffer_set (buffer, "print_hooks_enabled", "0");
num_lines = 0;
last_lines = logger_tail_file (filename, lines);
ptr_lines = last_lines;
while (ptr_lines)
{
datetime = 0;
pos_message = strchr (ptr_lines->data, '\t');
if (pos_message)
{
/* initialize structure, because strptime does not do it */
memset (&tm_line, 0, sizeof (struct tm));
/*
* we get current time to initialize daylight saving time in
* structure tm_line, otherwise printed time will be shifted
* and will not use DST used on machine
*/
time_now = time (NULL);
localtime_r (&time_now, &tm_line);
pos_message[0] = '\0';
error = strptime (ptr_lines->data,
weechat_config_string (logger_config_file_time_format),
&tm_line);
if (error && !error[0] && (tm_line.tm_year > 0))
datetime = mktime (&tm_line);
pos_message[0] = '\t';
}
pos_message = (pos_message && (datetime != 0)) ?
pos_message + 1 : ptr_lines->data;
charset = weechat_info_get ("charset_terminal", "");
message = (charset) ?
weechat_iconv_to_internal (charset, pos_message) : strdup (pos_message);
if (charset)
free (charset);
if (message)
{
pos_tab = strchr (message, '\t');
if (pos_tab)
pos_tab[0] = '\0';
weechat_printf_date_tags (buffer, datetime,
"no_highlight,notify_none,logger_backlog",
"%s%s%s%s%s",
weechat_color (weechat_config_string (logger_config_color_backlog_line)),
message,
(pos_tab) ? "\t" : "",
(pos_tab) ? weechat_color (weechat_config_string (logger_config_color_backlog_line)) : "",
(pos_tab) ? pos_tab + 1 : "");
if (pos_tab)
pos_tab[0] = '\t';
free (message);
}
num_lines++;
ptr_lines = ptr_lines->next_line;
}
if (last_lines)
logger_tail_free (last_lines);
if (num_lines > 0)
{
weechat_printf_date_tags (buffer, datetime,
"no_highlight,notify_none,logger_backlog_end",
_("%s===\t%s========== End of backlog (%d lines) =========="),
weechat_color (weechat_config_string (logger_config_color_backlog_end)),
weechat_color (weechat_config_string (logger_config_color_backlog_end)),
num_lines);
weechat_buffer_set (buffer, "unread", "");
}
weechat_buffer_set (buffer, "print_hooks_enabled", "1");
}
/*
* Callback for signal "logger_backlog".
*/
int
logger_backlog_signal_cb (const void *pointer, void *data,
const char *signal,
const char *type_data, void *signal_data)
{
struct t_logger_buffer *ptr_logger_buffer;
/* make C compiler happy */
(void) pointer;
(void) data;
(void) signal;
(void) type_data;
if (weechat_config_integer (logger_config_look_backlog) == 0)
return WEECHAT_RC_OK;
if (!logger_backlog_check_conditions (signal_data))
return WEECHAT_RC_OK;
ptr_logger_buffer = logger_buffer_search_buffer (signal_data);
if (ptr_logger_buffer && ptr_logger_buffer->log_enabled)
{
if (!ptr_logger_buffer->log_filename)
logger_set_log_filename (ptr_logger_buffer);
if (ptr_logger_buffer->log_filename)
{
ptr_logger_buffer->log_enabled = 0;
logger_backlog (signal_data,
ptr_logger_buffer->log_filename,
weechat_config_integer (logger_config_look_backlog));
ptr_logger_buffer->log_enabled = 1;
}
}
return WEECHAT_RC_OK;
}
+30
View File
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2003-2019 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat 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.
*
* WeeChat 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 WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef WEECHAT_PLUGIN_LOGGER_BACKLOG_H
#define WEECHAT_PLUGIN_LOGGER_BACKLOG_H
extern int logger_backlog_check_conditions (struct t_gui_buffer *buffer);
extern void logger_backlog (struct t_gui_buffer *buffer, const char *filename,
int lines);
extern int logger_backlog_signal_cb (const void *pointer, void *data,
const char *signal,
const char *type_data, void *signal_data);
#endif /* WEECHAT_PLUGIN_LOGGER_BACKLOG_H */
+1 -185
View File
@@ -19,11 +19,6 @@
* along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
/* this define is needed for strptime() (not on OpenBSD/Sun) */
#if !defined(__OpenBSD__) && !defined(__sun)
#define _XOPEN_SOURCE 700
#endif
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
@@ -39,6 +34,7 @@
#include "../weechat-plugin.h"
#include "logger.h"
#include "logger-backlog.h"
#include "logger-buffer.h"
#include "logger-command.h"
#include "logger-config.h"
@@ -840,186 +836,6 @@ logger_buffer_renamed_signal_cb (const void *pointer, void *data,
return WEECHAT_RC_OK;
}
/*
* Checks conditions to display the backlog.
*
* Returns:
* 1: conditions OK (backlog is displayed)
* 0: conditions not OK (backlog is NOT displayed)
*/
int
logger_backlog_check_conditions (struct t_gui_buffer *buffer)
{
struct t_hashtable *pointers, *options;
const char *ptr_condition;
char *result;
int condition_ok;
ptr_condition = weechat_config_string (logger_config_look_backlog_conditions);
/* empty condition displays the backlog everywhere */
if (!ptr_condition || !ptr_condition[0])
return 1;
pointers = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL,
NULL);
if (pointers)
{
weechat_hashtable_set (pointers, "window",
weechat_window_search_with_buffer (buffer));
weechat_hashtable_set (pointers, "buffer", buffer);
}
options = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
if (options)
weechat_hashtable_set (options, "type", "condition");
result = weechat_string_eval_expression (ptr_condition,
pointers, NULL, options);
condition_ok = (result && (strcmp (result, "1") == 0));
if (result)
free (result);
if (pointers)
weechat_hashtable_free (pointers);
if (options)
weechat_hashtable_free (options);
return condition_ok;
}
/*
* Displays backlog for a buffer (by reading end of log file).
*/
void
logger_backlog (struct t_gui_buffer *buffer, const char *filename, int lines)
{
struct t_logger_line *last_lines, *ptr_lines;
char *charset, *pos_message, *pos_tab, *error, *message;
time_t datetime, time_now;
struct tm tm_line;
int num_lines;
weechat_buffer_set (buffer, "print_hooks_enabled", "0");
num_lines = 0;
last_lines = logger_tail_file (filename, lines);
ptr_lines = last_lines;
while (ptr_lines)
{
datetime = 0;
pos_message = strchr (ptr_lines->data, '\t');
if (pos_message)
{
/* initialize structure, because strptime does not do it */
memset (&tm_line, 0, sizeof (struct tm));
/*
* we get current time to initialize daylight saving time in
* structure tm_line, otherwise printed time will be shifted
* and will not use DST used on machine
*/
time_now = time (NULL);
localtime_r (&time_now, &tm_line);
pos_message[0] = '\0';
error = strptime (ptr_lines->data,
weechat_config_string (logger_config_file_time_format),
&tm_line);
if (error && !error[0] && (tm_line.tm_year > 0))
datetime = mktime (&tm_line);
pos_message[0] = '\t';
}
pos_message = (pos_message && (datetime != 0)) ?
pos_message + 1 : ptr_lines->data;
charset = weechat_info_get ("charset_terminal", "");
message = (charset) ?
weechat_iconv_to_internal (charset, pos_message) : strdup (pos_message);
if (charset)
free (charset);
if (message)
{
pos_tab = strchr (message, '\t');
if (pos_tab)
pos_tab[0] = '\0';
weechat_printf_date_tags (buffer, datetime,
"no_highlight,notify_none,logger_backlog",
"%s%s%s%s%s",
weechat_color (weechat_config_string (logger_config_color_backlog_line)),
message,
(pos_tab) ? "\t" : "",
(pos_tab) ? weechat_color (weechat_config_string (logger_config_color_backlog_line)) : "",
(pos_tab) ? pos_tab + 1 : "");
if (pos_tab)
pos_tab[0] = '\t';
free (message);
}
num_lines++;
ptr_lines = ptr_lines->next_line;
}
if (last_lines)
logger_tail_free (last_lines);
if (num_lines > 0)
{
weechat_printf_date_tags (buffer, datetime,
"no_highlight,notify_none,logger_backlog_end",
_("%s===\t%s========== End of backlog (%d lines) =========="),
weechat_color (weechat_config_string (logger_config_color_backlog_end)),
weechat_color (weechat_config_string (logger_config_color_backlog_end)),
num_lines);
weechat_buffer_set (buffer, "unread", "");
}
weechat_buffer_set (buffer, "print_hooks_enabled", "1");
}
/*
* Callback for signal "logger_backlog".
*/
int
logger_backlog_signal_cb (const void *pointer, void *data,
const char *signal,
const char *type_data, void *signal_data)
{
struct t_logger_buffer *ptr_logger_buffer;
/* make C compiler happy */
(void) pointer;
(void) data;
(void) signal;
(void) type_data;
if (weechat_config_integer (logger_config_look_backlog) == 0)
return WEECHAT_RC_OK;
if (!logger_backlog_check_conditions (signal_data))
return WEECHAT_RC_OK;
ptr_logger_buffer = logger_buffer_search_buffer (signal_data);
if (ptr_logger_buffer && ptr_logger_buffer->log_enabled)
{
if (!ptr_logger_buffer->log_filename)
logger_set_log_filename (ptr_logger_buffer);
if (ptr_logger_buffer->log_filename)
{
ptr_logger_buffer->log_enabled = 0;
logger_backlog (signal_data,
ptr_logger_buffer->log_filename,
weechat_config_integer (logger_config_look_backlog));
ptr_logger_buffer->log_enabled = 1;
}
}
return WEECHAT_RC_OK;
}
/*
* Callback for signal "logger_start".
*/
+2
View File
@@ -26,12 +26,14 @@
#define LOGGER_LEVEL_DEFAULT 9
struct t_gui_buffer;
struct t_logger_buffer;
extern struct t_weechat_plugin *weechat_logger_plugin;
extern struct t_hook *logger_timer;
extern char *logger_build_option_name (struct t_gui_buffer *buffer);
extern void logger_set_log_filename (struct t_logger_buffer *logger_buffer);
extern void logger_start_buffer_all (int write_info_line);
extern void logger_flush ();
extern void logger_stop_all (int write_info_line);