logger.c 12.6 KB
Newer Older
1
2
3
/*****************************************************************************
 * logger.c : file logging plugin for vlc
 *****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
4
 * Copyright (C) 2002-2008 the VideoLAN team
5
 * $Id$
6
7
8
9
10
11
12
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *
 * 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.
13
 *
14
15
16
17
18
 * 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.
 *
19
20
21
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28
29
30
31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
34
35
36
37
#include <vlc_interface.h>
#include <vlc_playlist.h>
#include <vlc_charset.h>

38
#include <assert.h>
39

40
41
42
43
#ifdef UNDER_CE
#   define _IONBF 0x0004
#endif

44
45
#define MODE_TEXT 0
#define MODE_HTML 1
46
#define MODE_SYSLOG 2
47

48
#ifdef __APPLE__
hartman's avatar
hartman committed
49
50
51
52
53
54
#define LOG_DIR "Library/Logs/"
#endif

#define LOG_FILE_TEXT "vlc-log.txt"
#define LOG_FILE_HTML "vlc-log.html"

55
56
57
58
#define TEXT_HEADER "-- logger module started --\n"
#define TEXT_FOOTER "-- logger module stopped --\n"

#define HTML_HEADER \
59
60
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
    "  \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
61
62
63
    "<html>\n" \
    "  <head>\n" \
    "    <title>vlc log</title>\n" \
64
    "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" \
65
    "  </head>\n" \
66
    "  <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
67
68
69
70
71
72
73
74
    "    <pre>\n" \
    "      <b>-- logger module started --</b>\n"
#define HTML_FOOTER \
    "      <b>-- logger module stopped --</b>\n" \
    "    </pre>\n" \
    "  </body>\n" \
    "</html>\n"

75
76
77
78
#if HAVE_SYSLOG_H
#include <syslog.h>
#endif

79
80
81
82
83
84
85
struct msg_cb_data_t
{
    intf_thread_t *p_intf;
    FILE *p_file;
    int   i_mode;
};

86
87
88
89
90
91
/*****************************************************************************
 * intf_sys_t: description and status of log interface
 *****************************************************************************/
struct intf_sys_t
{
    msg_subscription_t *p_sub;
92
    msg_cb_data_t msg;
93
94
95
96
97
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
98
static int  Open    ( vlc_object_t * );
99
100
static void Close   ( vlc_object_t * );

101
static void Overflow (msg_cb_data_t *p_sys, msg_item_t *p_item, unsigned overruns);
102
103
static void TextPrint         ( const msg_item_t *, FILE * );
static void HtmlPrint         ( const msg_item_t *, FILE * );
104
105
106
#ifdef HAVE_SYSLOG_H
static void SyslogPrint       ( const msg_item_t *);
#endif
107
108
109
110

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
111
static const char *const mode_list[] = { "text", "html"
112
113
114
115
#ifdef HAVE_SYSLOG_H
,"syslog"
#endif
};
116
static const char *const mode_list_text[] = { N_("Text"), "HTML"
117
118
119
120
#ifdef HAVE_SYSLOG_H
, "syslog"
#endif
};
121

Christophe Massiot's avatar
Christophe Massiot committed
122
#define LOGMODE_TEXT N_("Log format")
123
#ifndef HAVE_SYSLOG_H
124
#define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
Christophe Mutricy's avatar
Christophe Mutricy committed
125
  "\"text\" (default) and \"html\".")
126
#else
Christophe Massiot's avatar
Christophe Massiot committed
127

128
129
130
#define LOGMODE_LONGTEXT N_("Specify the log format. Available choices are " \
  "\"text\" (default), \"html\", and \"syslog\" (special mode to send to " \
  "syslog instead of file.")
131
132
133

#define SYSLOG_FACILITY_TEXT N_("Syslog facility")
#define SYSLOG_FACILITY_LONGTEXT N_("Select the syslog facility where logs " \
134
  "will be forwarded. Available choices are \"user\" (default), \"daemon\", " \
135
  "and \"local0\" through \"local7\".")
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

/* First in list is the default facility used. */
#define DEFINE_SYSLOG_FACILITY \
  DEF( "user",   LOG_USER ), \
  DEF( "daemon", LOG_DAEMON ), \
  DEF( "local0", LOG_LOCAL0 ), \
  DEF( "local1", LOG_LOCAL1 ), \
  DEF( "local2", LOG_LOCAL2 ), \
  DEF( "local3", LOG_LOCAL3 ), \
  DEF( "local4", LOG_LOCAL4 ), \
  DEF( "local5", LOG_LOCAL5 ), \
  DEF( "local6", LOG_LOCAL6 ), \
  DEF( "local7", LOG_LOCAL7 )

#define DEF( a, b ) a
static const char *const fac_name[]   = { DEFINE_SYSLOG_FACILITY };
#undef  DEF
#define DEF( a, b ) b
static const int         fac_number[] = { DEFINE_SYSLOG_FACILITY };
#undef  DEF
enum                   { fac_entries = sizeof(fac_name)/sizeof(fac_name[0]) };
#undef  DEFINE_SYSLOG_FACILITY

