2 * @brief Debug logging macros.
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
23 #ifdef XAPIAN_DEBUG_LOG
27 #include "errno_to_string.h"
30 #include <sys/types.h>
31 #include "safefcntl.h"
32 #include "safesysstat.h"
33 #include "safeunistd.h"
36 #include <cstdlib> // For getenv().
41 DebugLogger xapian_debuglogger_
;
43 DebugLogger::~DebugLogger()
45 LOGLINE(ALWAYS
, PACKAGE_STRING
": debug log ended");
49 DebugLogger::initialise_categories_mask()
52 const char* f
= getenv("XAPIAN_DEBUG_LOG");
55 if (f
[0] == '-' && f
[1] == '\0') {
56 // Filename "-" means "log to stderr".
63 // Replace %p in the filename with the process id.
64 if (pid
.empty()) pid
= str(getpid());
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
73 // We use O_DSYNC in preference if available - updating
74 // the log file's mtime isn't important.
87 flags
|= O_CREAT
|O_WRONLY
|O_APPEND
|O_CLOEXEC
;
88 fd
= open(fnm
.c_str(), flags
, 0644);
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
95 LOGLINE(ALWAYS
, PACKAGE_STRING
": Failed to open debug log '"
96 << fnm
<< "' (" << errno_to_string(errno
) << ')');
102 const char* v
= getenv("XAPIAN_DEBUG_FLAGS");
104 bool toggle
= (*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");
119 DebugLogger::log_line(debuglog_categories category
, const string
& msg
)
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
;
128 line
.reserve(9 + indent_level
+ msg
.size());
129 line
= char(category
) + '@';
131 line
+= str(getpid());
132 line
.append(indent_level
+ 1, ' ');
136 const char* p
= line
.data();
137 size_t to_do
= line
.size();
139 ssize_t n
= write(fd
, p
, to_do
);
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
148 LOGLINE(ALWAYS
, PACKAGE_STRING
": Failed to write log output ("
149 << errno_to_string(errno
) << ')');
160 #endif // XAPIAN_DEBUG_LOG