]> git.sesse.net Git - vlc/blob - modules/logger/file.c
df6d202be856bdb65988f9a4d772ccbe411638da
[vlc] / modules / logger / file.c
1 /*****************************************************************************
2  * file.c: file logger plugin
3  *****************************************************************************
4  * Copyright (C) 2002-2008 the VideoLAN team
5  * Copyright © 2007-2015 Rémi Denis-Courmont
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_fs.h>
31 //#include <vlc_charset.h>
32
33 #include <stdarg.h>
34 #include <assert.h>
35 #include <errno.h>
36
37 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
38
39 typedef struct
40 {
41     FILE *stream;
42     const char *footer;
43     int verbosity;
44 } vlc_logger_sys_t;
45
46 #define TEXT_FILENAME "vlc-log.txt"
47 #define TEXT_HEADER "\xEF\xBB\xBF" /* UTF-8 BOM */ \
48                     "-- logger module started --\n"
49 #define TEXT_FOOTER "-- logger module stopped --\n"
50
51 static void LogText(void *opaque, int type, const vlc_log_t *meta,
52                     const char *format, va_list ap)
53 {
54     vlc_logger_sys_t *sys = opaque;
55     FILE *stream = sys->stream;
56
57     if (sys->verbosity < type)
58         return;
59
60     int canc = vlc_savecancel();
61
62     flockfile(stream);
63     fprintf(stream, "%s%s: ", meta->psz_module, msg_type[type]);
64     vfprintf(stream, format, ap);
65     putc_unlocked('\n', stream);
66     funlockfile(stream);
67
68     vlc_restorecancel(canc);
69 }
70
71 #define HTML_FILENAME "vlc-log.html"
72 #define HTML_HEADER \
73     "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
74     "  \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
75     "<html>\n" \
76     "  <head>\n" \
77     "    <title>vlc log</title>\n" \
78     "    <meta http-equiv=\"Content-Type\"" \
79              " content=\"text/html; charset=UTF-8\">\n" \
80     "  </head>\n" \
81     "  <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
82     "    <pre>\n" \
83     "      <strong>-- logger module started --</strong>\n"
84 #define HTML_FOOTER \
85     "      <strong>-- logger module stopped --</strong>\n" \
86     "    </pre>\n" \
87     "  </body>\n" \
88     "</html>\n"
89
90 static void LogHtml(void *opaque, int type, const vlc_log_t *meta,
91                     const char *format, va_list ap)
92 {
93     static const unsigned color[4] = {
94         0xffffff, 0xff6666, 0xffff66, 0xaaaaaa,
95     };
96     vlc_logger_sys_t *sys = opaque;
97     FILE *stream = sys->stream;
98
99     if (sys->verbosity < type)
100         return;
101
102     int canc = vlc_savecancel();
103
104     flockfile(stream);
105     fprintf(stream, "%s%s: <span style=\"color: #%06x\">",
106             meta->psz_module, msg_type[type], color[type]);
107     /* FIXME: encode special ASCII characters */
108     vfprintf(stream, format, ap);
109     fputs("</span>\n", stream);
110     funlockfile(stream);
111
112     vlc_restorecancel(canc);
113 }
114
115 static vlc_log_cb Open(vlc_object_t *obj, void **restrict sysp)
116 {
117     if (!var_InheritBool(obj, "file-logging"))
118         return NULL;
119
120     int verbosity = var_InheritInteger(obj, "log-verbose");
121     if (verbosity == -1)
122         verbosity = var_InheritInteger(obj, "verbose");
123     if (verbosity < 0)
124         return NULL; /* nothing to log */
125
126     verbosity += VLC_MSG_ERR;
127
128     vlc_logger_sys_t *sys = malloc(sizeof (*sys));
129     if (unlikely(sys == NULL))
130         return NULL;
131
132     const char *filename = TEXT_FILENAME;
133     const char *header = TEXT_HEADER;
134
135     vlc_log_cb cb = LogText;
136     sys->footer = TEXT_FOOTER;
137     sys->verbosity = verbosity;
138
139     char *mode = var_InheritString(obj, "logmode");
140     if (mode != NULL)
141     {
142         if (!strcmp(mode, "html"))
143         {
144             filename = HTML_FILENAME;
145             header = HTML_HEADER;
146             cb = LogHtml;
147             sys->footer = HTML_FOOTER;
148         }
149         else if (strcmp(mode, "text"))
150             msg_Warn(obj, "invalid log mode \"%s\"", mode);
151         free(mode);
152     }
153
154     char *path = var_InheritString(obj, "logfile");
155 #ifdef __APPLE__
156     if (path == NULL)
157     {
158         char *home = config_GetUserDir(VLC_HOME_DIR);
159         if (home != NULL)
160         {
161             if (asprintf(&path, "%s/Library/Logs/%s", home, path) == -1)
162                 path = NULL;
163             free(home);
164         }
165     }
166 #endif
167     if (path != NULL)
168         filename = path;
169
170     /* Open the log file and remove any buffering for the stream */
171     msg_Dbg(obj, "opening logfile `%s'", filename);
172     sys->stream = vlc_fopen(filename, "at");
173     if (sys->stream == NULL)
174     {
175         msg_Err(obj, "error opening log file `%s': %s", filename,
176                 vlc_strerror_c(errno) );
177         free(path);
178         free(sys);
179         return NULL;
180     }
181     free(path);
182
183     setvbuf(sys->stream, NULL, _IONBF, 0);
184     fputs(header, sys->stream);
185
186     *sysp = sys;
187     return cb;
188 }
189
190 static void Close(void *opaque)
191 {
192     vlc_logger_sys_t *sys = opaque;
193
194     fputs(sys->footer, sys->stream);
195     fclose(sys->stream);
196     free(sys);
197 }
198
199 static const char *const mode_list[] = { "text", "html" };
200 static const char *const mode_list_text[] = { N_("Text"), N_("HTML") };
201
202 #define FILE_LOG_TEXT N_("Log to file")
203 #define FILE_LOG_LONGTEXT N_("Log all VLC messages to a text file.")
204
205 #define LOGMODE_TEXT N_("Log format")
206 #define LOGMODE_LONGTEXT N_("Specify the logging format.")
207
208 #define LOGVERBOSE_TEXT N_("Verbosity")
209 #define LOGVERBOSE_LONGTEXT N_("Select the verbosity to use for log or -1 to " \
210 "use the same verbosity given by --verbose.")
211
212 vlc_module_begin()
213     set_shortname(N_("Logger"))
214     set_description(N_("File logger"))
215     set_category(CAT_ADVANCED)
216     set_subcategory(SUBCAT_ADVANCED_MISC)
217     set_capability("logger", 15)
218     set_callbacks(Open, Close)
219
220     add_bool("file-logging", false, FILE_LOG_TEXT, FILE_LOG_LONGTEXT, false)
221     add_savefile("logfile", NULL,
222                  N_("Log filename"), N_("Specify the log filename."), false)
223     add_string("logmode", "text", LOGMODE_TEXT, LOGMODE_LONGTEXT, false)
224         change_string_list(mode_list, mode_list_text)
225     add_integer("log-verbose", -1, LOGVERBOSE_TEXT, LOGVERBOSE_LONGTEXT,
226                 false)
227 vlc_module_end ()