Merge in Ubuntu fixes.
[m6w6/libmemcached] / util / log.hpp
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #pragma once
23
24 #include <cerrno>
25 #include <cstdio>
26 #include <fcntl.h>
27 #include <iostream>
28 #include <string>
29 #include <syslog.h>
30
31 #define UTIL_MAX_ERROR_SIZE 2048
32
33 namespace datadifferential {
34 namespace util {
35
36 /** Verbosity levels.
37 */
38 enum verbose_t
39 {
40 // Logging this will cause shutdown
41 VERBOSE_FATAL= LOG_EMERG, // syslog:LOG_EMERG
42
43 VERBOSE_ALERT= LOG_ALERT, // syslog:LOG_ALERT
44 VERBOSE_CRITICAL= LOG_CRIT, // syslog:LOG_CRIT
45
46 VERBOSE_ERROR= LOG_ERR, // syslog:LOG_ERR
47
48 VERBOSE_WARN= LOG_WARNING, // syslog:LOG_WARNING
49
50 VERBOSE_NOTICE= LOG_NOTICE, // syslog:LOG_NOTICE
51
52 VERBOSE_INFO= LOG_INFO, // syslog:LOG_INFO
53
54 VERBOSE_DEBUG= LOG_DEBUG // syslog:LOG_DEBUG
55 };
56
57
58 struct log_info_st
59 {
60 std::string name;
61 std::string filename;
62 int fd;
63 bool opt_syslog;
64 bool opt_file;
65 bool init_success;
66
67 log_info_st(const std::string& name_arg, const std::string &filename_arg, bool syslog_arg) :
68 name(name_arg),
69 filename(filename_arg),
70 fd(-1),
71 opt_syslog(syslog_arg),
72 opt_file(false),
73 init_success(false)
74 {
75 if (opt_syslog)
76 {
77 openlog(name.c_str(), LOG_PID | LOG_NDELAY, LOG_USER);
78 }
79
80 init();
81 }
82
83 void init()
84 {
85 if (filename.size())
86 {
87 if (filename.compare("stderr") == 0)
88 {
89 fd= STDERR_FILENO;
90 }
91 else
92 {
93 fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
94 if (fd == -1)
95 {
96 if (opt_syslog)
97 {
98 char buffer[1024];
99 (void)getcwd(buffer, sizeof(buffer));
100 syslog(LOG_ERR, "Could not open log file \"%.*s\", from \"%s\", open failed with (%s)",
101 int(filename.size()), filename.c_str(),
102 buffer,
103 strerror(errno));
104 }
105 std::cerr << "Could not open log file for writing, switching to stderr." << std::endl;
106
107 fd= STDERR_FILENO;
108 }
109 }
110
111 opt_file= true;
112 }
113
114 init_success= true;
115 }
116
117 bool initialized() const
118 {
119 return init_success;
120 }
121
122 int file() const
123 {
124 return fd;
125 }
126
127 void write(verbose_t verbose, const char *mesg)
128 {
129 if (opt_file)
130 {
131 char buffer[UTIL_MAX_ERROR_SIZE];
132 int buffer_length= snprintf(buffer, sizeof(buffer), "%7s %s\n", verbose_name(verbose), mesg);
133 if (::write(file(), buffer, buffer_length) == -1)
134 {
135 std::cerr << "Could not write to log file." << std::endl;
136 syslog(LOG_EMERG, "gearmand could not open log file %s, got error %s", filename.c_str(), strerror(errno));
137 }
138
139 }
140
141 if (opt_syslog)
142 {
143 syslog(int(verbose), "%7s %s", verbose_name(verbose), mesg);
144 }
145 }
146
147 ~log_info_st()
148 {
149 if (fd != -1 and fd != STDERR_FILENO)
150 {
151 close(fd);
152 }
153
154 if (opt_syslog)
155 {
156 closelog();
157 }
158 }
159
160 private:
161 const char *verbose_name(verbose_t verbose)
162 {
163 switch (verbose)
164 {
165 case VERBOSE_FATAL:
166 return "FATAL";
167
168 case VERBOSE_ALERT:
169 return "ALERT";
170
171 case VERBOSE_CRITICAL:
172 return "CRITICAL";
173
174 case VERBOSE_ERROR:
175 return "ERROR";
176
177 case VERBOSE_WARN:
178 return "WARNING";
179
180 case VERBOSE_NOTICE:
181 return "NOTICE";
182
183 case VERBOSE_INFO:
184 return "INFO";
185
186 case VERBOSE_DEBUG:
187 return "DEBUG";
188
189 default:
190 break;
191 }
192
193 return "UNKNOWN";
194 }
195 };
196
197 } // namespace util
198 } // namespace datadifferential