]> git.sesse.net Git - vlc/blob - modules/logger/syslog.c
vlc_plugin: fix non-LGPL plugins meta infos
[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 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29
30 #include <stdarg.h>
31 #include <syslog.h>
32
33 static const int priorities[4] = {
34     [VLC_MSG_INFO] = LOG_INFO,
35     [VLC_MSG_ERR]  = LOG_ERR,
36     [VLC_MSG_WARN] = LOG_WARNING,
37     [VLC_MSG_DBG]  = LOG_DEBUG,
38 };
39
40 static void Log(void *opaque, int type, const vlc_log_t *meta,
41                 const char *format, va_list ap)
42 {
43     static const char default_msg[] = "message lost";
44     char *str;
45     int priority = priorities[type];
46
47     int canc = vlc_savecancel();
48
49     if (vasprintf(&str, format, ap) == -1)
50         str = (char *)default_msg;
51
52     if (meta->psz_header != NULL)
53         syslog(priority, "[%s] %s: %s", meta->psz_header, meta->psz_module,
54                str);
55     else
56         syslog(priority, "%s: %s", meta->psz_module, str);
57     vlc_restorecancel(canc);
58
59     if (str != default_msg)
60         free(str);
61     (void) opaque;
62 }
63
64 /* First in list is the default facility used. */
65 #define DEFINE_SYSLOG_FACILITY \
66   DEF("user",   LOG_USER),   \
67   DEF("daemon", LOG_DAEMON), \
68   DEF("local0", LOG_LOCAL0), \
69   DEF("local1", LOG_LOCAL1), \
70   DEF("local2", LOG_LOCAL2), \
71   DEF("local3", LOG_LOCAL3), \
72   DEF("local4", LOG_LOCAL4), \
73   DEF("local5", LOG_LOCAL5), \
74   DEF("local6", LOG_LOCAL6), \
75   DEF("local7", LOG_LOCAL7)
76
77 #define DEF(a,b) a
78 static const char *const fac_names[] = { DEFINE_SYSLOG_FACILITY };
79 #undef  DEF
80 #define DEF(a,b) b
81 static const int         fac_ids[] = { DEFINE_SYSLOG_FACILITY };
82 #undef  DEF
83 #undef  DEFINE_SYSLOG_FACILITY
84
85 static int var_InheritFacility(vlc_object_t *obj, const char *varname)
86 {
87     char *str = var_InheritString(obj, varname);
88     if (unlikely(str == NULL))
89         return LOG_USER; /* LOG_USEr is the spec default. */
90
91     for (size_t i = 0; i < sizeof (fac_ids) / sizeof (fac_ids[0]); i++)
92     {
93         if (!strcmp(str, fac_names[i]))
94         {
95             free(str);
96             return fac_ids[i];
97         }
98     }
99
100     msg_Warn(obj, "unknown syslog facility \"%s\"", str);
101     free(str);
102     return LOG_USER;
103 }
104
105 static const char default_ident[] = PACKAGE;
106
107 static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
108 {
109     if (!var_InheritBool(obj, "syslog"))
110         return NULL;
111
112     char *ident = var_InheritString(obj, "syslog-ident");
113     if (ident == NULL)
114         ident = (char *)default_ident;
115     *sysp = ident;
116
117     /* Open log */
118     int facility = var_InheritFacility(obj, "syslog-facility");
119
120     openlog(ident, LOG_PID | LOG_NDELAY, facility);
121
122     /* Set priority filter */
123     int mask = LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING) | LOG_MASK(LOG_INFO);
124     if (var_InheritBool(obj, "syslog-debug"))
125         mask |= LOG_MASK(LOG_DEBUG);
126
127     setlogmask(mask);
128
129     return Log;
130 }
131
132 static void Close(void *opaque)
133 {
134     char *ident = opaque;
135
136     closelog();
137     if (ident != default_ident)
138         free(ident);
139 }
140
141 #define SYSLOG_TEXT N_("System log (syslog)")
142 #define SYSLOG_LONGTEXT N_("Emit log messages through the POSIX system log.")
143
144 #define SYSLOG_DEBUG_TEXT N_("Debug messages")
145 #define SYSLOG_DEBUG_LONGTEXT N_("Include debug messages in system log.")
146
147 #define SYSLOG_IDENT_TEXT N_("Identity")
148 #define SYSLOG_IDENT_LONGTEXT N_("Process identity in system log.")
149
150 #define SYSLOG_FACILITY_TEXT N_("Facility")
151 #define SYSLOG_FACILITY_LONGTEXT N_("System logging facility.")
152
153 vlc_module_begin()
154     set_shortname(N_( "syslog" ))
155     set_description(N_("System logger (syslog)"))
156     set_category(CAT_ADVANCED)
157     set_subcategory(SUBCAT_ADVANCED_MISC)
158     set_capability("logger", 20)
159     set_callbacks(Open, Close)
160
161     add_bool("syslog", false, SYSLOG_TEXT, SYSLOG_LONGTEXT,
162              false)
163     add_bool("syslog-debug", false, SYSLOG_DEBUG_TEXT, SYSLOG_DEBUG_LONGTEXT,
164              false)
165     add_string("syslog-ident", default_ident, SYSLOG_IDENT_TEXT,
166                SYSLOG_IDENT_LONGTEXT, true)
167     add_string("syslog-facility", fac_names[0], SYSLOG_FACILITY_TEXT,
168                SYSLOG_FACILITY_LONGTEXT, true)
169         change_string_list(fac_names, fac_names)
170 vlc_module_end()