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 #include <vlc_common.h>
27 #include <vlc_plugin.h>
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,
39 static void Log(void *opaque, int type, const vlc_log_t *meta,
40 const char *format, va_list ap)
42 static const char default_msg[] = "message lost";
44 int priority = priorities[type];
46 int canc = vlc_savecancel();
48 if (vasprintf(&str, format, ap) == -1)
49 str = (char *)default_msg;
51 if (meta->psz_header != NULL)
52 syslog(priority, "[%s] %s: %s", meta->psz_header, meta->psz_module,
55 syslog(priority, "%s: %s", meta->psz_module, str);
56 vlc_restorecancel(canc);
58 if (str != default_msg)
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)
77 static const char *const fac_names[] = { DEFINE_SYSLOG_FACILITY };
80 static const int fac_ids[] = { DEFINE_SYSLOG_FACILITY };
82 #undef DEFINE_SYSLOG_FACILITY
84 static int var_InheritFacility(vlc_object_t *obj, const char *varname)
86 char *str = var_InheritString(obj, varname);
87 if (unlikely(str == NULL))
88 return LOG_USER; /* LOG_USEr is the spec default. */
90 for (size_t i = 0; i < sizeof (fac_ids) / sizeof (fac_ids[0]); i++)
92 if (!strcmp(str, fac_names[i]))
99 msg_Warn(obj, "unknown syslog facility \"%s\"", str);
104 static const char default_ident[] = PACKAGE;
106 static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
108 if (!var_InheritBool(obj, "syslog"))
111 char *ident = var_InheritString(obj, "syslog-ident");
113 ident = (char *)default_ident;
117 int facility = var_InheritFacility(obj, "syslog-facility");
119 openlog(ident, LOG_PID | LOG_NDELAY, facility);
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);
131 static void Close(void *opaque)
133 char *ident = opaque;
136 if (ident != default_ident)
140 #define SYSLOG_TEXT N_("System log (syslog)")
141 #define SYSLOG_LONGTEXT N_("Emit log messages through the POSIX system log.")
143 #define SYSLOG_DEBUG_TEXT N_("Debug messages")
144 #define SYSLOG_DEBUG_LONGTEXT N_("Include debug messages in system log.")
146 #define SYSLOG_IDENT_TEXT N_("Identity")
147 #define SYSLOG_IDENT_LONGTEXT N_("Process identity in system log.")
149 #define SYSLOG_FACILITY_TEXT N_("Facility")
150 #define SYSLOG_FACILITY_LONGTEXT N_("System logging facility.")
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)
160 add_bool("syslog", false, SYSLOG_TEXT, SYSLOG_LONGTEXT,
162 add_bool("syslog-debug", false, SYSLOG_DEBUG_TEXT, SYSLOG_DEBUG_LONGTEXT,
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)