aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPA4WDH2023-05-24 09:09:23 +0200
committerPA4WDH2023-05-24 09:09:23 +0200
commit33bd1b7c488e935f247407e191811a7539a12375 (patch)
treef60c23d07933afbfc36363b742945b62c93732cf
parentAdd logging functions (diff)
downloadcputemp2maxfreq-33bd1b7c488e935f247407e191811a7539a12375.tar.gz
cputemp2maxfreq-33bd1b7c488e935f247407e191811a7539a12375.tar.bz2
cputemp2maxfreq-33bd1b7c488e935f247407e191811a7539a12375.zip
Redirect all output to logging functions
-rw-r--r--argparse.c2
-rw-r--r--cputemp2maxfreq.c65
-rw-r--r--failsafe.c10
-rw-r--r--logger.c2
4 files changed, 38 insertions, 41 deletions
diff --git a/argparse.c b/argparse.c
index 7246d31..43ee2ec 100644
--- a/argparse.c
+++ b/argparse.c
@@ -86,7 +86,7 @@ void argparse(int argc, char **argv)
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");
+ config.logger("Invalid value for logger, exiting");
exit(1);
}
diff --git a/cputemp2maxfreq.c b/cputemp2maxfreq.c
index 9e20848..88d9a5d 100644
--- a/cputemp2maxfreq.c
+++ b/cputemp2maxfreq.c
@@ -28,7 +28,7 @@ struct s_cpudata cpudata;
void signal_handler(int signum)
{
- printf("Received signal %d, exiting.\n",signum);
+ config.logger("Received signal %d, exiting.",signum);
failsafe(0);
}
@@ -37,38 +37,34 @@ int main(int argc,char **argv)
long int diff;
long int newfreq;
- printf("Version %s, buildtime %s %s\n",version(),__DATE__,__TIME__);
argparse(argc,argv);
-
-// Print configuration
- printf("Configuration:\n");
- printf("Governor: %s\n",config.governor);
- printf("Temperature: %ld\n",config.max_temp);
- printf("Temp input: %s\n",config.temp_input);
- 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");
+ config.logger("Version %s, buildtime %s %s",version(),__DATE__,__TIME__);
+
+// Log configuration
+ config.logger("Configuration:");
+ config.logger("Governor: %s",config.governor);
+ config.logger("Temperature: %ld",config.max_temp);
+ config.logger("Temp input: %s",config.temp_input);
+ config.logger("Frequency step: %ld",config.freq_step);
+ config.logger("Fallback frquency: %ld",config.fallback_freq);
+ config.logger("Interval: %d",config.interval);
+ config.logger("Logger: %s (%p)",config.logger_name,config.logger);
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);
+ config.logger("Invalid temperature, range is %d-%d",VALID_TEMP_MIN,VALID_TEMP_MAX);
exit(1);
}
if ((config.freq_step<VALID_STEP_MIN) || (config.freq_step>VALID_STEP_MAX))
{
- printf("Invalid frequency step, range is %d-%d\n",VALID_STEP_MIN,VALID_STEP_MAX);
+ config.logger("Invalid frequency step, range is %d-%d",VALID_STEP_MIN,VALID_STEP_MAX);
exit(1);
}
if ((config.interval<VALID_INTERVAL_MIN) || (config.interval>VALID_INTERVAL_MAX))
{
- printf("Invalid poll interval, range is %d-%d\n",VALID_INTERVAL_MIN,VALID_INTERVAL_MAX);
+ config.logger("Invalid poll interval, range is %d-%d",VALID_INTERVAL_MIN,VALID_INTERVAL_MAX);
exit(1);
}
@@ -77,30 +73,29 @@ int main(int argc,char **argv)
cpudata.max_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq");
cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
- printf("CPU data:\n");
- printf("Minimum frequency: %ld\n",cpudata.min_freq);
- printf("Maximum frequency: %ld\n",cpudata.max_freq);
- printf("Scaling maximum frequency: %ld\n",cpudata.scale_max);
- printf("\n");
+ config.logger("CPU data:");
+ config.logger("Minimum frequency: %ld",cpudata.min_freq);
+ config.logger("Maximum frequency: %ld",cpudata.max_freq);
+ config.logger("Scaling maximum frequency: %ld",cpudata.scale_max);
if ((cpudata.min_freq<VALID_FREQ_MIN) || (cpudata.min_freq>VALID_FREQ_MAX) ||
(cpudata.max_freq<VALID_FREQ_MIN) || (cpudata.max_freq>VALID_FREQ_MAX) ||
(cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
{
// If we have to fail now, there's not much we can do because we have no data
- printf("Invalid CPU data, exiting.\n");
+ config.logger("Invalid CPU data, exiting");
exit(1);
}
// Modify fallback frequency if needed
if (config.fallback_freq<cpudata.min_freq)
{
- printf("Fallback frequency is lower than CPU's minimum frequency, setting to %ld.\n",cpudata.min_freq);
+ config.logger("Fallback frequency is lower than CPU's minimum frequency, setting to %ld",cpudata.min_freq);
config.fallback_freq=cpudata.min_freq;
}
if (config.fallback_freq>cpudata.max_freq)
{
- printf("Fallback frequency is higher than CPU's maximum frequency, setting to %ld.\n",cpudata.max_freq);
+ config.logger("Fallback frequency is higher than CPU's maximum frequency, setting to %ld",cpudata.max_freq);
config.fallback_freq=cpudata.max_freq;
}
@@ -109,7 +104,7 @@ int main(int argc,char **argv)
{
if (cpufreq_set_str("scaling_governor",config.governor,0)<0)
{
- printf("Failed to set governor, error %d (%s).\n",errno,strerror(errno));
+ config.logger("Failed to set governor, error %d (%s)",errno,strerror(errno));
// We failed to set the governor, call the failsafe
failsafe(1);
@@ -127,14 +122,14 @@ int main(int argc,char **argv)
cpudata.cur_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq");
if ((cpudata.cur_freq<VALID_FREQ_MIN) || (cpudata.cur_freq>VALID_FREQ_MAX))
{
- printf("Invalid current frequency reported by CPU, exiting.\n");
+ config.logger("Invalid current frequency reported by CPU, exiting");
failsafe(1);
}
cpudata.cur_temp=sysfs_read_long_int(config.temp_input);
if ((cpudata.cur_temp<VALID_TEMP_MIN) || (cpudata.cur_temp>VALID_TEMP_MAX))
{
- printf("Invalid current tempature reported by CPU, exiting.\n");
+ config.logger("Invalid current tempature reported by CPU, exiting");
failsafe(1);
}
@@ -147,17 +142,18 @@ int main(int argc,char **argv)
newfreq=cpudata.scale_max+(config.freq_step*diff);
if (newfreq>cpudata.max_freq) newfreq=cpudata.max_freq;
DEBUG1_MAIN("Increase to %ld\n",newfreq);
+ config.logger("Increase scaling_max_freq to %ld",newfreq);
// Set new value and validate
if (cpufreq_set_long_int("scaling_max_freq",newfreq,100)<0)
{
- printf("Failed to set scaling_max_freq, exiting.\n");
+ config.logger("Failed to set scaling_max_freq, exiting");
failsafe(1);
}
cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
if ((cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
{
- printf("Invalid scale_max_freq reported, exiting,\n");
+ config.logger("Invalid scale_max_freq reported, exiting");
failsafe(1);
}
}
@@ -169,17 +165,18 @@ int main(int argc,char **argv)
newfreq=cpudata.scale_max-(config.freq_step*diff);
if (newfreq<cpudata.min_freq) newfreq=cpudata.min_freq;
DEBUG1_MAIN("Decrease to %ld\n",newfreq);
+ config.logger("Decrease scaling_max_freq to %ld",newfreq);
// Set new value and validate
if (cpufreq_set_long_int("scaling_max_freq",newfreq,100)<0)
{
- printf("Failed to set scaling_max_freq, exiting.\n");
+ config.logger("Failed to set scaling_max_freq, exiting");
failsafe(1);
}
cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq");
if ((cpudata.scale_max<VALID_FREQ_MIN) || (cpudata.scale_max>VALID_FREQ_MAX))
{
- printf("Invalid scale_max_freq reported, exiting,\n");
+ config.logger("Invalid scale_max_freq reported, exiting");
failsafe(1);
}
}
diff --git a/failsafe.c b/failsafe.c
index fec00e8..f0c0b3e 100644
--- a/failsafe.c
+++ b/failsafe.c
@@ -13,20 +13,20 @@ void failsafe(int code)
// First try to set the CPU to it's minimum frequency
if (cpufreq_set_long_int("scaling_max_freq",cpudata.min_freq,100)>0)
{
- printf("Set scaling frequency to CPU's minimum frequency.\n");
+ config.logger("Set scaling frequency to CPU's minimum frequency");
exit(code);
}
- printf("Failed to set scaling frequency to CPU's minimum frequency, error: %d (%s).\n",errno,strerror(errno));
+ config.logger("Failed to set scaling frequency to CPU's minimum frequency, error: %d (%s)",errno,strerror(errno));
// If that failed, try the fallback frequency
if (cpufreq_set_long_int("scaling_max_freq",config.fallback_freq,100)>0)
{
- printf("Set scaling frequency to fallback frequency.\n");
+ config.logger("Set scaling frequency to fallback frequency");
exit(code);
}
- printf("Failed to set scaling frequency to fallback frequency, error: %d (%s).\n",errno,strerror(errno));
+ config.logger("Failed to set scaling frequency to fallback frequency, error: %d (%s)",errno,strerror(errno));
// Everything failed, issue a warning
- printf("All safety measures failed, watch out not to fry your hardware.\n");
+ config.logger("All safety measures failed, watch out not to fry your hardware");
exit(code);
}
diff --git a/logger.c b/logger.c
index 628fe8b..83e92e1 100644
--- a/logger.c
+++ b/logger.c
@@ -65,7 +65,7 @@ void logger_kmsg(char *format,...)
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("Failed to open /dev/kmsg, falling back to stdout logging");
config.logger(buffer);
}
}