summaryrefslogtreecommitdiff
path: root/src/plugins/logger/logger.c
blob: 950ba350e71763975b4b050427e307db216b7152 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Copyright (c) 2003-2007 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.c: Logger plugin for WeeChat */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#include "../weechat-plugin.h"
#include "logger-buffer.h"


char plugin_name[] = "logger";
char plugin_version[]     = "0.1";
char plugin_description[] = "Logger plugin for WeeChat";

struct t_weechat_plugin *weechat_logger_plugin = NULL;
#define weechat_plugin weechat_logger_plugin

static char *logger_path = NULL;
static char *logger_time_format = NULL;


/*
 * logger_config_read: read config options for logger plugin
 */

static void
logger_config_read ()
{
    if (logger_path)
        free (logger_path);
    logger_path = weechat_plugin_config_get ("path");
    if (!logger_path)
    {
        weechat_plugin_config_set ("path", "%h/logs/");
        logger_path = weechat_plugin_config_get ("path");
    }

    if (logger_time_format)
        free (logger_time_format);
    logger_time_format = weechat_plugin_config_get ("time_format");
    if (!logger_time_format)
    {
        weechat_plugin_config_set ("time_format", "%Y %b %d %H:%M:%S");
        logger_time_format = weechat_plugin_config_get ("time_format");
    }
}

/*
 * logger_create_directory: create logger directory
 *                          return 1 if success, 0 if failed
 */

static int
logger_create_directory ()
{
    int rc;
    char *dir1, *dir2, *weechat_dir;
    
    rc = 1;
    
    dir1 = weechat_string_replace (logger_path, "~", getenv ("HOME"));
    if (dir1)
    {
        weechat_dir = weechat_info_get ("weechat_dir");
        if (weechat_dir)
        {
            dir2 = weechat_string_replace (dir1, "%h", weechat_dir);
            if (dir2)
            {
                if (mkdir (dir2, 0755) < 0)
                {
                    if (errno != EEXIST)
                        rc = 0;
                }
                else
                    chmod (dir2, 0700);
                free (dir2);
            }
            else
                rc = 0;
            free (weechat_dir);
        }
        else
            rc = 0;
        free (dir1);
    }
    else
        rc = 0;
    
    return rc;
}

/*
 * logger_get_filename: build log filename for a buffer
 */

static char *
logger_get_filename (void *buffer)
{
    struct t_plugin_infolist *ptr_infolist;
    char *res;
    char *dir_separator, *weechat_dir, *log_path, *log_path2;
    char *category, *category2, *name, *name2;
    int length;
    
    res = NULL;
    
    dir_separator = weechat_info_get ("dir_separator");
    weechat_dir = weechat_info_get ("weechat_dir");
    log_path = weechat_string_replace (logger_path, "~", getenv ("HOME"));
    log_path2 = weechat_string_replace (log_path, "%h", weechat_dir);
    
    if (dir_separator && weechat_dir && log_path && log_path2)
    {
        ptr_infolist = weechat_infolist_get ("buffer", buffer);
        if (ptr_infolist)
        {
            category2 = NULL;
            name2 = NULL;
            if (weechat_infolist_next (ptr_infolist))
            {
                category = weechat_infolist_string (ptr_infolist, "category");
                category2 = (category) ?
                    weechat_string_replace (category, dir_separator, "_") : NULL;
                name = weechat_infolist_string (ptr_infolist, "name");
                name2 = (name) ?
                    weechat_string_replace (name, dir_separator, "_") : NULL;
            }
            length = strlen (log_path2);
            if (category2)
                length += strlen (category2);
            if (name2)
                length += strlen (name2);
            length += 16;
            res = (char *)malloc (length);
            if (res)
            {
                strcpy (res, log_path2);
                if (category2)
                {
                    strcat (res, category2);
                    strcat (res, ".");
                }
                if (name2)
                {
                    strcat (res, name2);
                    strcat (res, ".");
                }
                strcat (res, "weechatlog");
            }
            if (category2)
                free (category2);
            if (name2)
                free (name2);
            weechat_infolist_free (ptr_infolist);
        }
    }
    
    if (dir_separator)
        free (dir_separator);
    if (weechat_dir)
        free (weechat_dir);
    if (log_path)
        free (log_path);
    if (log_path2)
        free (log_path2);
    
    return res;
}

/*
 * logger_write_line: write a line to log file
 */

static void
logger_write_line (struct t_logger_buffer *logger_buffer, char *format, ...)
{
    va_list argptr;
    char buf[4096], *charset, *message;
    time_t seconds;
    struct tm *date_tmp;
    char buf_time[256];
    
    if (logger_buffer->log_filename)
    {
        charset = weechat_info_get ("charset_terminal");
        
        if (!logger_buffer->log_file)
        {
            logger_buffer->log_file =
                fopen (logger_buffer->log_filename, "a");
            if (!logger_buffer->log_file)
            {
                weechat_printf (NULL,
                                _("%sLogger: unable to write log file \"%s\"\n"),
                                weechat_prefix ("error"),
                                logger_buffer->log_filename);
                free (logger_buffer->log_filename);
                logger_buffer->log_filename = NULL;
                if (charset)
                    free (charset);
                return;
            }
            
            seconds = time (NULL);
            date_tmp = localtime (&seconds);
            buf_time[0] = '\0';
            if (date_tmp)
                strftime (buf_time, sizeof (buf_time) - 1,
                          logger_time_format, date_tmp);
            snprintf (buf, sizeof (buf) - 1,
                      _("****  Beginning of log  %s  ****"),
                      buf_time);
            message = (charset) ?
                weechat_iconv_from_internal (charset, buf) : NULL;
            fprintf (logger_buffer->log_file,
                     "%s\n", (message) ? message : buf);
            if (message)
                free (message);
        }
        
        va_start (argptr, format);
        vsnprintf (buf, sizeof (buf) - 1, format, argptr);
        va_end (argptr);
        
        message = (charset) ?
            weechat_iconv_from_internal (charset, buf) : NULL;
        
        fprintf (logger_buffer->log_file,
                 "%s\n", (message) ? message : buf);
        fflush (logger_buffer->log_file);
        if (message)
            free (message);
        if (charset)
            free (charset);
    }
}

