aboutsummaryrefslogtreecommitdiffstats
path: root/logger.c
diff options
context:
space:
mode:
authorPA4WDH2023-05-24 08:33:53 +0200
committerPA4WDH2023-05-24 08:33:53 +0200
commitfa20f603834ef9febe396c058c9d775038f4a28b (patch)
treedbe5ad3e978f562c4709b66653a5efd9bbdcc2f3 /logger.c
parentAdd keep feature for governor (diff)
downloadcputemp2maxfreq-fa20f603834ef9febe396c058c9d775038f4a28b.tar.gz
cputemp2maxfreq-fa20f603834ef9febe396c058c9d775038f4a28b.tar.bz2
cputemp2maxfreq-fa20f603834ef9febe396c058c9d775038f4a28b.zip
Add logging functions
Diffstat (limited to 'logger.c')
-rw-r--r--logger.c71
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);
+ }
+}