From 92606e58e226522076ab34874115140b567e4cb6 Mon Sep 17 00:00:00 2001 From: Laurence Withers Date: Sat, 26 Jul 2008 15:33:56 +0000 Subject: [PATCH] Add more logging infrastructure --- src/daemonitor/000_TopSource.c | 2 + src/daemonitor/101_log.c | 42 ++++++++++++++++++++ src/daemonitor/999_main.c | 71 +++++++++++++++++++++++++++++++++- 3 files changed, 114 insertions(+), 1 deletion(-) diff --git a/src/daemonitor/000_TopSource.c b/src/daemonitor/000_TopSource.c index b771594..13d0e1e 100644 --- a/src/daemonitor/000_TopSource.c +++ b/src/daemonitor/000_TopSource.c @@ -7,9 +7,11 @@ /* Below are all the includes used throughout the application. */ #include +#include #include #include #include +#include #include #include #include diff --git a/src/daemonitor/101_log.c b/src/daemonitor/101_log.c index cfe184b..7c1ddc4 100644 --- a/src/daemonitor/101_log.c +++ b/src/daemonitor/101_log.c @@ -157,6 +157,48 @@ void log_func_syslog(int level, const char* fmt, ...) +/* log_destination_set() + * Set `log_func' function pointer. + */ +void log_destination_set(enum log_destination_t dest) +{ + switch(dest) { + case log_destination_stdout: + log_func_fd = STDOUT_FILENO; + log_func = log_func_file; + break; + + case log_destination_stderr: + log_func_fd = STDERR_FILENO; + log_func = log_func_file; + break; + + case log_destination_syslog: + log_func = log_func_syslog; + break; + + case log_destination_file: + /* log_func_fd() must be set by log_destination_set_file() */ + log_func = log_func_file; + break; + } +} + + + +/* log_destination_set_file() + * Open `filename' for writing, return 0 on success and -1 on error. sets `log_func_fd'. + */ +int log_destination_set_file(const char* filename) __attribute__((nonnull,warn_unused_result)); +int log_destination_set_file(const char* filename) +{ + log_func_fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0666); + if(log_func_fd == -1) return -1; + return 0; +} + + + /* options for text editors kate: replace-trailing-space-save true; space-indent true; tab-width 4; vim: expandtab:ts=4:sw=4 diff --git a/src/daemonitor/999_main.c b/src/daemonitor/999_main.c index 7d00a07..7ab4b6c 100644 --- a/src/daemonitor/999_main.c +++ b/src/daemonitor/999_main.c @@ -7,8 +7,77 @@ -int main(void) +/* usage() + * Display the usage info. + */ +void usage(void) { + fputs("Usage:\n\n" + " daemonitor [options] -- /path/to/program [args]\n\n" + "Valid options:\n" + " -h, --help Display this screen.\n" + " -V, --version Display version number.\n" + " -l, --log Log file destination, may be one of:\n" + " SYSLOG - log to syslog (default)\n" + " STDOUT - log to stdout\n" + " STDERR - log to stderr\n" + " file path - log to file\n" + "", stderr); +} + + + +/* options[] + * The array of long commandline options for getopt(3). + */ +struct option options[] = { + { "help", no_argument, 0, 'h' }, + { "version", no_argument, 0, 'V' }, + { "log", required_argument, 0, 'l' }, + { 0, 0, 0, 0 } +}; + + + +/* optstr + * The list of short commandline options for getopt(3). Sorted alphabetically, lowercase first. + */ +const char* optstr = "hl:V"; + + + +/* main() + * The program driver. Parses the commandline options, sets up the environment, and starts the + * daemon monitoring process. + */ +int main(int argc, char* argv[]) +{ + const char* log_argument = 0; + + while(1) { + switch(getopt_long(argc, argv, optstr, options, 0)) { + case -1: goto opts_done; + case '?': LOG_ANYWHERE("Invalid commandline options."); return 1; + case 'h': usage(); return 1; + case 'V': fputs("daemonitor " VERSION "\n", stderr); return 1; + case 'l': log_argument = optarg; break; + } + } + opts_done: + /* set up logging */ + if(!log_argument || !strcmp(log_argument, "SYSLOG")) { + log_destination_set(log_destination_syslog); + } else if(!strcmp(log_argument, "STDOUT")) { + log_destination_set(log_destination_stdout); + } else if(!strcmp(log_argument, "STDERR")) { + log_destination_set(log_destination_stderr); + } else { + if(log_destination_set_file(log_argument)) { + LOG_ANYWHERE("Error logging to file `%s' (%m).", log_argument); + return 1; + } + } + return 0; }