aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPA4WDH2023-05-24 08:33:53 +0200
committerPA4WDH2023-05-24 08:33:53 +0200
commitfa20f603834ef9febe396c058c9d775038f4a28b (patch)
treedbe5ad3e978f562c4709b66653a5efd9bbdcc2f3
parentAdd keep feature for governor (diff)
downloadcputemp2maxfreq-fa20f603834ef9febe396c058c9d775038f4a28b.tar.gz
cputemp2maxfreq-fa20f603834ef9febe396c058c9d775038f4a28b.tar.bz2
cputemp2maxfreq-fa20f603834ef9febe396c058c9d775038f4a28b.zip
Add logging functions
-rw-r--r--Makefile2
-rw-r--r--argparse.c22
-rw-r--r--cputemp2maxfreq.c16
-rw-r--r--cputemp2maxfreq.h2
-rw-r--r--logger.c71
-rw-r--r--logger.h9
6 files changed, 117 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index 89fc8b7..77e77c6 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-cputemp2maxfreq: version.o debug.o sysfs.o cpufreq.o failsafe.o argparse.o
+cputemp2maxfreq: version.o debug.o sysfs.o cpufreq.o failsafe.o argparse.o logger.o
version.h: generate_version_h.sh .git/index
./generate_version_h.sh > version.h
diff --git a/argparse.c b/argparse.c
index 01b6c57..7246d31 100644
--- a/argparse.c
+++ b/argparse.c
@@ -4,6 +4,7 @@
#include <string.h>
#include "cputemp2maxfreq.h"
#include "debug.h"
+#include "logger.h"
extern struct s_config config;
@@ -20,14 +21,16 @@ void printhelp()
"-h Display this help text\n"
"-i <file> Input for temperature readout, usually somewhere in sysfs\n"
" Default: %s\n"
+ "-l <logger> Logger to use, valid values: none, kmsg, stdout, syslog\n"
+ " Default: %s\n"
"-p <time> Poll interval in seconds\n"
" Default: %d\n"
"-s <step> Step size in Khz when increasing/decreasing CPU speed\n"
" Default: %ld\n"
"-t <number> Temperature limit\n"
" Default: %ld\n",
- config.fallback_freq,config.governor,config.temp_input,config.interval,
- config.freq_step,config.max_temp/1000
+ config.fallback_freq,config.governor,config.temp_input,config.logger_name,
+ config.interval,config.freq_step,config.max_temp/1000
);
}
@@ -42,7 +45,7 @@ void argparse(int argc, char **argv)
memcpy(&userconfig,&config,sizeof(struct s_config));
// Parse options
- while((opt=getopt(argc,argv,"f:g:hi:p:s:t:"))!=-1)
+ while((opt=getopt(argc,argv,"f:g:hi:l:p:s:t:"))!=-1)
{
DEBUG1_ARGPARSE("Argument: %c Value: %s\n",opt,optarg);
switch(opt)
@@ -59,6 +62,9 @@ void argparse(int argc, char **argv)
case 'i':
strncpy(userconfig.temp_input,optarg,sizeof(userconfig.temp_input));
break;
+ case 'l':
+ strncpy(userconfig.logger_name,optarg,sizeof(userconfig.logger_name));
+ break;
case 'p':
userconfig.interval=strtoll(optarg,NULL,10);
break;
@@ -74,6 +80,16 @@ void argparse(int argc, char **argv)
}
}
+// Change the logger function pointer
+ if (strcmp(userconfig.logger_name,"none")==0) userconfig.logger=&logger_none;
+ else if (strcmp(userconfig.logger_name,"stdout")==0) userconfig.logger=&logger_stdout;
+ else if (strcmp(userconfig.logger_name,"syslog")==0) userconfig.logger=&logger_syslog;
+ else if (strcmp(userconfig.logger_name,"kmsg")==0) userconfig.logger=&logger_kmsg;
+ else {
+ printf("Invalid value for logger, exiting.\n");
+ exit(1);
+ }
+
// Copy modified config to global config
memcpy(&config,&userconfig,sizeof(struct s_config));
}
diff --git a/cputemp2maxfreq.c b/cputemp2maxfreq.c
index aabb99f..9e20848 100644
--- a/cputemp2maxfreq.c
+++ b/cputemp2maxfreq.c
@@ -11,9 +11,19 @@
#include "failsafe.h"
#include "version.h"
#include "argparse.h"
+#include "logger.h"
// Set default config
-struct s_config config={"conservative",70000,"/sys/devices/virtual/thermal/thermal_zone0/temp",100000,2000000,10};
+struct s_config config={
+ "conservative", // Governor
+ 70000, // Temperature
+ "/sys/devices/virtual/thermal/thermal_zone0/temp", // Temperature input
+ 100000, // Frequency step
+ 2000000, // Fallback frequency
+ 10, // Interval
+ "stdout", // Logger name
+ &logger_stdout, // Logger function
+};
struct s_cpudata cpudata;
void signal_handler(int signum)
@@ -38,8 +48,12 @@ int main(int argc,char **argv)
printf("Frequency step: %ld\n",config.freq_step);
printf("Fallback frquency: %ld\n",config.fallback_freq);
printf("Interval: %d\n",config.interval);
+ printf("Logger: %s (%p)\n",config.logger_name,config.logger);
printf("\n");
+ config.logger("Log test1");
+ config.logger("Log test2");
+
if ((config.max_temp<VALID_TEMP_MIN) || (config.max_temp>VALID_TEMP_MAX))
{
printf("Invalid temperature, range is %d-%d\n",VALID_TEMP_MIN,VALID_TEMP_MAX);
diff --git a/cputemp2maxfreq.h b/cputemp2maxfreq.h
index 39886fd..cda9d4b 100644
--- a/cputemp2maxfreq.h
+++ b/cputemp2maxfreq.h
@@ -32,6 +32,8 @@ struct s_config {
long int freq_step; // Step size to increase/decrease CPU frequency
long int fallback_freq; // CPU frquency to set if we fail to protect hardware
unsigned int interval; // Time interval to check CPU temperature
+ char logger_name[10]; // Name of the logging function
+ void (*logger)(char *,...); // Pointer to the logging function
};
#endif
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);
+ }
+}
diff --git a/logger.h b/logger.h
new file mode 100644
index 0000000..b9ee787
--- /dev/null
+++ b/logger.h
@@ -0,0 +1,9 @@
+#ifndef HAVE_LOGGER_H
+#define HAVE_LOGGER_H
+
+void logger_none(char *format,...);
+void logger_stdout(char *format,...);
+void logger_syslog(char *format,...);
+void logger_kmsg(char *format,...);
+
+#endif