CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
helper_log.cc
Go to the documentation of this file.
1 
5 #include "helper_log.h"
6 
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <syslog.h>
10 #include <unistd.h>
11 
12 #include <cassert>
13 #include <cstdarg>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <cstring>
17 #include <ctime>
18 
19 using namespace std; // NOLINT
20 
21 namespace {
22 int fd_debug = -1;
23 FILE *file_debug = NULL;
24 int syslog_facility = LOG_USER;
25 int syslog_level = LOG_NOTICE;
26 char *syslog_prefix = NULL;
27 }
28 
29 
30 void SetLogAuthzDebug(const string &path) {
31  assert(!path.empty());
32  if (fd_debug >= 0)
33  close(fd_debug);
34  int fd_debug = open(path.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0600);
35  if (fd_debug < 0) {
36  syslog(LOG_USER | LOG_ERR, "could not open debug log %s (%d), abort",
37  path.c_str(), errno);
38  abort();
39  }
40  file_debug = fdopen(fd_debug, "a");
41  assert(file_debug != NULL);
42 }
43 
44 
45 void SetLogAuthzSyslogLevel(const int level) {
46  switch (level) {
47  case 1:
48  syslog_level = LOG_DEBUG;
49  break;
50  case 2:
51  syslog_level = LOG_INFO;
52  break;
53  case 3:
54  syslog_level = LOG_NOTICE;
55  break;
56  default:
57  syslog_level = LOG_NOTICE;
58  break;
59  }
60 }
61 
62 
63 void SetLogAuthzSyslogFacility(const int local_facility) {
64  switch (local_facility) {
65  case 0:
66  syslog_facility = LOG_LOCAL0;
67  break;
68  case 1:
69  syslog_facility = LOG_LOCAL1;
70  break;
71  case 2:
72  syslog_facility = LOG_LOCAL2;
73  break;
74  case 3:
75  syslog_facility = LOG_LOCAL3;
76  break;
77  case 4:
78  syslog_facility = LOG_LOCAL4;
79  break;
80  case 5:
81  syslog_facility = LOG_LOCAL5;
82  break;
83  case 6:
84  syslog_facility = LOG_LOCAL6;
85  break;
86  case 7:
87  syslog_facility = LOG_LOCAL7;
88  break;
89  default:
90  syslog_facility = LOG_USER;
91  }
92 }
93 
94 
95 void SetLogAuthzSyslogPrefix(const string &prefix) {
96  if (syslog_prefix)
97  free(syslog_prefix);
98 
99  if (prefix == "") {
100  syslog_prefix = NULL;
101  } else {
102  unsigned len = prefix.length() + 1;
103  syslog_prefix = static_cast<char *>(malloc(len));
104  assert(syslog_prefix != NULL);
105  syslog_prefix[len-1] = '\0';
106  memcpy(syslog_prefix, &prefix[0], prefix.length());
107  }
108 }
109 
110 
111 void LogAuthz(const int flags, const char *format, ...) {
112  char *msg = NULL;
113  va_list variadic_list;
114  va_start(variadic_list, format);
115  int retval = vasprintf(&msg, format, variadic_list);
116  assert(retval != -1); // else: out of memory
117  va_end(variadic_list);
118 
119  if ((flags & kLogAuthzDebug) && (file_debug != NULL)) {
120  time_t rawtime;
121  time(&rawtime);
122  struct tm now;
123  localtime_r(&rawtime, &now);
124  fprintf(file_debug, "%s [%02d-%02d-%04d %02d:%02d:%02d %s]\n", msg,
125  (now.tm_mon)+1, now.tm_mday, (now.tm_year)+1900, now.tm_hour,
126  now.tm_min, now.tm_sec, now.tm_zone);
127  fflush(file_debug);
128  }
129 
131  int level = syslog_level;
132  if (flags & kLogAuthzSyslogWarn) level = LOG_WARNING;
133  if (flags & kLogAuthzSyslogErr) level = LOG_ERR;
134  if (syslog_prefix) {
135  syslog(syslog_facility | level, "(%s) %s", syslog_prefix, msg);
136  } else {
137  syslog(syslog_facility | level, "%s", msg);
138  }
139  }
140 
141  free(msg);
142 }
const unsigned kLogAuthzDebug
Definition: helper_log.h:10
assert((mem||(size==0))&&"Out Of Memory")
void SetLogAuthzSyslogPrefix(const string &prefix)
Definition: helper_log.cc:95
const unsigned kLogAuthzSyslog
Definition: helper_log.h:11
const unsigned kLogAuthzSyslogErr
Definition: helper_log.h:13
void SetLogAuthzSyslogFacility(const int local_facility)
Definition: helper_log.cc:63
void SetLogAuthzDebug(const string &path)
Definition: helper_log.cc:30
const unsigned kLogAuthzSyslogWarn
Definition: helper_log.h:12
const int const char * format
Definition: logging.h:23
void LogAuthz(const int flags, const char *format,...)
Definition: helper_log.cc:111
void SetLogAuthzSyslogLevel(const int level)
Definition: helper_log.cc:45