diff options
Diffstat (limited to 'logger.c')
-rw-r--r-- | logger.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/logger.c b/logger.c new file mode 100644 index 0000000..628fe8b --- /dev/null +++ b/logger.c @@ -0,0 +1,71 @@ +#include <stdarg.h> +#include <stdio.h> +#include <syslog.h> +#include <sys/time.h> +#include <time.h> +#include "cputemp2maxfreq.h" +#include <string.h> + +extern struct s_config config; + +// These functions are not called directly. They are set by argparse in the +// config.logger function pointer. + +// No logging at all +void logger_none(char *format,...) { } + +// Logging to stdout +void logger_stdout(char *format,...) +{ + va_list args; + struct timeval unixtime; + struct tm *time; + char timestring[255]; + + gettimeofday(&unixtime,NULL); + time=localtime(&unixtime.tv_sec); + strftime(timestring,255,"%F %T",time); + printf("%s: ",timestring); + + va_start(args,format); + vprintf(format,args); + va_end(args); + putchar(10); +} + +// Logging to syslog +void logger_syslog(char *format,...) +{ + va_list args; + + va_start(args,format); + vsyslog(LOG_DAEMON||LOG_CRIT,format,args); + va_end(args); +} + +// Logging to kernel log buffer (/dev/kmsg) +void logger_kmsg(char *format,...) +{ + va_list args; + FILE *kmsg; + char buffer[255]; + + kmsg=fopen("/dev/kmsg","w"); + if (kmsg!=NULL) + { + fprintf(kmsg,"cputemp2maxfreq: "); + va_start(args,format); + vfprintf(kmsg,format,args); + va_end(args); + putc(10,kmsg); + fclose(kmsg); + } else { + va_start(args,format); + vsnprintf(buffer,sizeof(buffer),format,args); + va_end(args); + strcpy(config.logger_name,"stdout"); + config.logger=&logger_stdout; + config.logger("Failed to open /dev/kmsg, falling back to stdout logging."); + config.logger(buffer); + } +} |