2 * @brief Debug logging macros.
4 /* Copyright (C) 2008,2011,2012,2014,2015 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>
32 #include "safeerrno.h"
33 #include "safefcntl.h"
34 #include "safeunistd.h"
36 #include <cstdlib> // For getenv().
41 DebugLogger xapian_debuglogger_
;
43 // We use O_SYNC to attempt to ensure that debug output is written to disk so
44 // that none is lost if we crash. Some platforms (e.g. mingw) don't support
45 // O_SYNC, so on these we just don't use it.
50 DebugLogger::~DebugLogger()
52 LOGLINE(ALWAYS
, PACKAGE_STRING
": debug log ended");
56 DebugLogger::initialise_categories_mask()
59 const char * f
= getenv("XAPIAN_DEBUG_LOG");
61 if (f
[0] == '-' && f
[1] == '\0') {
62 // Filename "-" means "log to stderr".
67 if (*f
== '%' && f
[1] == 'p') {
68 // Replace %p in the filename with the process id.
69 if (pid
.empty()) pid
= str(getpid());
77 fd
= open(fnm
.c_str(), O_CREAT
|O_WRONLY
|O_SYNC
|O_APPEND
|O_CLOEXEC
, 0644);
79 // If we failed to open the log file, report to stderr, but
80 // don't spew all the log output to stderr too or else the
81 // user will probably miss the message about the debug log
85 errno_to_string(errno
, e
);
86 LOGLINE(ALWAYS
, PACKAGE_STRING
": Failed to open debug log '"
87 << fnm
<< "' (" << e
<< ')');
93 const char * v
= getenv("XAPIAN_DEBUG_FLAGS");
95 bool toggle
= (*v
== '-');
100 if (ch
> 0 && ch
<= 26) categories_mask
|= 1ul << ch
;
102 if (toggle
) categories_mask
^= 0xffffffff;
106 LOGLINE(ALWAYS
, PACKAGE_STRING
": debug log started");
110 DebugLogger::log_line(debuglog_categories category
, const string
& msg
)
114 // Preserve errno over logging calls, so they can safely be added to code
115 // which expects errno not to change.
116 int saved_errno
= errno
;
119 line
.reserve(9 + indent_level
+ msg
.size());
120 line
= char(category
) + '@';
122 line
+= str(getpid());
123 line
.append(indent_level
+ 1, ' ');
127 const char * p
= line
.data();
128 size_t to_do
= line
.size();
130 ssize_t n
= write(fd
, p
, to_do
);
132 // Retry if interrupted by a signal.
133 if (errno
== EINTR
) continue;
135 // Upon other errors, close the log file, moan to stderr, and stop
140 errno_to_string(errno
, e
);
141 LOGLINE(ALWAYS
, PACKAGE_STRING
": Failed to write log output ("
153 #endif // XAPIAN_DEBUG_LOG