stream_out: standard: add 'srt'
[vlc.git] / modules / logger / file.c
blobff7962497bac21aa25497afd2bdb311c45d01a3d
1 /*****************************************************************************
2 * file.c: file logger plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2008 the VideoLAN team
5 * Copyright © 2007-2015 Rémi Denis-Courmont
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_fs.h>
32 //#include <vlc_charset.h>
34 #include <stdarg.h>
35 #include <assert.h>
36 #include <errno.h>
38 static const char msg_type[4][9] = { "", " error", " warning", " debug" };
40 typedef struct
42 FILE *stream;
43 const char *footer;
44 int verbosity;
45 } vlc_logger_sys_t;
47 #define TEXT_FILENAME "vlc-log.txt"
48 #define TEXT_HEADER "\xEF\xBB\xBF" /* UTF-8 BOM */ \
49 "-- logger module started --\n"
50 #define TEXT_FOOTER "-- logger module stopped --\n"
52 static void LogText(void *opaque, int type, const vlc_log_t *meta,
53 const char *format, va_list ap)
55 vlc_logger_sys_t *sys = opaque;
56 FILE *stream = sys->stream;
58 if (sys->verbosity < type)
59 return;
61 flockfile(stream);
62 fprintf(stream, "%s%s: ", meta->psz_module, msg_type[type]);
63 vfprintf(stream, format, ap);
64 putc_unlocked('\n', stream);
65 funlockfile(stream);
68 #define HTML_FILENAME "vlc-log.html"
69 #define HTML_HEADER \
70 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
71 " \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
72 "<html>\n" \
73 " <head>\n" \
74 " <title>vlc log</title>\n" \
75 " <meta http-equiv=\"Content-Type\"" \
76 " content=\"text/html; charset=UTF-8\">\n" \
77 " </head>\n" \
78 " <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
79 " <pre>\n" \
80 " <strong>-- logger module started --</strong>\n"
81 #define HTML_FOOTER \
82 " <strong>-- logger module stopped --</strong>\n" \
83 " </pre>\n" \
84 " </body>\n" \
85 "</html>\n"
87 static void LogHtml(void *opaque, int type, const vlc_log_t *meta,
88 const char *format, va_list ap)
90 static const unsigned color[4] = {
91 0xffffff, 0xff6666, 0xffff66, 0xaaaaaa,
93 vlc_logger_sys_t *sys = opaque;
94 FILE *stream = sys->stream;
96 if (sys->verbosity < type)
97 return;
99 flockfile(stream);
100 fprintf(stream, "%s%s: <span style=\"color: #%06x\">",
101 meta->psz_module, msg_type[type], color[type]);
102 /* FIXME: encode special ASCII characters */
103 vfprintf(stream, format, ap);
104 fputs("</span>\n", stream);
105 funlockfile(stream);
108 static vlc_log_cb Open(vlc_object_t *obj, void **restrict sysp)
110 if (!var_InheritBool(obj, "file-logging"))
111 return NULL;
113 int verbosity = var_InheritInteger(obj, "log-verbose");
114 if (verbosity == -1)
115 verbosity = var_InheritInteger(obj, "verbose");
116 if (verbosity < 0)
117 return NULL; /* nothing to log */
119 verbosity += VLC_MSG_ERR;
121 vlc_logger_sys_t *sys = malloc(sizeof (*sys));
122 if (unlikely(sys == NULL))
123 return NULL;
125 const char *filename = TEXT_FILENAME;
126 const char *header = TEXT_HEADER;
128 vlc_log_cb cb = LogText;
129 sys->footer = TEXT_FOOTER;
130 sys->verbosity = verbosity;
132 char *mode = var_InheritString(obj, "logmode");
133 if (mode != NULL)
135 if (!strcmp(mode, "html"))
137 filename = HTML_FILENAME;
138 header = HTML_HEADER;
139 cb = LogHtml;
140 sys->footer = HTML_FOOTER;
142 else if (strcmp(mode, "text"))
143 msg_Warn(obj, "invalid log mode \"%s\"", mode);
144 free(mode);
147 char *path = var_InheritString(obj, "logfile");
148 #ifdef __APPLE__
149 if (path == NULL)
151 char *home = config_GetUserDir(VLC_HOME_DIR);
152 if (home != NULL)
154 if (asprintf(&path, "%s/Library/Logs/%s", home, path) == -1)
155 path = NULL;
156 free(home);
159 #endif
160 if (path != NULL)
161 filename = path;
163 /* Open the log file and remove any buffering for the stream */
164 msg_Dbg(obj, "opening logfile `%s'", filename);
165 sys->stream = vlc_fopen(filename, "at");
166 if (sys->stream == NULL)
168 msg_Err(obj, "error opening log file `%s': %s", filename,
169 vlc_strerror_c(errno) );
170 free(path);
171 free(sys);
172 return NULL;
174 free(path);
176 setvbuf(sys->stream, NULL, _IONBF, 0);
177 fputs(header, sys->stream);
179 *sysp = sys;
180 return cb;
183 static void Close(void *opaque)
185 vlc_logger_sys_t *sys = opaque;
187 fputs(sys->footer, sys->stream);
188 fclose(sys->stream);
189 free(sys);
192 static const char *const mode_list[] = { "text", "html" };
193 static const char *const mode_list_text[] = { N_("Text"), N_("HTML") };
195 static const int verbosity_values[] = {
197 VLC_MSG_INFO,
198 VLC_MSG_ERR,
199 VLC_MSG_WARN,
200 VLC_MSG_DBG
203 static const char *const verbosity_text[] = { N_("Default"), N_("Info"), N_("Error"), N_("Warning"), N_("Debug") };
205 #define FILE_LOG_TEXT N_("Log to file")
206 #define FILE_LOG_LONGTEXT N_("Log all VLC messages to a text file.")
208 #define LOGMODE_TEXT N_("Log format")
209 #define LOGMODE_LONGTEXT N_("Specify the logging format.")
211 #define LOGVERBOSE_TEXT N_("Verbosity")
212 #define LOGVERBOSE_LONGTEXT N_("Select the verbosity to use for log or " \
213 "default to use the same verbosity given by --verbose.")
215 vlc_module_begin()
216 set_shortname(N_("Logger"))
217 set_description(N_("File logger"))
218 set_category(CAT_ADVANCED)
219 set_subcategory(SUBCAT_ADVANCED_MISC)
220 set_capability("logger", 15)
221 set_callbacks(Open, Close)
223 add_bool("file-logging", false, FILE_LOG_TEXT, FILE_LOG_LONGTEXT, false)
224 add_savefile("logfile", NULL,
225 N_("Log filename"), N_("Specify the log filename."), false)
226 add_string("logmode", "text", LOGMODE_TEXT, LOGMODE_LONGTEXT, false)
227 change_string_list(mode_list, mode_list_text)
228 add_integer("log-verbose", -1, LOGVERBOSE_TEXT, LOGVERBOSE_LONGTEXT, false)
229 change_integer_list(verbosity_values, verbosity_text)
230 vlc_module_end ()