Update spec/m4/etc.
[awesomized/libmemcached] / util / log.hpp
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential Utility library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #pragma once
38
39 #include <cerrno>
40 #include <cstdarg>
41 #include <cstdio>
42 #include <fcntl.h>
43 #include <iostream>
44 #include <string>
45 #include <syslog.h>
46
47 #define UTIL_MAX_ERROR_SIZE 2048
48
49 namespace datadifferential {
50 namespace util {
51
52 /** Verbosity levels.
53 */
54 enum verbose_t
55 {
56 // Logging this will cause shutdown
57 VERBOSE_FATAL= LOG_EMERG, // syslog:LOG_EMERG
58
59 VERBOSE_ALERT= LOG_ALERT, // syslog:LOG_ALERT
60 VERBOSE_CRITICAL= LOG_CRIT, // syslog:LOG_CRIT
61
62 VERBOSE_ERROR= LOG_ERR, // syslog:LOG_ERR
63
64 VERBOSE_WARN= LOG_WARNING, // syslog:LOG_WARNING
65
66 VERBOSE_NOTICE= LOG_NOTICE, // syslog:LOG_NOTICE
67
68 VERBOSE_INFO= LOG_INFO, // syslog:LOG_INFO
69
70 VERBOSE_DEBUG= LOG_DEBUG // syslog:LOG_DEBUG
71 };
72
73 #ifndef __INTEL_COMPILER
74 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
75 #endif
76
77 struct log_info_st
78 {
79 std::string name;
80 std::string filename;
81 int fd;
82 bool opt_syslog;
83 bool opt_file;
84 bool init_success;
85
86 log_info_st(const std::string& name_arg, const std::string &filename_arg, bool syslog_arg) :
87 name(name_arg),
88 filename(filename_arg),
89 fd(-1),
90 opt_syslog(syslog_arg),
91 opt_file(false),
92 init_success(false)
93 {
94 if (opt_syslog)
95 {
96 openlog(name.c_str(), LOG_PID | LOG_NDELAY, LOG_USER);
97 }
98
99 init();
100 }
101
102 void init()
103 {
104 if (filename.size())
105 {
106 if (filename.compare("stderr") == 0)
107 {
108 fd= STDERR_FILENO;
109 }
110 else
111 {
112 fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
113 if (fd == -1)
114 {
115 if (opt_syslog)
116 {
117 char buffer[1024];
118 char *getcwd_ret= getcwd(buffer, sizeof(buffer));
119 syslog(LOG_ERR, "Could not open log file \"%.*s\", from \"%s\", open failed with (%s)",
120 int(filename.size()), filename.c_str(),
121 getcwd_ret,
122 strerror(errno));
123 }
124 std::cerr << "Could not open log file for writing, switching to stderr." << std::endl;
125
126 fd= STDERR_FILENO;
127 }
128 }
129
130 opt_file= true;
131 }
132
133 init_success= true;
134 }
135
136 bool initialized() const
137 {
138 return init_success;
139 }
140
141 int file() const
142 {
143 return fd;
144 }
145
146 void write(verbose_t verbose, const char *format, ...)
147 {
148 if (opt_file or opt_syslog)
149 {
150 va_list args;
151 va_start(args, format);
152 char mesg[BUFSIZ];
153 int mesg_length= vsnprintf(mesg, sizeof(mesg), format, args);
154 va_end(args);
155
156 if (opt_file)
157 {
158 char buffer[UTIL_MAX_ERROR_SIZE];
159 int buffer_length= snprintf(buffer, sizeof(buffer), "%7s %.*s\n", verbose_name(verbose), mesg_length, mesg);
160 if (::write(file(), buffer, buffer_length) == -1)
161 {
162 std::cerr << "Could not write to log file." << std::endl;
163 syslog(LOG_EMERG, "gearmand could not open log file %s, got error %s", filename.c_str(), strerror(errno));
164 }
165
166 }
167
168 if (opt_syslog)
169 {
170 syslog(int(verbose), "%7s %.*s", verbose_name(verbose), mesg_length, mesg);
171 }
172 }
173 }
174
175 ~log_info_st()
176 {
177 if (fd != -1 and fd != STDERR_FILENO)
178 {
179 close(fd);
180 }
181
182 if (opt_syslog)
183 {
184 closelog();
185 }
186 }
187
188 private:
189 const char *verbose_name(verbose_t verbose)
190 {
191 switch (verbose)
192 {
193 case VERBOSE_FATAL:
194 return "FATAL";
195
196 case VERBOSE_ALERT:
197 return "ALERT";
198
199 case VERBOSE_CRITICAL:
200 return "CRITICAL";
201
202 case VERBOSE_ERROR:
203 return "ERROR";
204
205 case VERBOSE_WARN:
206 return "WARNING";
207
208 case VERBOSE_NOTICE:
209 return "NOTICE";
210
211 case VERBOSE_INFO:
212 return "INFO";
213
214 case VERBOSE_DEBUG:
215 return "DEBUG";
216
217 default:
218 break;
219 }
220
221 return "UNKNOWN";
222 }
223 };
224
225 } // namespace util
226 } // namespace datadifferential