46ddaf450e6c73015707b4321adb57904a9c14d8
[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
74 struct log_info_st
75 {
76 std::string name;
77 std::string filename;
78 int fd;
79 bool opt_syslog;
80 bool opt_file;
81 bool init_success;
82
83 log_info_st(const std::string& name_arg, const std::string &filename_arg, bool syslog_arg) :
84 name(name_arg),
85 filename(filename_arg),
86 fd(-1),
87 opt_syslog(syslog_arg),
88 opt_file(false),
89 init_success(false)
90 {
91 if (opt_syslog)
92 {
93 openlog(name.c_str(), LOG_PID | LOG_NDELAY, LOG_USER);
94 }
95
96 init();
97 }
98
99 void init()
100 {
101 if (filename.size())
102 {
103 if (filename.compare("stderr") == 0)
104 {
105 fd= STDERR_FILENO;
106 }
107 else
108 {
109 fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
110 if (fd == -1)
111 {
112 if (opt_syslog)
113 {
114 char buffer[1024];
115 char *getcwd_ret= getcwd(buffer, sizeof(buffer));
116 syslog(LOG_ERR, "Could not open log file \"%.*s\", from \"%s\", open failed with (%s)",
117 int(filename.size()), filename.c_str(),
118 getcwd_ret,
119 strerror(errno));
120 }
121 std::cerr << "Could not open log file for writing, switching to stderr." << std::endl;
122
123 fd= STDERR_FILENO;
124 }
125 }
126
127 opt_file= true;
128 }
129
130 init_success= true;
131 }
132
133 bool initialized() const
134 {
135 return init_success;
136 }
137
138 int file() const
139 {
140 return fd;
141 }
142
143 void write(verbose_t verbose, const char *format, ...)
144 {
145 if (opt_file or opt_syslog)
146 {
147 va_list args;
148 va_start(args, format);
149 char mesg[BUFSIZ];
150 int mesg_length= vsnprintf(mesg, sizeof(mesg), format, args);
151 va_end(args);
152
153 if (opt_file)
154 {
155 char buffer[UTIL_MAX_ERROR_SIZE];
156 int buffer_length= snprintf(buffer, sizeof(buffer), "%7s %.*s\n", verbose_name(verbose), mesg_length, mesg);
157 if (::write(file(), buffer, buffer_length) == -1)
158 {
159 std::cerr << "Could not write to log file." << std::endl;
160 syslog(LOG_EMERG, "gearmand could not open log file %s, got error %s", filename.c_str(), strerror(errno));
161 }
162
163 }
164
165 if (opt_syslog)
166 {
167 syslog(int(verbose), "%7s %.*s", verbose_name(verbose), mesg_length, mesg);
168 }
169 }
170 }
171
172 ~log_info_st()
173 {
174 if (fd != -1 and fd != STDERR_FILENO)
175 {
176 close(fd);
177 }
178
179 if (opt_syslog)
180 {
181 closelog();
182 }
183 }
184
185 private:
186 const char *verbose_name(verbose_t verbose)
187 {
188 switch (verbose)
189 {
190 case VERBOSE_FATAL:
191 return "FATAL";
192
193 case VERBOSE_ALERT:
194 return "ALERT";
195
196 case VERBOSE_CRITICAL:
197 return "CRITICAL";
198
199 case VERBOSE_ERROR:
200 return "ERROR";
201
202 case VERBOSE_WARN:
203 return "WARNING";
204
205 case VERBOSE_NOTICE:
206 return "NOTICE";
207
208 case VERBOSE_INFO:
209 return "INFO";
210
211 case VERBOSE_DEBUG:
212 return "DEBUG";
213
214 default:
215 break;
216 }
217
218 return "UNKNOWN";
219 }
220 };
221
222 } // namespace util
223 } // namespace datadifferential