1 /* Copyright (C) 2007, 2008 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include "dynamic-string.h"
34 /* Name for each logging level. */
35 static const char *level_names[VLL_N_LEVELS] = {
42 /* Name for each logging facility. */
43 static const char *facility_names[VLF_N_FACILITIES] = {
44 [VLF_CONSOLE] = "console",
45 [VLF_SYSLOG] = "syslog",
48 /* Name for each logging module */
49 static const char *module_names[VLM_N_MODULES] = {
50 #define VLOG_MODULE(NAME) #NAME,
55 static int levels[VLM_N_MODULES][VLF_N_FACILITIES];
57 /* Searches the 'n_names' in 'names'. Returns the index of a match for
58 * 'target', or 'n_names' if no name matches. */
60 search_name_array(const char *target, const char **names, size_t n_names)
64 for (i = 0; i < n_names; i++) {
66 if (!strcasecmp(names[i], target)) {
73 /* Returns the name for logging level 'level'. */
75 vlog_get_level_name(enum vlog_level level)
77 assert(level < VLL_N_LEVELS);
78 return level_names[level];
81 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
82 * is not the name of a logging level. */
84 vlog_get_level_val(const char *name)
86 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
89 /* Returns the name for logging facility 'facility'. */
91 vlog_get_facility_name(enum vlog_facility facility)
93 assert(facility < VLF_N_FACILITIES);
94 return facility_names[facility];
97 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
98 * not the name of a logging facility. */
100 vlog_get_facility_val(const char *name)
102 return search_name_array(name, facility_names, ARRAY_SIZE(facility_names));
105 /* Returns the name for logging module 'module'. */
106 const char *vlog_get_module_name(enum vlog_module module)
108 assert(module < VLM_N_MODULES);
109 return module_names[module];
112 /* Returns the logging module named 'name', or VLM_N_MODULES if 'name' is not
113 * the name of a logging module. */
115 vlog_get_module_val(const char *name)
117 return search_name_array(name, module_names, ARRAY_SIZE(module_names));
120 /* Returns the current logging level for the given 'module' and 'facility'. */
122 vlog_get_level(enum vlog_module module, enum vlog_facility facility)
124 assert(module < VLM_N_MODULES);
125 assert(facility < VLF_N_FACILITIES);
126 return levels[module][facility];
130 set_facility_level(enum vlog_facility facility, enum vlog_module module,
131 enum vlog_level level)
133 assert(facility >= 0 && facility < VLF_N_FACILITIES);
134 assert(level < VLL_N_LEVELS);
136 if (module == VLM_ANY_MODULE) {
137 for (module = 0; module < VLM_N_MODULES; module++) {
138 levels[module][facility] = level;
141 levels[module][facility] = level;
145 /* Sets the logging level for the given 'module' and 'facility' to 'level'. */
147 vlog_set_levels(enum vlog_module module, enum vlog_facility facility,
148 enum vlog_level level)
150 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
151 if (facility == VLF_ANY_FACILITY) {
152 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
153 set_facility_level(facility, module, level);
156 set_facility_level(facility, module, level);
160 /* Set debugging levels:
162 * mod:facility:level mod2:facility:level ...
164 * Return null if successful, otherwise an error message that the caller must
168 vlog_set_levels_from_string(const char *s_)
171 char *s = xstrdup(s_);
172 char *module, *level, *facility;
174 for (module = strtok_r(s, ": \t", &save_ptr); module != NULL;
175 module = strtok_r(NULL, ": \t", &save_ptr)) {
176 enum vlog_module e_module;
177 enum vlog_level e_level;
178 enum vlog_facility e_facility;
180 facility = strtok_r(NULL, ":", &save_ptr);
181 level = strtok_r(NULL, ":", &save_ptr);
182 if (level == NULL || facility == NULL) {
184 return xstrdup("syntax error in level string");
187 if (!strcmp(module, "ANY")) {
188 e_module = VLM_ANY_MODULE;
190 e_module = vlog_get_module_val(module);
191 if (e_module >= VLM_N_MODULES) {
192 char *msg = xasprintf("unknown module \"%s\"", module);
198 if (!strcmp(facility, "ANY")) {
199 e_facility = VLF_ANY_FACILITY;
201 e_facility = vlog_get_facility_val(facility);
202 if (e_facility >= VLF_N_FACILITIES) {
203 char *msg = xasprintf("unknown facility \"%s\"", facility);
209 e_level = vlog_get_level_val(level);
210 if (e_level >= VLL_N_LEVELS) {
211 char *msg = xasprintf("unknown level \"%s\"", level);
216 vlog_set_levels(e_module, e_facility, e_level);
222 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
223 * configuration according to 'arg' (see vlog_set_levels_from_string()). If
224 * parsing fails, default to maximum verbosity. */
226 vlog_set_verbosity(const char *arg)
228 if (arg == NULL || !vlog_set_levels_from_string(arg)) {
229 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_DBG);
233 /* Initializes the logging subsystem. */
237 openlog(program_name, LOG_NDELAY, LOG_DAEMON);
238 vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
241 /* Closes the logging subsystem. */
248 /* Print the current logging level for each module. */
250 vlog_get_levels(void)
252 struct ds s = DS_EMPTY_INITIALIZER;
253 enum vlog_module module;
255 ds_put_format(&s, " console syslog\n");
256 ds_put_format(&s, " ------- ------\n");
258 for (module = 0; module < VLM_N_MODULES; module++) {
259 ds_put_format(&s, "%-16s %4s %4s\n",
260 vlog_get_module_name(module),
261 vlog_get_level_name(vlog_get_level(module, VLF_CONSOLE)),
262 vlog_get_level_name(vlog_get_level(module, VLF_SYSLOG)));
268 /* Writes 'message' to the log at the given 'level' and as coming from the
271 * Guaranteed to preserve errno. */
273 vlog(enum vlog_module module, enum vlog_level level, const char *message, ...)
275 bool log_console = levels[module][VLF_CONSOLE] >= level;
276 bool log_syslog = levels[module][VLF_SYSLOG] >= level;
277 if (log_console || log_syslog) {
278 int save_errno = errno;
280 const char *module_name = vlog_get_module_name(module);
281 const char *level_name = vlog_get_level_name(level);
286 len = sprintf(s, "%05d|%s|%s:", ++msg_num, module_name, level_name);
287 va_start(args, message);
288 len += vsnprintf(s + len, sizeof s - len, message, args);
290 if (len >= sizeof s) {
293 if (s[len - 1] == '\n') {
298 fprintf(stderr, "%s\n", s);
302 static const int syslog_levels[VLL_N_LEVELS] = {
303 [VLL_EMER] = LOG_EMERG,
305 [VLL_WARN] = LOG_WARNING,
306 [VLL_DBG] = LOG_DEBUG,
309 syslog(syslog_levels[level], "%s", s);