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
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.
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.
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 *****************************************************************************/
26 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
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,
40 static void Log(void *opaque, int type, const vlc_log_t *meta,
41 const char *format, va_list ap)
43 static const char default_msg[] = "message lost";
45 int priority = priorities[type];
47 int canc = vlc_savecancel();
49 if (vasprintf(&str, format, ap) == -1)
50 str = (char *)default_msg;
52 if (meta->psz_header != NULL)
53 syslog(priority, "[%s] %s: %s", meta->psz_header, meta->psz_module,
56 syslog(priority, "%s: %s", meta->psz_module, str);
57 vlc_restorecancel(canc);
59 if (str != default_msg)
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)
78 static const char *const fac_names[] = { DEFINE_SYSLOG_FACILITY };
81 static const int fac_ids[] = { DEFINE_SYSLOG_FACILITY };
83 #undef DEFINE_SYSLOG_FACILITY
85 static int var_InheritFacility(vlc_object_t *obj, const char *varname)
87 char *str = var_InheritString(obj, varname);
88 if (unlikely(str == NULL))
89 return LOG_USER; /* LOG_USEr is the spec default. */
91 for (size_t i = 0; i < sizeof (fac_ids) / sizeof (fac_ids[0]); i++)
93 if (!strcmp(str, fac_names[i]))
100 msg_Warn(obj, "unknown syslog facility \"%s\"", str);
105 static const char default_ident[] = PACKAGE;
107 static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
109 if (!var_InheritBool(obj, "syslog"))
112 char *ident = var_InheritString(obj, "syslog-ident");
114 ident = (char *)default_ident;
118 int facility = var_InheritFacility(obj, "syslog-facility");
120 openlog(ident, LOG_PID | LOG_NDELAY, facility);
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);
132 static void Close(void *opaque)
134 char *ident = opaque;
137 if (ident != default_ident)
141 #define SYSLOG_TEXT N_("System log (syslog)")
142 #define SYSLOG_LONGTEXT N_("Emit log messages through the POSIX system log.")
144 #define SYSLOG_DEBUG_TEXT N_("Debug messages")
145 #define SYSLOG_DEBUG_LONGTEXT N_("Include debug messages in system log.")
147 #define SYSLOG_IDENT_TEXT N_("Identity")
148 #define SYSLOG_IDENT_LONGTEXT N_("Process identity in system log.")
150 #define SYSLOG_FACILITY_TEXT N_("Facility")
151 #define SYSLOG_FACILITY_LONGTEXT N_("System logging facility.")
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)
161 add_bool("syslog", false, SYSLOG_TEXT, SYSLOG_LONGTEXT,
163 add_bool("syslog-debug", false, SYSLOG_DEBUG_TEXT, SYSLOG_DEBUG_LONGTEXT,
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)