forked from ccache/ccache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logging.cpp
189 lines (164 loc) · 4.32 KB
/
Logging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (C) 2002 Andrew Tridgell
// Copyright (C) 2009-2020 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "Logging.hpp"
#include "Config.hpp"
#include "File.hpp"
#include "Util.hpp"
#include "exceptions.hpp"
#include "execute.hpp"
#ifdef HAVE_SYSLOG_H
# include <syslog.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef __linux__
# ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
# endif
#endif
#ifdef _WIN32
# include <psapi.h>
# include <sys/locking.h>
# include <tchar.h>
#endif
using nonstd::string_view;
namespace {
// Logfile path and file handle, read from Config::log_file().
std::string logfile_path;
File logfile;
// Whether to use syslog() instead.
bool use_syslog;
// Buffer used for logs in debug mode.
std::string debug_log_buffer;
// Whether debug logging is enabled via configuration or environment variable.
bool debug_log_enabled = false;
// Print error message to stderr about failure writing to the log file and exit
// with failure.
[[noreturn]] void
print_fatal_error_and_exit()
{
// Note: Can't throw Fatal since that would lead to recursion.
fmt::print(stderr,
"ccache: error: Failed to write to {}: {}\n",
logfile_path,
strerror(errno));
exit(EXIT_FAILURE);
}
void
do_log(string_view message, bool bulk)
{
static char prefix[200];
if (!bulk) {
char timestamp[100];
struct timeval tv;
gettimeofday(&tv, nullptr);
auto tm = Util::localtime(tv.tv_sec);
if (tm) {
strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", &*tm);
} else {
snprintf(timestamp, sizeof(timestamp), "%lu", tv.tv_sec);
}
snprintf(prefix,
sizeof(prefix),
"[%s.%06d %-5d] ",
timestamp,
static_cast<int>(tv.tv_usec),
static_cast<int>(getpid()));
}
if (logfile
&& (fputs(prefix, *logfile) == EOF
|| fwrite(message.data(), message.length(), 1, *logfile) != 1
|| fputc('\n', *logfile) == EOF
|| (!bulk && fflush(*logfile) == EOF))) {
print_fatal_error_and_exit();
}
#ifdef HAVE_SYSLOG
if (use_syslog) {
// Note: No log prefix since syslog will add a prefix of its own.
syslog(
LOG_DEBUG, "%.*s", static_cast<int>(message.length()), message.data());
// Note: No trailing newline.
}
#endif
if (debug_log_enabled) {
debug_log_buffer += prefix;
debug_log_buffer.append(message.data(), message.length());
debug_log_buffer += '\n';
}
}
} // namespace
namespace Logging {
// Initialize logging. Call only once.
void
init(const Config& config)
{
debug_log_enabled = config.debug();
#ifdef HAVE_SYSLOG
if (config.log_file() == "syslog") {
use_syslog = true;
openlog("ccache", LOG_PID, LOG_USER);
return; // Don't open logfile
}
#endif
if (!config.log_file().empty()) {
logfile_path = config.log_file();
logfile.open(logfile_path, "a");
if (logfile) {
Util::set_cloexec_flag(fileno(*logfile));
} else {
print_fatal_error_and_exit();
}
}
}
bool
enabled()
{
return debug_log_enabled || logfile || use_syslog;
}
void
log(string_view message)
{
if (!enabled()) {
return;
}
do_log(message, false);
}
void
bulk_log(string_view message)
{
if (!enabled()) {
return;
}
do_log(message, true);
}
void
dump_log(const std::string& path)
{
if (!enabled()) {
return;
}
File file(path, "w");
if (file) {
(void)fwrite(debug_log_buffer.data(), debug_log_buffer.length(), 1, *file);
} else {
log("Failed to open {}: {}", path, strerror(errno));
}
}
} // namespace Logging