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