aboutsummaryrefslogtreecommitdiffstats
path: root/logger.c
blob: 83e92e18f8a459bdc8e8d39822ff8b94c84806cb (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
#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);
 }
}