]> git.sesse.net Git - cubemap/blob - log.cpp
Fix broken linking.
[cubemap] / log.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <syslog.h>
8 #include <string>
9 #include <vector>
10
11 #include "log.h"
12
13 using namespace std;
14
15 // Yes, it's a bit ugly.
16 #define SYSLOG_FAKE_FILE (static_cast<FILE *>(NULL))
17
18 bool logging_started = false;
19 std::vector<FILE *> log_destinations;
20
21 void add_log_destination_file(const std::string &filename)
22 {
23         FILE *fp = fopen(filename.c_str(), "a");
24         if (fp == NULL) {
25                 perror(filename.c_str());
26                 return;
27         }
28
29         log_destinations.push_back(fp); 
30 }
31
32 void add_log_destination_console()
33 {
34         log_destinations.push_back(stderr);
35 }
36
37 void add_log_destination_syslog()
38 {
39         openlog("cubemap", LOG_PID, LOG_DAEMON);
40         log_destinations.push_back(SYSLOG_FAKE_FILE);
41 }
42
43 void start_logging()
44 {
45         logging_started = true;
46 }
47
48 void shut_down_logging()
49 {
50         for (size_t i = 0; i < log_destinations.size(); ++i) {
51                 if (log_destinations[i] == SYSLOG_FAKE_FILE) {
52                         closelog();
53                 } else if (log_destinations[i] != stderr) {
54                         if (fclose(log_destinations[i]) != 0) {
55                                 perror("fclose");
56                         }
57                 }
58         }
59         log_destinations.clear();
60         logging_started = false;
61 }
62
63 void log(LogLevel log_level, const char *fmt, ...)
64 {
65         char formatted_msg[4096];
66         va_list ap;
67         va_start(ap, fmt);
68         vsnprintf(formatted_msg, sizeof(formatted_msg), fmt, ap);
69         va_end(ap);
70
71         const char *log_level_str;
72         int syslog_level;
73
74         switch (log_level) {
75         case INFO:
76                 log_level_str = "INFO:    ";
77                 syslog_level = LOG_INFO;
78                 break;
79         case WARNING:
80                 log_level_str = "WARNING: ";
81                 syslog_level = LOG_WARNING;
82                 break;
83         case ERROR:
84                 log_level_str = "ERROR:   ";
85                 syslog_level = LOG_ERR;
86                 break;
87         default:
88                 assert(false);
89         }
90
91         // Log to stderr if logging hasn't been set up yet. Note that this means
92         // that such messages will come even if there are no “error_log” lines.
93         if (!logging_started) {
94                 fprintf(stderr, "%s%s\n", log_level_str, formatted_msg);
95                 return;
96         }
97
98         for (size_t i = 0; i < log_destinations.size(); ++i) {
99                 if (log_destinations[i] == SYSLOG_FAKE_FILE) {
100                         syslog(syslog_level, "%s", formatted_msg);
101                 } else {
102                         int err = fprintf(log_destinations[i], "%s%s\n", log_level_str, formatted_msg);
103                         if (err < 0) {
104                                 perror("fprintf");
105                         }
106                         if (log_destinations[i] != stderr) {
107                                 fflush(log_destinations[i]);
108                         }
109                 }
110         }
111 }
112
113 void log_perror(const char *msg)
114 {
115         char errbuf[4096];
116         log(ERROR, "%s: %s", msg, strerror_r(errno, errbuf, sizeof(errbuf)));
117 }