From 16fb812a46d0e8cde432b58f7dbf6fce147eb89f Mon Sep 17 00:00:00 2001 From: PA4WDH Date: Wed, 24 May 2023 12:57:25 +0200 Subject: Add CSV logging --- logger.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 3 deletions(-) (limited to 'logger.c') diff --git a/logger.c b/logger.c index 83e92e1..143b62e 100644 --- a/logger.c +++ b/logger.c @@ -3,13 +3,16 @@ #include #include #include -#include "cputemp2maxfreq.h" #include +#include +#include "cputemp2maxfreq.h" +#include "debug.h" extern struct s_config config; +extern struct s_cpudata cpudata; -// These functions are not called directly. They are set by argparse in the -// config.logger function pointer. +// The logger_* 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,...) { } @@ -69,3 +72,60 @@ void logger_kmsg(char *format,...) config.logger(buffer); } } + +// CSV logging fields: +// 1: Timestamp +// 2: CPU Minimum frequency +// 3: CPU Maximum frequency +// 4: CPU Current frequency +// 5: CPU Temperature +// 6: Target temperature +// 7: Scaling maximum frequency before increase/decrease +void csvlog_init() +{ + int exists; + + DEBUG1_LOG("Started\n"); + exists=access(config.csvlog,F_OK); + DEBUG1_LOG("File %s exists: %d\n",config.csvlog,exists); + + if ((exists!=0) || (config.csvoverwrite==1)) + { + DEBUG1_LOG("Creating/overwriting CSV file\n"); + config.csvfile=fopen(config.csvlog,"w"); + fputs("\"timestamp\"," + "\"CPU Minumum frequency\"," + "\"CPU Maximum frequency\"," + "\"CPU Current frequency\"," + "\"CPU Temperature\"," + "\"Target temperature\"," + "\"Scaling maximum frequency\"\n",config.csvfile); + } else { + DEBUG1_LOG("Appending to CSV file\n"); + config.csvfile=fopen(config.csvlog,"a"); + } +} + +// Write a single line of CSV data +void csvlog_write() +{ + struct timeval unixtime; + struct tm *time; + char timestring[255]; + + DEBUG1_LOG("Writing data series to CSV file\n"); + + gettimeofday(&unixtime,NULL); + time=localtime(&unixtime.tv_sec); + strftime(timestring,255,"%F %T",time); + + 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 +void csvlog_close() +{ + DEBUG1_LOG("Closing CSV file\n"); + fclose(config.csvfile); + config.csvfile=NULL; +} -- cgit v1.2.3