Also register for path / on D-Bus
[browser-switch] / log.c
1 /*
2  * log.c -- logging functions for browser-switchboard
3  *
4  * Copyright (C) 2010 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  */
22
23 #include <stdarg.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <syslog.h>
27
28 #include "log.h"
29
30 #define DEFAULT_LOGGER LOGTO_STDOUT
31 static enum {
32         LOGTO_NONE,
33         LOGTO_STDOUT,
34         LOGTO_SYSLOG,
35 } logger = DEFAULT_LOGGER;
36
37 /* Configure the logging target, performing any required setup for that
38    target */
39 void log_config(char *logger_name) {
40         if (!logger_name) {
41                 /* No logger configured, use the default log target */
42                 logger = DEFAULT_LOGGER;
43                 return;
44         }
45
46         if (!strcmp(logger_name, "stdout"))
47                 logger = LOGTO_STDOUT;
48         else if (!strcmp(logger_name, "syslog")) {
49                 /* XXX allow syslog facility to be configured? */
50                 openlog("browser-switchboard", LOG_PID, LOG_USER);
51                 logger = LOGTO_SYSLOG;
52         }
53         else if (!strcmp(logger_name, "none"))
54                 logger = LOGTO_NONE;
55         else
56                 /* Invalid logger configured, use the default log target */
57                 logger = DEFAULT_LOGGER;
58
59         return;
60 }
61
62 /* Log output to the chosen log target */
63 void log_msg(const char *format, ...) {
64         va_list ap;
65
66         if (!format)
67                 return;
68
69         va_start(ap, format);
70         switch (logger) {
71                 case LOGTO_NONE:
72                         break;
73                 case LOGTO_SYSLOG:
74                         /* XXX allow syslog priority to be set by caller? */
75                         vsyslog(LOG_DEBUG, format, ap);
76                         break;
77                 case LOGTO_STDOUT:
78                 default:
79                         vprintf(format, ap);
80                         break;
81         }
82         va_end(ap);
83 }
84
85 /* Log strerror(errnum), with the string in prefix appended
86    Behaves like perror() except that it logs to chosen target, not stderr */
87 void log_perror(int errnum, const char *prefix) {
88         char *errmsg;
89
90         if (!prefix)
91                 return;
92         if (!(errmsg = strerror(errnum)))
93                 return;
94
95         log_msg("%s: %s\n", prefix, errmsg);
96 }