159
160
#endif

161
162
163
vlc_module_begin ()
    set_shortname( N_( "Logging" ) )
    set_description( N_("File logging") )
gbazin's avatar
   
gbazin committed
164

165
166
    set_category( CAT_ADVANCED )
    set_subcategory( SUBCAT_ADVANCED_MISC )
167

168
    add_file( "logfile", NULL, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
169
             N_("Log filename"), N_("Specify the log filename."), false )
170
    add_string( "logmode", "text", NULL, LOGMODE_TEXT, LOGMODE_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
171
                false )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
172
        change_string_list( mode_list, mode_list_text, 0 )
173
#ifdef HAVE_SYSLOG_H
174
    add_string( "syslog-facility", fac_name[0], NULL, SYSLOG_FACILITY_TEXT,
175
                SYSLOG_FACILITY_LONGTEXT, true )
176
        change_string_list( fac_name, fac_name, 0 )
177
#endif
gbazin's avatar
   
gbazin committed
178

179
    add_obsolete_string( "rrd-file" )
zorglub's avatar
zorglub committed
180

181
182
183
    set_capability( "interface", 0 )
    set_callbacks( Open, Close )
vlc_module_end ()
184
185
186
187
188

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
189
{
190
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
191
    intf_sys_t *p_sys;
192
    char *psz_mode;
193

gbazin's avatar
   
gbazin committed
194
    CONSOLE_INTRO_MSG;
195
    msg_Info( p_intf, "using logger." );
196
197

    /* Allocate instance and initialize some members */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
198
199
200
    p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
    if( p_sys == NULL )
        return VLC_ENOMEM;
201

202
203
    p_sys->msg.p_intf = p_intf;
    p_sys->msg.i_mode = MODE_TEXT;
204
    psz_mode = var_CreateGetString( p_intf, "logmode" );
205
206
207
    if( psz_mode )
    {
        if( !strcmp( psz_mode, "text" ) )
208
            ;
209
210
        else if( !strcmp( psz_mode, "html" ) )
        {
211
            p_sys->msg.i_mode = MODE_HTML;
212
        }
213
214
215
#ifdef HAVE_SYSLOG_H
        else if( !strcmp( psz_mode, "syslog" ) )
        {
216
            p_sys->msg.i_mode = MODE_SYSLOG;
217
218
        }
#endif
219
220
        else
        {
221
            msg_Warn( p_intf, "invalid log mode `%s', using `text'", psz_mode );
222
            p_sys->msg.i_mode = MODE_TEXT;
223
224
225
226
227
228
229
230
        }
        free( psz_mode );
    }
    else
    {
        msg_Warn( p_intf, "no log mode specified, using `text'" );
    }

231
    if( p_sys->msg.i_mode != MODE_SYSLOG )
232
    {
233
        char *psz_file = config_GetPsz( p_intf, "logfile" );
234
235
        if( !psz_file )
        {
236
#ifdef __APPLE__
237
            if( asprintf( &psz_file, "%s/"LOG_DIR"/%s", config_GetHomeDir(),
hartman's avatar
hartman committed
238
                (p_sys->msg.i_mode == MODE_HTML) ? LOG_FILE_HTML
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
239
                                             : LOG_FILE_TEXT ) == -1 )
240
                psz_file = NULL;
241
#else
242
            switch( p_sys->msg.i_mode )
hartman's avatar
hartman committed
243
244
            {
            case MODE_HTML:
245
                psz_file = strdup( LOG_FILE_HTML );
hartman's avatar
hartman committed
246
247
248
                break;
            case MODE_TEXT:
            default:
249
                psz_file = strdup( LOG_FILE_TEXT );
hartman's avatar
hartman committed
250
251
                break;
            }
252
253
254
#endif
            msg_Warn( p_intf, "no log filename provided, using `%s'",
                               psz_file );
hartman's avatar
hartman committed
255
        }
256
257
258

        /* Open the log file and remove any buffering for the stream */
        msg_Dbg( p_intf, "opening logfile `%s'", psz_file );
259
260
        p_sys->msg.p_file = utf8_fopen( psz_file, "at" );
        if( p_sys->msg.p_file == NULL )
261
262
        {
            msg_Err( p_intf, "error opening logfile `%s'", psz_file );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
263
            free( p_sys );
264
265
266
            free( psz_file );
            return -1;
        }
267
        setvbuf( p_sys->msg.p_file, NULL, _IONBF, 0 );
268
269
270

        free( psz_file );

271
        switch( p_sys->msg.i_mode )
272
273
        {
        case MODE_HTML:
274
            fputs( HTML_HEADER, p_sys->msg.p_file );
275
276
277
            break;
        case MODE_TEXT:
        default:
278
            fputs( TEXT_HEADER, p_sys->msg.p_file );
279
280
281
282
            break;
        }

    }
283
    else
284
    {
285
        p_sys->msg.p_file = NULL;
286
#ifdef HAVE_SYSLOG_H
287
        int i_facility;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
288
        char *psz_facility = var_CreateGetString( p_intf, "syslog-facility" );
289
290
291
        if( psz_facility )
        {
            bool b_valid = 0;
292
            for( size_t i = 0; i < fac_entries; ++i )
293
            {
294
                if( !strcmp( psz_facility, fac_name[i] ) )
295
                {
296
                    i_facility = fac_number[i];
297
298
299
300
301
302
                    b_valid = 1;
                    break;
                }
            }
            if( !b_valid )
            {
303
304
305
                msg_Warn( p_intf, "invalid syslog facility `%s', using `%s'",
                          psz_facility, fac_name[0] );
                i_facility = fac_number[0];
306
307
308
309
310
            }
            free( psz_facility );
        }
        else
        {
311
312
313
            msg_Warn( p_intf, "no syslog facility specified, using `%s'",
                      fac_name[0] );
            i_facility = fac_number[0];
314
315
316
        }

        openlog( "vlc", LOG_PID|LOG_NDELAY, i_facility );
317
#endif
318
319
    }

320
    p_sys->p_sub = msg_Subscribe( p_intf->p_libvlc, Overflow, &p_sys->msg );
gbazin's avatar
   
gbazin committed
321

322
323
324
325
326
327
328
    return 0;
}

