]> git.sesse.net Git - vlc/blob - modules/logger/syslog.c
d88ecb47990701b911c100ada3d80ab6d5b9e824
[vlc] / modules / logger / syslog.c
1 /*****************************************************************************
2  * syslog.c: POSIX syslog logger plugin
3  *****************************************************************************
4  * Copyright (C) 2002-2008 the VideoLAN team
5  * Copyright © 2007-2015 Rémi Denis-Courmont
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28
29 #include <stdarg.h>
30 #include <syslog.h>
31
32 static const int priorities[4] = {
33     [VLC_MSG_INFO] = LOG_INFO,
34     [VLC_MSG_ERR]  = LOG_ERR,
35     [VLC_MSG_WARN] = LOG_WARNING,
36     [VLC_MSG_DBG]  = LOG_DEBUG,
37 };
38
39 static void Log(void *opaque, int type, const vlc_log_t *meta,
40                 const char *format, va_list ap)
41 {
42     static const char default_msg[] = "message lost";
43     char *str;
44     int priority = priorities[type];
45
46     int canc = vlc_savecancel();
47
48     if (vasprintf(&str, format, ap) == -1)
49         str = (char *)default_msg;
50
51     if (meta->psz_header != NULL)
52         syslog(priority, "[%s] %s: %s", meta->psz_header, meta->psz_module,
53                str);
54     else
55         syslog(priority, "%s: %s", meta->psz_module, str);
56     vlc_restorecancel(canc);
57
58     if (str != default_msg)
59         free(str);
60     (void) opaque;
61 }
62
63 /* First in list is the default facility used. */
64 #define DEFINE_SYSLOG_FACILITY \
65   DEF("user",   LOG_USER),   \
66   DEF("daemon", LOG_DAEMON), \
67   DEF("local0", LOG_LOCAL0), \
68   DEF("local1", LOG_LOCAL1), \
69   DEF("local2", LOG_LOCAL2), \
70   DEF("local3", LOG_LOCAL3), \
71   DEF("local4", LOG_LOCAL4), \
72   DEF("local5", LOG_LOCAL5), \
73   DEF("local6", LOG_LOCAL6), \
74   DEF("local7", LOG_LOCAL7)
75
76 #define DEF(a,b) a
77 static const char *const fac_names[] = { DEFINE_SYSLOG_FACILITY };
78 #undef  DEF
79 #define DEF(a,b) b
80 static const int         fac_ids[] = { DEFINE_SYSLOG_FACILITY };
81 #undef  DEF
82 #undef  DEFINE_SYSLOG_FACILITY
83
84 static int var_InheritFacility(vlc_object_t *obj, const char *varname)
85 {
86     char *str = var_InheritString(obj, varname);
87     if (unlikely(str == NULL))
88         return LOG_USER; /* LOG_USEr is the spec default. */
89
90     for (size_t i = 0; i < sizeof (fac_ids) / sizeof (fac_ids[0]); i++)
91     {
92         if (!strcmp(str, fac_names[i]))
93         {
94             free(str);
95             return fac_ids[i];
96         }
97     }
98
99     msg_Warn(obj, "unknown syslog facility \"%s\"", str);
100     free(str);
101     return LOG_USER;
102 }
103
104 static const char default_ident[] = PACKAGE;
105
106 static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
107 {
108     if (!var_InheritBool(obj, "syslog"))
109         return NULL;
110
111     char *ident = var_InheritString(obj, "syslog-ident");
112     if (ident == NULL)
113         ident = (char *)default_ident;
114     *sysp = ident;
115
116     /* Open log */
117     int facility = var_InheritFacility(obj, "syslog-facility");
118
119     openlog(ident, LOG_PID | LOG_NDELAY, facility);
120
121     /* Set priority filter */
122     int mask = LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING) | LOG_MASK(LOG_INFO);
123     if (var_InheritBool(obj, "syslog-debug"))
124         mask |= LOG_MASK(LOG_DEBUG);
125
126     setlogmask(mask);
127
128     return Log;
129 }
130
131 static void Close(void *opaque)
132 {
133     char *ident = opaque;
134
135     closelog();
136     if (ident != default_ident)
137         free(ident);
138 }
139
140 #define SYSLOG_TEXT N_("System log (syslog)")
141 #define SYSLOG_LONGTEXT N_("Emit log messages through the POSIX system log.")
142
143 #define SYSLOG_DEBUG_TEXT N_("Debug messages")
144 #define SYSLOG_DEBUG_LONGTEXT N_("Include debug messages in system log.")
145
146 #define SYSLOG_IDENT_TEXT N_("Identity")
147 #define SYSLOG_IDENT_LONGTEXT N_("Process identity in system log.")
148
149 #define SYSLOG_FACILITY_TEXT N_("Facility")
150 #define SYSLOG_FACILITY_LONGTEXT N_("System logging facility.")
151
152 vlc_module_begin()
153     set_shortname(N_( "syslog" ))
154     set_description(N_("System logger (syslog)"))
155     set_category(CAT_ADVANCED)
156     set_subcategory(SUBCAT_ADVANCED_MISC)
157     set_capability("logger", 20)
158     set_callbacks(Open, Close)
159
160     add_bool("syslog", false, SYSLOG_TEXT, SYSLOG_LONGTEXT,
161              false)
162     add_bool("syslog-debug", false, SYSLOG_DEBUG_TEXT, SYSLOG_DEBUG_LONGTEXT,
163              false)
164     add_string("syslog-ident", default_ident, SYSLOG_IDENT_TEXT,
165                SYSLOG_IDENT_LONGTEXT, true)
166     add_string("syslog-facility", fac_names[0], SYSLOG_FACILITY_TEXT,
167                SYSLOG_FACILITY_LONGTEXT, true)
168         change_string_list(fac_names, fac_names)
169 vlc_module_end()