diff options
author | Yorhel <git@yorhel.nl> | 2013-04-14 15:12:18 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2013-04-14 15:12:18 +0200 |
commit | afb39c7f0489ce534ad0847da9a8c131fcd18d13 (patch) | |
tree | 219ffffc8f5a746f871a43f87c42cf8b912400eb | |
parent | 893a8e62bb546d54e65b62d3baaaf595f3b190da (diff) |
main.c: Add --log-file=syslog functionality
-rw-r--r-- | doc/globster.pod | 6 | ||||
-rw-r--r-- | src/main.c | 34 |
2 files changed, 26 insertions, 14 deletions
diff --git a/doc/globster.pod b/doc/globster.pod index 7220539..ecd6104 100644 --- a/doc/globster.pod +++ b/doc/globster.pod @@ -39,9 +39,9 @@ Connect to the session D-Bus (default). =item -l, --log-file I<file> -Write the logs to the specified file. The special values C<stdout> and -C<stderr> can be used to log to standard output or standard error, -respectively. +Write the logs to the specified file. The special values C<stdout>, C<stderr> +and C<syslog> can be used to log to standard output, standard error or to the +syslog daemon, respectively. =item --log-level I<level> @@ -23,6 +23,7 @@ #include <global.h> #include <dbusev.h> #include <yopt.h> +#include <syslog.h> DBusConnection *dbuscon; @@ -93,20 +94,26 @@ static void log_handler(const char *file, int line, int level, const char *messa #endif static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; char lvl[8], tstr[32] = {}; - time_t t = time(NULL); - strftime(tstr, sizeof(tstr)-1, "%F %H:%M:%S %Z", localtime(&t)); - switch(level) { - case YLOG_ERR: strcpy(lvl, "ERROR"); break; - case YLOG_WARN: strcpy(lvl, "WARN-"); break; - case YLOG_INFO: strcpy(lvl, "info-"); break; - case YLOG_DEBUG: strcpy(lvl, "debug"); break; - case YLOG_TRACE: strcpy(lvl, "trace"); break; - default: sprintf(lvl, "%5d", level); + if(strcmp(conf_log_file, "syslog") != 0) { + time_t t = time(NULL); + strftime(tstr, sizeof(tstr)-1, "%F %H:%M:%S %Z", localtime(&t)); + switch(level) { + case YLOG_ERR: strcpy(lvl, "ERROR"); break; + case YLOG_WARN: strcpy(lvl, "WARN-"); break; + case YLOG_INFO: strcpy(lvl, "info-"); break; + case YLOG_DEBUG: strcpy(lvl, "debug"); break; + default: sprintf(lvl, "%5d", level); + } } pthread_mutex_lock(&m); if(log_file) logfile_logf(log_file, "%s -%s- %s:%d: %s\n", tstr, lvl, file, line, message); + else if(strcmp(conf_log_file, "syslog") == 0) + syslog(level == YLOG_ERR ? LOG_ERR : + level == YLOG_WARN ? LOG_WARNING : + level == YLOG_INFO ? LOG_INFO : LOG_DEBUG, + "%s:%d: %s", file, line, message); else fprintf(strcmp(conf_log_file, "stderr") == 0 ? stderr : stdout, "%s -%s- %s:%d: %s\n", tstr, lvl, file, line, message); @@ -138,7 +145,7 @@ static void print_help() { " -h, --help This help message\n" " --system Connect to the D-Bus system bus\n" " --session Connect to the D-Bus session bus (default)\n" - " --log-file FILE Log to the given file, `stdout' or `stderr'\n" + " --log-file FILE Log to the given file, `stdout', `stderr' or `syslog'\n" " --log-level Set the log level\n" " -c, --session-dir PATH Set the session directory\n" " -n Disable autoconnect"); @@ -223,8 +230,11 @@ int main(int argc, char **argv) { main_state = MAIN_INIT; - if(strcmp(conf_log_file, "stderr") != 0 && strcmp(conf_log_file, "stdout") != 0) + if(strcmp(conf_log_file, "syslog") == 0) + openlog("globster", 0, LOG_USER); + else if(strcmp(conf_log_file, "stderr") != 0 && strcmp(conf_log_file, "stdout") != 0) log_file = logfile_open(conf_log_file); + if(!conf_log_level) conf_log_level = getenv("YLOG_LEVEL"); ylog_set_level(YLOG_DEFAULT, conf_log_level); @@ -269,6 +279,8 @@ int main(int argc, char **argv) { if(log_file) logfile_close(log_file); + else if(strcmp(conf_log_file, "syslog") == 0) + closelog(); return 0; } |