Add more logging infrastructure
This commit is contained in:
		
							parent
							
								
									ff1f6fb734
								
							
						
					
					
						commit
						92606e58e2
					
				|  | @ -7,9 +7,11 @@ | |||
| 
 | ||||
| /* Below are all the includes used throughout the application. */ | ||||
| #include <errno.h> | ||||
| #include <fcntl.h> | ||||
| #include <stdio.h> | ||||
| #include <getopt.h> | ||||
| #include <stdarg.h> | ||||
| #include <string.h> | ||||
| #include <syslog.h> | ||||
| #include <unistd.h> | ||||
| #include <sys/types.h> | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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 <dest>          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; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 Laurence Withers
						Laurence Withers