input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / logger / syslog.c
blobfcb408b5c14bc149299cb5e55a6bccb8f224ed01
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 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
30 #include <stdarg.h>
31 #include <syslog.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";
44 char *str;
45 int priority = priorities[type];
47 if (vasprintf(&str, format, ap) == -1)
48 str = (char *)default_msg;
50 if (meta->psz_header != NULL)
51 syslog(priority, "[%s] %s: %s", meta->psz_header, meta->psz_module,
52 str);
53 else
54 syslog(priority, "%s: %s", meta->psz_module, str);
56 if (str != default_msg)
57 free(str);
58 (void) opaque;
61 /* First in list is the default facility used. */
62 #define DEFINE_SYSLOG_FACILITY \
63 DEF("user", LOG_USER), \
64 DEF("daemon", LOG_DAEMON), \
65 DEF("local0", LOG_LOCAL0), \
66 DEF("local1", LOG_LOCAL1), \
67 DEF("local2", LOG_LOCAL2), \
68 DEF("local3", LOG_LOCAL3), \
69 DEF("local4", LOG_LOCAL4), \
70 DEF("local5", LOG_LOCAL5), \
71 DEF("local6", LOG_LOCAL6), \
72 DEF("local7", LOG_LOCAL7)
74 #define DEF(a,b) a
75 static const char *const fac_names[] = { DEFINE_SYSLOG_FACILITY };
76 #undef DEF
77 #define DEF(a,b) b
78 static const int fac_ids[] = { DEFINE_SYSLOG_FACILITY };
79 #undef DEF
80 #undef DEFINE_SYSLOG_FACILITY
82 static int var_InheritFacility(vlc_object_t *obj, const char *varname)
84 char *str = var_InheritString(obj, varname);
85 if (unlikely(str == NULL))
86 return LOG_USER; /* LOG_USEr is the spec default. */
88 for (size_t i = 0; i < sizeof (fac_ids) / sizeof (fac_ids[0]); i++)
90 if (!strcmp(str, fac_names[i]))
92 free(str);
93 return fac_ids[i];
97 msg_Warn(obj, "unknown syslog facility \"%s\"", str);
98 free(str);
99 return LOG_USER;
102 static const char default_ident[] = PACKAGE;
104 static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
106 if (!var_InheritBool(obj, "syslog"))
107 return NULL;
109 char *ident = var_InheritString(obj, "syslog-ident");
110 if (ident == NULL)
111 ident = (char *)default_ident;
112 *sysp = ident;
114 /* Open log */
115 int facility = var_InheritFacility(obj, "syslog-facility");
117 openlog(ident, LOG_PID | LOG_NDELAY, facility);
119 /* Set priority filter */
120 int mask = LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING) | LOG_MASK(LOG_INFO);
121 if (var_InheritBool(obj, "syslog-debug"))
122 mask |= LOG_MASK(LOG_DEBUG);
124 setlogmask(mask);
126 return Log;
129 static void Close(void *opaque)
131 char *ident = opaque;
133 closelog();
134 if (ident != default_ident)
135 free(ident);
138 #define SYSLOG_TEXT N_("System log (syslog)")
139 #define SYSLOG_LONGTEXT N_("Emit log messages through the POSIX system log.")
141 #define SYSLOG_DEBUG_TEXT N_("Debug messages")
142 #define SYSLOG_DEBUG_LONGTEXT N_("Include debug messages in system log.")
144 #define SYSLOG_IDENT_TEXT N_("Identity")
145 #define SYSLOG_IDENT_LONGTEXT N_("Process identity in system log.")
147 #define SYSLOG_FACILITY_TEXT N_("Facility")
148 #define SYSLOG_FACILITY_LONGTEXT N_("System logging facility.")
150 vlc_module_begin()
151 set_shortname(N_( "syslog" ))
152 set_description(N_("System logger (syslog)"))
153 set_category(CAT_ADVANCED)
154 set_subcategory(SUBCAT_ADVANCED_MISC)
155 set_capability("logger", 20)
156 set_callbacks(Open, Close)
158 add_bool("syslog", false, SYSLOG_TEXT, SYSLOG_LONGTEXT,
159 false)
160 add_bool("syslog-debug", false, SYSLOG_DEBUG_TEXT, SYSLOG_DEBUG_LONGTEXT,
161 false)
162 add_string("syslog-ident", default_ident, SYSLOG_IDENT_TEXT,
163 SYSLOG_IDENT_LONGTEXT, true)
164 add_string("syslog-facility", fac_names[0], SYSLOG_FACILITY_TEXT,
165 SYSLOG_FACILITY_LONGTEXT, true)
166 change_string_list(fac_names, fac_names)
167 vlc_module_end()