From b80926797863c5a36417eb44a34483bb93610b79 Mon Sep 17 00:00:00 2001 From: PA4WDH Date: Sat, 27 May 2023 11:20:22 +0200 Subject: Add feature to use unixtime timestamps in logs and CSV --- argparse.c | 9 +++++++-- cputemp2maxfreq.c | 2 ++ cputemp2maxfreq.h | 1 + logger.c | 38 ++++++++++++++++++++++++++------------ 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/argparse.c b/argparse.c index 75ca20d..5c834cc 100644 --- a/argparse.c +++ b/argparse.c @@ -32,7 +32,9 @@ void printhelp() "-s Step size in Khz when increasing/decreasing CPU speed\n" " Default: %ld\n" "-t Temperature limit\n" - " Default: %ld\n", + " Default: %ld\n" + "-u Use unixtime timestamps in log and CSV outputs\n" + " Default timestamp format is: YYYY-MM-DD HH:MM:SS\n", config.name,config.fallback_freq,config.governor,config.temp_input,config.logger_name, config.interval,config.freq_step,config.max_temp/1000 ); @@ -59,7 +61,7 @@ void argparse(int argc, char **argv) memcpy(&userconfig,&config,sizeof(struct s_config)); // Parse options - while((opt=getopt(argc,argv,"c:C:f:g:hi:l:mp:s:t:"))!=-1) + while((opt=getopt(argc,argv,"c:C:f:g:hi:l:mp:s:t:u"))!=-1) { DEBUG1_ARGPARSE("Argument: %c Value: %s\n",opt,optarg); switch(opt) @@ -99,6 +101,9 @@ void argparse(int argc, char **argv) case 't': userconfig.max_temp=strtoll(optarg,NULL,10)*1000; break; + case 'u': + userconfig.use_unixtime=1; + break; default: printhelp(); exit(1); diff --git a/cputemp2maxfreq.c b/cputemp2maxfreq.c index 9600d13..887a406 100644 --- a/cputemp2maxfreq.c +++ b/cputemp2maxfreq.c @@ -28,6 +28,7 @@ struct s_config config={ "", // CSV logfile 0, // Don't overwrite CSV logfile NULL, // File handler for CSV file + 0, // Don't use unix timestamps in log outputs }; struct s_cpudata cpudata; @@ -62,6 +63,7 @@ int main(int argc,char **argv) config.logger("CSV Log file: %s",config.csvlog); config.logger("Overwrite CSV log: %d",config.csvoverwrite); } + config.logger("Use unix timestamps: %d",config.use_unixtime); if ((config.max_tempVALID_TEMP_MAX)) { diff --git a/cputemp2maxfreq.h b/cputemp2maxfreq.h index 31f6da1..afce5ca 100644 --- a/cputemp2maxfreq.h +++ b/cputemp2maxfreq.h @@ -44,6 +44,7 @@ struct s_config { char csvlog[255]; // CSV logfile char csvoverwrite; // Overwrite CSV file if it already exists FILE *csvfile; // File handle for CSV file + char use_unixtime; // Use unixtime timestamps in logs and CSV }; #endif diff --git a/logger.c b/logger.c index 00ee4a7..315c8c2 100644 --- a/logger.c +++ b/logger.c @@ -21,14 +21,21 @@ void logger_none(char *format,...) { } void logger_stdout(char *format,...) { va_list args; - struct timeval unixtime; - struct tm *time; + struct timeval systime; + struct tm *systime_tm; char timestring[255]; + time_t unixtime; - gettimeofday(&unixtime,NULL); - time=localtime(&unixtime.tv_sec); - strftime(timestring,255,"%F %T",time); - printf("%s: ",timestring); + if (config.use_unixtime==0) + { + gettimeofday(&systime,NULL); + systime_tm=localtime(&systime.tv_sec); + strftime(timestring,sizeof(timestring),"%F %T",systime_tm); + printf("%s: ",timestring); + } else { + unixtime=time(NULL); + printf("%ld: ",unixtime); + } va_start(args,format); vprintf(format,args); @@ -109,17 +116,24 @@ void csvlog_init() // Write a single line of CSV data void csvlog_write() { - struct timeval unixtime; - struct tm *time; + struct timeval systime; + struct tm *systime_tm; char timestring[255]; + time_t unixtime; DEBUG1_LOG("Writing data series to CSV file\n"); - gettimeofday(&unixtime,NULL); - time=localtime(&unixtime.tv_sec); - strftime(timestring,255,"%F %T",time); + if (config.use_unixtime==0) + { + gettimeofday(&systime,NULL); + systime_tm=localtime(&systime.tv_sec); + strftime(timestring,sizeof(timestring),"\"%F %T\"",systime_tm); + } else { + unixtime=time(NULL); + snprintf(timestring,sizeof(timestring),"%ld",unixtime); + } - fprintf(config.csvfile,"\"%s\",\"%ld\",\"%ld\",\"%ld\",\"%ld\",\"%ld\",\"%ld\"\n",timestring,cpudata.min_freq,cpudata.max_freq,cpudata.cur_freq,cpudata.cur_temp/1000,config.max_temp/1000,cpudata.scale_max); + fprintf(config.csvfile,"%s,\"%ld\",\"%ld\",\"%ld\",\"%ld\",\"%ld\",\"%ld\"\n",timestring,cpudata.min_freq,cpudata.max_freq,cpudata.cur_freq,cpudata.cur_temp/1000,config.max_temp/1000,cpudata.scale_max); } // Close the CSV file -- cgit v1.2.3