summaryrefslogtreecommitdiff
path: root/src/plugins/logger/logger-backlog.c
blob: 3c9f8dd6abc6e172c3fbbbd52a49f807b82a7969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * logger-backlog.c - display backlog of messages
 *
 * Copyright (C) 2003-2023 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"


/*
 * Displays a line read from log file.
 */

void
logger_backlog_display_line (struct t_gui_buffer *buffer, const char *line)
{
    const char *pos_message;
    char *str_date, *charset, *pos_tab, *error, *message, *message2;
    time_t datetime, time_now;
    struct tm tm_line;
    int color_lines;

    if (!line)
        return;

    color_lines = weechat_config_boolean (logger_config_file_color_lines);

    datetime = 0;
    pos_message = strchr (line, '\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);
        str_date = weechat_strndup (line, pos_message - line);
        if (str_date)
        {
            error = strptime (
                str_date,
                weechat_config_string (logger_config_file_time_format),
                &tm_line);
            if (error && !error[0] && (tm_line.tm_year > 0))
                datetime = mktime (&tm_line);
            free (str_date);
        }
    }
    pos_message = (pos_message && (datetime != 0)) ?
        pos_message + 1 : line;
    message = weechat_hook_modifier_exec (
        "color_decode_ansi",
        (color_lines) ? "1" : "0",
        pos_message);
    if (message)
    {
        charset = weechat_info_get ("charset_terminal", "");
        message2 = (charset) ?
            weechat_iconv_to_internal (charset, message) : strdup (message);
        if (charset)
            free (charset);
        if (message2)
        {
            pos_tab = strchr (message2, '\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",
                (color_lines) ? "" : weechat_color (weechat_config_string (logger_config_color_backlog_line)),
                message2,
                (pos_tab) ? "\t" : "",
                (pos_tab && !color_lines) ? weechat_color (weechat_config_string (logger_config_color_backlog_line)) : "",
                (pos_tab) ? pos_tab + 1 : "");
            if (pos_tab)
                pos_tab[0] = '\t';
            free (message2);
        }
        free (message);
    }
}

/*
 * Compares two messages in arraylist.
 */

int
logger_backlog_msg_cmp_cb (void *data,
                           struct t_arraylist *arraylist,
                           void *pointer1,
                           void *pointer2)
{
    /* make C compiler happy */
    (void) data;
    (void) arraylist;

    return weechat_strcmp ((const char *)pointer1, (const char *)pointer2);
}

/*
 * Frees a message in arraylist.
 */

void
logger_backlog_msg_free_cb (void *data, struct t_arraylist *arraylist,
                            void *pointer)
{
    /* make C compiler happy */
    (void) data;
    (void) arraylist;

    free (pointer);
}

/*
 * Groups lines by messages: each line with a timestamp is considered the first
 * line of a message, and subsequent lines without timestamp are the rest of
 * the message.
 *
 * Note: result must be freed after use.
 */

struct t_arraylist *
logger_backlog_group_messages (struct t_arraylist *lines)
{
    int i, size, time_found;
    char **message, **old_message, *str_date, *error;
    const char *ptr_line, *pos_message;
    struct tm tm_line;
    struct t_arraylist *messages;

    if (!lines)
        return NULL;

    size = weechat_arraylist_size (lines);

    messages = weechat_arraylist_new (size, 0, 1,
                                      &logger_backlog_msg_cmp_cb, NULL,
                                      &logger_backlog_msg_free_cb, NULL);
    if (!messages)
        return NULL;

    message = weechat_string_dyn_alloc (256);
    old_message = weechat_string_dyn_alloc (256);

    for (i = size - 1; i >= 0; i--)
    {
        ptr_line = (const char *)weechat_arraylist_get (lines, i);

        weechat_string_dyn_copy (old_message, *message);
        weechat_string_dyn_copy (message, ptr_line);
        if ((*old_message)[0])
        {
            weechat_string_dyn_concat (message, "\n", -1);
            weechat_string_dyn_concat (message, *old_message, -1);
        }

        time_found = 0;
        pos_message = strchr (ptr_line, '\t');
        if (pos_message)
        {
            str_date = weechat_strndup (ptr_line, pos_message - ptr_line);
            if (str_date)
            {
                memset (&tm_line, 0, sizeof (struct tm));
                error = strptime (
                    str_date,
                    weechat_config_string (logger_config_file_time_format),
                    &tm_line);
                if (error && !error[0] && (tm_line.tm_year > 0))
                    time_found = 1;
                free (str_date);
            }
        }
        if (time_found)
        {
            weechat_arraylist_insert (messages, 0, strdup (*message));
            weechat_string_dyn_copy (message, NULL);
        }
    }

    if ((*message)[0])
        weechat_arraylist_insert (messages, 0, strdup (*message));

    weechat_string_dyn_free (message, 1);
    weechat_string_dyn_free (old_message, 1);

    return messages;
}

/*
 * Displays backlog for a buffer by reading end of log file.
 */

void
logger_backlog_file (struct t_gui_buffer *buffer, const char *filename,
                     int lines)
{
    struct t_arraylist *last_lines, *messages;
    int i, num_msgs, old_input_multiline;

    num_msgs = 0;
    last_lines = logger_tail_file (filename, lines);
    if (!last_lines)
        return;

    messages = logger_backlog_group_messages (last_lines);
    if (!messages)
    {
        weechat_arraylist_free (last_lines);
        return;
    }

    weechat_arraylist_free (last_lines);

    /* disable any print hook during display of backlog */
    weechat_buffer_set (buffer, "print_hooks_enabled", "0");

    /* temporary enable multiline support so we can display multiline msgs */
    old_input_multiline = weechat_buffer_get_integer (buffer, "input_multiline");
    weechat_buffer_set (buffer, "input_multiline", "1");

    num_msgs = weechat_arraylist_size (messages);
    for (i = 0; i < num_msgs; i++)
    {
        logger_backlog_display_line (
            buffer,
            (const char *)weechat_arraylist_get (messages, i));
    }
    weechat_arraylist_free (messages);

    if (num_msgs > 0)
    {
        weechat_printf_date_tags (buffer, 0,
                                  "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_msgs);
        weechat_buffer_set (buffer, "unread", "");
    }

    weechat_buffer_set (buffer, "input_multiline",
                        (old_input_multiline) ? "1" : "0");
    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;
    int rc;

    /* 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;

    rc = logger_check_conditions (
        signal_data,
        weechat_config_string (logger_config_look_backlog_conditions));
    if (!rc)
        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_buffer_set_log_filename (ptr_logger_buffer);
        if (ptr_logger_buffer->log_filename)
        {
            ptr_logger_buffer->log_enabled = 0;
            logger_backlog_file (signal_data,
                                 ptr_logger_buffer->log_filename,
                                 weechat_config_integer (logger_config_look_backlog));
            ptr_logger_buffer->log_enabled = 1;
        }
    }

    return WEECHAT_RC_OK;
}