aboutsummaryrefslogtreecommitdiffstats
path: root/logger.c
blob: 315c8c278cebfc7da28ed0c639b21e4555abdab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include "cputemp2maxfreq.h"
#include "debug.h"

extern struct s_config config;
extern struct s_cpudata cpudata;

// 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,...) { }

// Logging to stdout
void logger_stdout(char *format,...)
{
 va_list args;
 struct timeval systime;
 struct tm *systime_tm;
 char timestring[255];
 time_t unixtime;

 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);
 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);
 }
}

// 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 systime;
 struct tm *systime_tm;
 char timestring[255];
 time_t unixtime;

 DEBUG1_LOG("Writing data series to CSV file\n");

 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);
}

// Close the CSV file
void csvlog_close()
{
 DEBUG1_LOG("Closing CSV file\n");
 fclose(config.csvfile);
 config.csvfile=NULL;
}