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
83 log_info_st(const std::string& name_arg, const std::string &filename_arg, bool syslog_arg) :
85 filename(filename_arg),
87 opt_syslog(syslog_arg),
93 openlog(name.c_str(), LOG_PID | LOG_NDELAY, LOG_USER);
103 if (filename.compare("stderr") == 0)
109 fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
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(),
121 std::cerr << "Could not open log file for writing, switching to stderr." << std::endl;
133 bool initialized() const
143 void write(verbose_t verbose, const char *format, ...)
145 if (opt_file or opt_syslog)
148 va_start(args, format);
150 int mesg_length= vsnprintf(mesg, sizeof(mesg), format, args);
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)
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));
167 syslog(int(verbose), "%7s %.*s", verbose_name(verbose), mesg_length, mesg);
174 if (fd != -1 and fd != STDERR_FILENO)
186 const char *verbose_name(verbose_t verbose)
196 case VERBOSE_CRITICAL:
223 } // namespace datadifferential