From: Steven Luo Date: Sun, 21 Feb 2010 02:13:28 +0000 (-0800) Subject: Add logging functions with selectable log output targets X-Git-Tag: v3.2~13 X-Git-Url: http://git.maemo.org/git/?p=browser-switch;a=commitdiff_plain;h=663c89819b6990c99427d1cf5b0fcae21e4201bc;hp=ef66c102d916ea842c8ba15f010bf04326e3a590 Add logging functions with selectable log output targets Currently, our "logging" consists of a mix of printf(), perror(), and other such calls which print output to stdout/stderr. In normal usage, browser-switchboard will be launched by D-Bus, which results in stdout/stderr pointing to /dev/null and debugging output being lost. To improve this situation, introduce a new set of logging functions log_out() and log_perror(), and a log_config() which allows choosing the log target. These functions log to stdout by default, but also support syslog output (to the LOG_USER facility, with priority LOG_DEBUG) and disabling logging entirely. Thanks to Faheem Pervez (qwerty12) for the suggestion. --- diff --git a/log.c b/log.c new file mode 100644 index 0000000..36d0fd5 --- /dev/null +++ b/log.c @@ -0,0 +1,96 @@ +/* + * log.c -- logging functions for browser-switchboard + * + * Copyright (C) 2010 Steven Luo + * Derived from a Python implementation by Jason Simpson and Steven Luo + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + +#include +#include +#include +#include + +#include "log.h" + +#define DEFAULT_LOGGER LOGTO_STDOUT +static enum { + LOGTO_NONE, + LOGTO_STDOUT, + LOGTO_SYSLOG, +} logger = DEFAULT_LOGGER; + +/* Configure the logging target, performing any required setup for that + target */ +void log_config(char *logger_name) { + if (!logger_name) { + /* No logger configured, use the default log target */ + logger = DEFAULT_LOGGER; + return; + } + + if (!strcmp(logger_name, "stdout")) + logger = LOGTO_STDOUT; + else if (!strcmp(logger_name, "syslog")) { + /* XXX allow syslog facility to be configured? */ + openlog("browser-switchboard", LOG_PID, LOG_USER); + logger = LOGTO_SYSLOG; + } + else if (!strcmp(logger_name, "none")) + logger = LOGTO_NONE; + else + /* Invalid logger configured, use the default log target */ + logger = DEFAULT_LOGGER; + + return; +} + +/* Log output to the chosen log target */ +void log_msg(const char *format, ...) { + va_list ap; + + if (!format) + return; + + va_start(ap, format); + switch (logger) { + case LOGTO_NONE: + break; + case LOGTO_SYSLOG: + /* XXX allow syslog priority to be set by caller? */ + vsyslog(LOG_DEBUG, format, ap); + break; + case LOGTO_STDOUT: + default: + vprintf(format, ap); + break; + } + va_end(ap); +} + +/* Log strerror(errnum), with the string in prefix appended + Behaves like perror() except that it logs to chosen target, not stderr */ +void log_perror(int errnum, const char *prefix) { + char *errmsg; + + if (!prefix) + return; + if (!(errmsg = strerror(errnum))) + return; + + log_msg("%s: %s\n", prefix, errmsg); +} diff --git a/log.h b/log.h new file mode 100644 index 0000000..350dbcc --- /dev/null +++ b/log.h @@ -0,0 +1,30 @@ +/* + * log.h -- definitions for the logging functions + * + * Copyright (C) 2010 Steven Luo + * Derived from a Python implementation by Jason Simpson and Steven Luo + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + +#ifndef _LOG_H +#define _LOG_H 1 + +void log_config(char *logger_name); +void log_msg(const char *format, ...); +void log_perror(int errnum, const char *prefix); + +#endif /* _LOG_H */