1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential Utility library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
47 #define UTIL_MAX_ERROR_SIZE 2048
49 namespace datadifferential {
56 // Logging this will cause shutdown
57 VERBOSE_FATAL= LOG_EMERG, // syslog:LOG_EMERG
59 VERBOSE_ALERT= LOG_ALERT, // syslog:LOG_ALERT
60 VERBOSE_CRITICAL= LOG_CRIT, // syslog:LOG_CRIT
62 VERBOSE_ERROR= LOG_ERR, // syslog:LOG_ERR
64 VERBOSE_WARN= LOG_WARNING, // syslog:LOG_WARNING
66 VERBOSE_NOTICE= LOG_NOTICE, // syslog:LOG_NOTICE
68 VERBOSE_INFO= LOG_INFO, // syslog:LOG_INFO
70 VERBOSE_DEBUG= LOG_DEBUG // syslog:LOG_DEBUG
73 #ifndef __INTEL_COMPILER
74 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
86 log_info_st(const std::string& name_arg, const std::string &filename_arg, bool syslog_arg) :
88 filename(filename_arg),
90 opt_syslog(syslog_arg),
96 openlog(name.c_str(), LOG_PID | LOG_NDELAY, LOG_USER);
106 if (filename.compare("stderr") == 0)
112 fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
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(),
124 std::cerr << "Could not open log file for writing, switching to stderr." << std::endl;
136 bool initialized() const
146 void write(verbose_t verbose, const char *format, ...)
148 if (opt_file or opt_syslog)
151 va_start(args, format);
153 int mesg_length= vsnprintf(mesg, sizeof(mesg), format, args);
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)
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));
170 syslog(int(verbose), "%7s %.*s", verbose_name(verbose), mesg_length, mesg);
177 if (fd != -1 and fd != STDERR_FILENO)
189 const char *verbose_name(verbose_t verbose)
199 case VERBOSE_CRITICAL:
226 } // namespace datadifferential