/*
 * logger_start_buffer: start a log for a buffer
 */

static void
logger_start_buffer (void *buffer)
{
    struct t_logger_buffer *ptr_logger_buffer;
    char *log_filename;
    
    if (!buffer)
        return;
    
    ptr_logger_buffer = logger_buffer_search (buffer);
    if (!ptr_logger_buffer)
    {
        log_filename = logger_get_filename (buffer);
        if (!log_filename)
            return;
        ptr_logger_buffer = logger_buffer_add (buffer, log_filename);
        free (log_filename);
    }
    if (ptr_logger_buffer)
    {
        if (ptr_logger_buffer->log_filename)
        {
            if (ptr_logger_buffer->log_file)
            {
                fclose (ptr_logger_buffer->log_file);
                ptr_logger_buffer->log_file = NULL;
            }
        }
    }
}

/*
 * logger_start_buffer_all: start log buffer for all buffers
 */

static void
logger_start_buffer_all ()
{
    struct t_plugin_infolist *ptr_infolist;
    
    ptr_infolist = weechat_infolist_get ("buffer", NULL);
    while (weechat_infolist_next (ptr_infolist))
    {
        logger_start_buffer (weechat_infolist_pointer (ptr_infolist,
                                                       "pointer"));
    }
}

/*
 * logger_end: end log for a logger buffer
 */

static void
logger_end (struct t_logger_buffer *logger_buffer)
{
    time_t seconds;
    struct tm *date_tmp;
    char buf_time[256];
    
    if (!logger_buffer)
        return;
    
    if (logger_buffer->log_file)
    {
        seconds = time (NULL);
        date_tmp = localtime (&seconds);
        buf_time[0] = '\0';
        if (date_tmp)
            strftime (buf_time, sizeof (buf_time) - 1,
                      logger_time_format, date_tmp);
        logger_write_line (logger_buffer,
                           _("****  End of log  %s  ****"),
                           buf_time);
        fclose (logger_buffer->log_file);
        logger_buffer->log_file = NULL;
        logger_buffer_remove (logger_buffer);
    }
}

/*
 * logger_end_all: end log for all buffers
 */

static void
logger_end_all ()
{
    struct t_logger_buffer *ptr_logger_buffer;

    for (ptr_logger_buffer = logger_buffers; ptr_logger_buffer;
         ptr_logger_buffer = ptr_logger_buffer->next_buffer)
    {
        logger_end (ptr_logger_buffer);
    }
}

/*
 * logger_event_cb: callback for event hook
 */

static int
logger_event_cb (void *data, char *event, void *pointer)
{
    /* make C compiler happy */
    (void) data;
    (void) pointer;
    
    if (weechat_strcasecmp (event, "buffer_open") == 0)
    {
        logger_start_buffer (pointer);
    }
    else if (weechat_strcasecmp (event, "buffer_close") == 0)
    {
        logger_end (logger_buffer_search (pointer));
    }
    
    return PLUGIN_RC_SUCCESS;
}

/*
 * logger_print_cb: callback for print hook
 */

static int
logger_print_cb (void *data, void *buffer, time_t date, char *prefix,
                 char *message)
{
    struct t_logger_buffer *ptr_logger_buffer;
    struct tm *date_tmp;
    char buf_time[256];
    
    /* make C compiler happy */
    (void) data;

    ptr_logger_buffer = logger_buffer_search (buffer);
    if (ptr_logger_buffer && ptr_logger_buffer->log_filename)
    {
        date_tmp = localtime (&date);
        buf_time[0] = '\0';
        if (date_tmp)
            strftime (buf_time, sizeof (buf_time) - 1,
                      logger_time_format, date_tmp);
        
        logger_write_line (ptr_logger_buffer,
                           "%s%s%s%s%s",
                           buf_time,
                           (buf_time[0]) ? "  " : "",
                           (prefix) ? prefix : "",
                           (prefix && prefix[0]) ? " " : "",
                           message);
    }
    
    return PLUGIN_RC_SUCCESS;
}

/*
 * weechat_plugin_init: initialize logger plugin
 */

int
weechat_plugin_init (struct t_weechat_plugin *plugin)
{
    weechat_plugin = plugin;
    
    logger_config_read ();
    if (!logger_path || !logger_time_format)
        return PLUGIN_RC_FAILED;
    if (!logger_create_directory ())
        return PLUGIN_RC_FAILED;
    
    logger_start_buffer_all ();
    
    weechat_hook_event ("buffer_open", logger_event_cb, NULL);
    weechat_hook_event ("buffer_close", logger_event_cb, NULL);
    
    weechat_hook_print (NULL, NULL, 1, logger_print_cb, NULL);
    
    return PLUGIN_RC_SUCCESS;
}

/*
 * weechat_plugin_end: end logger plugin
 */

int
weechat_plugin_end ()
{
    logger_end_all ();
    
    return PLUGIN_RC_SUCCESS;
}