1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
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.
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.
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
32 #define UTIL_MAX_ERROR_SIZE 2048
34 namespace datadifferential {
41 // Logging this will cause shutdown
42 VERBOSE_FATAL= LOG_EMERG, // syslog:LOG_EMERG
44 VERBOSE_ALERT= LOG_ALERT, // syslog:LOG_ALERT
45 VERBOSE_CRITICAL= LOG_CRIT, // syslog:LOG_CRIT
47 VERBOSE_ERROR= LOG_ERR, // syslog:LOG_ERR
49 VERBOSE_WARN= LOG_WARNING, // syslog:LOG_WARNING
51 VERBOSE_NOTICE= LOG_NOTICE, // syslog:LOG_NOTICE
53 VERBOSE_INFO= LOG_INFO, // syslog:LOG_INFO
55 VERBOSE_DEBUG= LOG_DEBUG // syslog:LOG_DEBUG
68 log_info_st(const std::string& name_arg, const std::string &filename_arg, bool syslog_arg) :
70 filename(filename_arg),
72 opt_syslog(syslog_arg),
78 openlog(name.c_str(), LOG_PID | LOG_NDELAY, LOG_USER);
88 if (filename.compare("stderr") == 0)
94 fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
100 char *getcwd_ret= getcwd(buffer, sizeof(buffer));
101 syslog(LOG_ERR, "Could not open log file \"%.*s\", from \"%s\", open failed with (%s)",
102 int(filename.size()), filename.c_str(),
106 std::cerr << "Could not open log file for writing, switching to stderr." << std::endl;
118 bool initialized() const
128 void write(verbose_t verbose, const char *format, ...)
130 if (opt_file or opt_syslog)
133 va_start(args, format);
135 int mesg_length= vsnprintf(mesg, sizeof(mesg), format, args);
140 char buffer[UTIL_MAX_ERROR_SIZE];
141 int buffer_length= snprintf(buffer, sizeof(buffer), "%7s %.*s\n", verbose_name(verbose), mesg_length, mesg);
142 if (::write(file(), buffer, buffer_length) == -1)
144 std::cerr << "Could not write to log file." << std::endl;
145 syslog(LOG_EMERG, "gearmand could not open log file %s, got error %s", filename.c_str(), strerror(errno));
152 syslog(int(verbose), "%7s %.*s", verbose_name(verbose), mesg_length, mesg);
159 if (fd != -1 and fd != STDERR_FILENO)
171 const char *verbose_name(verbose_t verbose)
181 case VERBOSE_CRITICAL:
208 } // namespace datadifferential