/*****************************************************************************
 * Close: destroy interface stuff
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
329
{
330
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
331
332
    intf_sys_t *p_sys = p_intf->p_sys;

333
    /* Flush the queue and unsubscribe from the message queue */
334
335
    /* FIXME: flush */
    msg_Unsubscribe( p_sys->p_sub );
336

337
    switch( p_sys->msg.i_mode )
338
339
    {
    case MODE_HTML:
340
        fputs( HTML_FOOTER, p_sys->msg.p_file );
341
        break;
342
343
344
345
346
#ifdef HAVE_SYSLOG_H
    case MODE_SYSLOG:
        closelog();
        break;
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
347
    case MODE_TEXT:
348
    default:
349
        fputs( TEXT_FOOTER, p_sys->msg.p_file );
350
351
352
353
        break;
    }

    /* Close the log file */
354
355
    if( p_sys->msg.p_file )
        fclose( p_sys->msg.p_file );
356
357

    /* Destroy structure */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
358
    free( p_sys );
359
360
}

361
362
363
364
/**
 * Log a message
 */
static void Overflow (msg_cb_data_t *p_sys, msg_item_t *p_item, unsigned overruns)
365
{
Pierre's avatar
Pierre committed
366
    VLC_UNUSED(overruns);
367
368
369
370
    int verbosity = var_CreateGetInteger( p_sys->p_intf, "verbose" );
    int priority = 0;

    switch( p_item->i_type )
371
    {
372
373
        case VLC_MSG_WARN: priority = 1; break;
        case VLC_MSG_DBG:  priority = 2; break;
374
    }
375
376
    if (verbosity < priority)
        return;
377

378
    switch( p_sys->i_mode )
379
    {
380
381
382
        case MODE_HTML:
            HtmlPrint( p_item, p_sys->p_file );
            break;
383
#ifdef HAVE_SYSLOG_H
384
385
386
        case MODE_SYSLOG:
            SyslogPrint( p_item );
            break;
387
#endif
388
389
390
391
        case MODE_TEXT:
        default:
            TextPrint( p_item, p_sys->p_file );
            break;
392
393
394
    }
}

395
396
397
398
399
400
static const char ppsz_type[4][11] = {
    ": ",
    " error: ",
    " warning: ",
    " debug: ",
};
401
402
403

static void TextPrint( const msg_item_t *p_msg, FILE *p_file )
{
404
405
    fprintf( p_file, "%s%s%s\n", p_msg->psz_module, ppsz_type[p_msg->i_type],
             p_msg->psz_msg );
406
407
}

408
409
410
#ifdef HAVE_SYSLOG_H
static void SyslogPrint( const msg_item_t *p_msg )
{
411
412
    static const int i_prio[4] = { LOG_INFO, LOG_ERR, LOG_WARNING, LOG_DEBUG };
    int i_priority = i_prio[p_msg->i_type];
413

414
    if( p_msg->psz_header )
415
416
        syslog( i_priority, "%s %s%s%s", p_msg->psz_header, p_msg->psz_module,
                ppsz_type[p_msg->i_type], p_msg->psz_msg );
417
    else
418
        syslog( i_priority, "%s%s%s", p_msg->psz_module, 
419
                ppsz_type[p_msg->i_type], p_msg->psz_msg );
420
 
421
422
423
}
#endif

424
425
static void HtmlPrint( const msg_item_t *p_msg, FILE *p_file )
{
426
427
428
429
430
431
432
433
434
435
    static const char ppsz_color[4][30] = {
        "<span style=\"color: #ffffff\">",
        "<span style=\"color: #ff6666\">",
        "<span style=\"color: #ffff66\">",
        "<span style=\"color: #aaaaaa\">",
    };

    fprintf( p_file, "%s%s%s%s</span>\n", p_msg->psz_module,
             ppsz_type[p_msg->i_type], ppsz_color[p_msg->i_type],
             p_msg->psz_msg );
436
}