Update for 1.4.18
[xapian.git] / xapian-core / common / debuglog.cc
blob88e926dad131109c37aefdb998d2b2cef3be675f
1 /** @file
2 * @brief Debug logging macros.
3 */
4 /* Copyright (C) 2008,2011,2012,2014,2015,2019 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
23 #ifdef XAPIAN_DEBUG_LOG
25 #include "debuglog.h"
27 #include "errno_to_string.h"
28 #include "str.h"
30 #include <sys/types.h>
31 #include "safefcntl.h"
32 #include "safesysstat.h"
33 #include "safeunistd.h"
35 #include <cerrno>
36 #include <cstdlib> // For getenv().
37 #include <string>
39 using namespace std;
41 DebugLogger xapian_debuglogger_;
43 DebugLogger::~DebugLogger()
45 LOGLINE(ALWAYS, PACKAGE_STRING": debug log ended");
48 void
49 DebugLogger::initialise_categories_mask()
51 fd = -2;
52 const char* f = getenv("XAPIAN_DEBUG_LOG");
53 int flags = 0;
54 if (f && *f) {
55 if (f[0] == '-' && f[1] == '\0') {
56 // Filename "-" means "log to stderr".
57 fd = 2;
58 } else {
59 string fnm, pid;
60 while (*f) {
61 if (*f == '%') {
62 if (f[1] == 'p') {
63 // Replace %p in the filename with the process id.
64 if (pid.empty()) pid = str(getpid());
65 fnm += pid;
66 f += 2;
67 continue;
68 } else if (f[1] == '!') {
69 // %! in the filename means we should attempt to ensure
70 // that debug output is written to disk so that none is
71 // lost if we crash.
73 // We use O_DSYNC in preference if available - updating
74 // the log file's mtime isn't important.
75 #if O_DSYNC - 0 != 0
76 flags = O_DSYNC;
77 #elif O_SYNC - 0 != 0
78 flags = O_SYNC;
79 #endif
80 f += 2;
81 continue;
84 fnm += *f++;
87 flags |= O_CREAT|O_WRONLY|O_APPEND|O_CLOEXEC;
88 fd = open(fnm.c_str(), flags, 0644);
89 if (fd == -1) {
90 // If we failed to open the log file, report to stderr, but
91 // don't spew all the log output to stderr too or else the
92 // user will probably miss the message about the debug log
93 // failing to open!
94 fd = 2;
95 LOGLINE(ALWAYS, PACKAGE_STRING": Failed to open debug log '"
96 << fnm << "' (" << errno_to_string(errno) << ')');
97 fd = -2;
101 if (fd >= 0) {
102 const char* v = getenv("XAPIAN_DEBUG_FLAGS");
103 if (v) {
104 bool toggle = (*v == '-');
105 if (toggle) ++v;
106 categories_mask = 0;
107 while (*v) {
108 int ch = *v++ - '@';
109 if (ch > 0 && ch <= 26) categories_mask |= 1ul << ch;
111 if (toggle) categories_mask ^= 0xffffffff;
115 LOGLINE(ALWAYS, PACKAGE_STRING": debug log started");
118 void
119 DebugLogger::log_line(debuglog_categories category, const string& msg)
121 if (fd < 0) return;
123 // Preserve errno over logging calls, so they can safely be added to code
124 // which expects errno not to change.
125 int saved_errno = errno;
127 string line;
128 line.reserve(9 + indent_level + msg.size());
129 line = char(category) + '@';
130 line += ' ';
131 line += str(getpid());
132 line.append(indent_level + 1, ' ');
133 line += msg;
134 line += '\n';
136 const char* p = line.data();
137 size_t to_do = line.size();
138 while (to_do) {
139 ssize_t n = write(fd, p, to_do);
140 if (n < 0) {
141 // Retry if interrupted by a signal.
142 if (errno == EINTR) continue;
144 // Upon other errors, close the log file, moan to stderr, and stop
145 // logging.
146 (void)close(fd);
147 fd = 2;
148 LOGLINE(ALWAYS, PACKAGE_STRING": Failed to write log output ("
149 << errno_to_string(errno) << ')');
150 fd = -2;
151 break;
153 p += n;
154 to_do -= n;
157 errno = saved_errno;
160 #endif // XAPIAN_DEBUG_LOG