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
31 #define UTIL_MAX_ERROR_SIZE 2048
33 namespace datadifferential {
40 // Logging this will cause shutdown
41 VERBOSE_FATAL= LOG_EMERG, // syslog:LOG_EMERG
43 VERBOSE_ALERT= LOG_ALERT, // syslog:LOG_ALERT
44 VERBOSE_CRITICAL= LOG_CRIT, // syslog:LOG_CRIT
46 VERBOSE_ERROR= LOG_ERR, // syslog:LOG_ERR
48 VERBOSE_WARN= LOG_WARNING, // syslog:LOG_WARNING
50 VERBOSE_NOTICE= LOG_NOTICE, // syslog:LOG_NOTICE
52 VERBOSE_INFO= LOG_INFO, // syslog:LOG_INFO
54 VERBOSE_DEBUG= LOG_DEBUG // syslog:LOG_DEBUG
67 log_info_st(const std::string& name_arg, const std::string &filename_arg, bool syslog_arg) :
69 filename(filename_arg),
71 opt_syslog(syslog_arg),
77 openlog(name.c_str(), LOG_PID | LOG_NDELAY, LOG_USER);
87 if (filename.compare("stderr") == 0)
93 fd= open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0644);
99 char *getcwd_ret= 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(),
105 std::cerr << "Could not open log file for writing, switching to stderr." << std::endl;
117 bool initialized() const
127 void write(verbose_t verbose, const char *mesg)
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)
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));
143 syslog(int(verbose), "%7s %s", verbose_name(verbose), mesg);
149 if (fd != -1 and fd != STDERR_FILENO)
161 const char *verbose_name(verbose_t verbose)
171 case VERBOSE_CRITICAL:
198 } // namespace datadifferential