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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
#include "c-calls.hh"
#include "lix/libutil/config-impl.hh"
#include "lix/libutil/environment-variables.hh"
#include "lix/libutil/file-descriptor.hh"
#include "lix/libutil/logging.hh"
#include "lix/libutil/config.hh"
#include "lix/libutil/json.hh"
#include "lix/libutil/position.hh"
#include "lix/libutil/terminal.hh"
#include "manually-drop.hh"
#include <algorithm>
#include <atomic>
#include <mutex>
#include <optional>
#include <sstream>
#include <syslog.h>
namespace nix {
LoggerSettings loggerSettings;
Logger * logger = makeSimpleLogger(true);
Activity Logger::startActivity(
Verbosity lvl,
ActivityType type,
const std::string & s,
const Fields & fields,
const Activity * parent
)
{
Activity result{*this};
// NOTE we assume that activities aren't started in monstrous numbers, so the inevitable
// state updates (via progress, result, or setExpected) should be enough to flush stuff.
(void) startActivityImpl(result.id, lvl, type, s, fields, parent ? parent->id : 0);
return result;
}
Activity Logger::startActivity(ActivityType type, const Fields & fields, const Activity * parent)
{
return startActivity(lvlError, type, "", fields, parent);
}
void Logger::writeToStdout(std::string_view s)
{
writeFull(
STDOUT_FILENO,
filterANSIEscapes(
s,
!shouldANSI(StandardOutputStream::Stdout),
std::numeric_limits<unsigned int>::max(),
false
)
);
writeFull(STDOUT_FILENO, "\n");
}
class SimpleLogger : public Logger
{
public:
bool systemd, tty;
bool printBuildLogs;
SimpleLogger(bool printBuildLogs)
: printBuildLogs(printBuildLogs)
{
systemd = getEnv("IN_SYSTEMD") == "1";
tty = shouldANSI();
}
bool isVerbose() override {
return printBuildLogs;
}
BufferState log(Verbosity lvl, std::string_view s) override
{
if (lvl > verbosity) {
return BufferState::HasSpace;
}
std::string prefix;
if (systemd) {
char c;
switch (lvl) {
case lvlError: c = '3'; break;
case lvlWarn: c = '4'; break;
case lvlNotice: case lvlInfo: c = '5'; break;
case lvlTalkative: case lvlChatty: c = '6'; break;
case lvlDebug: case lvlVomit:
default: c = '7'; break; // default case should not happen, and missing enum case is reported by -Werror=switch-enum
}
prefix = std::string("<") + c + ">";
}
writeLogsToStderr(prefix + filterANSIEscapes(s, !tty) + "\n");
return BufferState::HasSpace;
}
BufferState logEI(const ErrorInfo & ei) override
{
std::stringstream oss;
showErrorInfo(oss, ei, loggerSettings.showTrace.get());
return log(ei.level, oss.str());
}
BufferState startActivityImpl(
ActivityId act,
Verbosity lvl,
ActivityType type,
const std::string & s,
const Fields & fields,
ActivityId parent
) override
{
if (lvl <= verbosity && !s.empty()) {
return log(lvl, s + "...");
}
return BufferState::HasSpace;
}
BufferState resultImpl(ActivityId act, ResultType type, const Fields & fields) override
{
if (type == resBuildLogLine && printBuildLogs) {
auto lastLine = fields[0].s;
printError("%1%", Uncolored(lastLine));
}
else if (type == resPostBuildLogLine) {
auto lastLine = fields[0].s;
printError("post-build-hook: %1%", Uncolored(lastLine));
}
return BufferState::HasSpace;
}
};
Verbosity verbosity = lvlInfo;
Verbosity verbosityFromIntClamped(int val)
{
int clamped = std::clamp(val, int(lvlError), int(lvlVomit));
return static_cast<Verbosity>(clamped);
}
Logger * makeSimpleLogger(bool printBuildLogs)
{
return new SimpleLogger(printBuildLogs);
}
std::atomic<uint64_t> nextId{0};
Activity::Activity(Logger & logger) : logger(&logger), id(nextId++ + (((uint64_t) getpid()) << 32))
{
}
void to_json(JSON & json, std::shared_ptr<Pos> pos)
{
if (pos) {
json["line"] = pos->line;
json["column"] = pos->column;
std::ostringstream str;
pos->print(str, true);
json["file"] = str.str();
} else {
json["line"] = nullptr;
json["column"] = nullptr;
json["file"] = nullptr;
}
}
struct JSONLogger : Logger {
Logger & prevLogger;
JSONLogger(Logger & prevLogger) : prevLogger(prevLogger) { }
bool isVerbose() override {
return true;
}
void addFields(JSON & json, const Fields & fields)
{
if (fields.empty()) return;
auto & arr = json["fields"] = JSON::array();
for (auto & f : fields)
if (f.type == Logger::Field::tInt)
arr.push_back(f.i);
else if (f.type == Logger::Field::tString)
arr.push_back(f.s);
else
abort();
}
BufferState write(const JSON & json)
{
return prevLogger.log(
lvlError, "@nix " + json.dump(-1, ' ', false, JSON::error_handler_t::replace)
);
}
BufferState log(Verbosity lvl, std::string_view s) override
{
JSON json;
json["action"] = "msg";
json["level"] = lvl;
json["msg"] = s;
return write(json);
}
BufferState logEI(const ErrorInfo & ei) override
{
std::ostringstream oss;
showErrorInfo(oss, ei, loggerSettings.showTrace.get());
JSON json;
json["action"] = "msg";
json["level"] = ei.level;
json["msg"] = oss.str();
json["raw_msg"] = ei.msg.str();
to_json(json, ei.pos);
if (loggerSettings.showTrace.get() && !ei.traces.empty()) {
JSON traces = JSON::array();
for (auto iter = ei.traces.rbegin(); iter != ei.traces.rend(); ++iter) {
JSON stackFrame;
stackFrame["raw_msg"] = iter->hint.str();
to_json(stackFrame, iter->pos);
traces.push_back(stackFrame);
}
json["trace"] = traces;
}
return write(json);
}
BufferState startActivityImpl(
ActivityId act,
Verbosity lvl,
ActivityType type,
const std::string & s,
const Fields & fields,
ActivityId parent
) override
{
JSON json;
json["action"] = "start";
json["id"] = act;
json["level"] = lvl;
json["type"] = type;
json["text"] = s;
json["parent"] = parent;
addFields(json, fields);
return write(json);
}
BufferState stopActivityImpl(ActivityId act) override
{
JSON json;
json["action"] = "stop";
json["id"] = act;
return write(json);
}
BufferState resultImpl(ActivityId act, ResultType type, const Fields & fields) override
{
JSON json;
json["action"] = "result";
json["id"] = act;
json["type"] = type;
addFields(json, fields);
return write(json);
}
};
Logger * makeJSONLogger(Logger & prevLogger)
{
return new JSONLogger(prevLogger);
}
MakeError(StructuredLogError, Error);
static Logger::Fields getFields(JSON & json)
{
Logger::Fields fields;
for (auto & f : json) {
if (f.type() == JSON::value_t::number_unsigned)
fields.emplace_back(Logger::Field(f.get<uint64_t>()));
else if (f.type() == JSON::value_t::string)
fields.emplace_back(Logger::Field(f.get<std::string>()));
else throw StructuredLogError("unsupported log field type '%s'", f.type_name());
}
return fields;
}
std::optional<JSON> parseJSONMessage(const std::string & msg, std::string_view source)
{
if (!msg.starts_with("@nix ")) return std::nullopt;
try {
return json::parse(std::string(msg, 5));
} catch (std::exception & e) { // NOLINT(lix-foreign-exceptions)
printError("bad JSON log message from %s: %s",
Uncolored(source),
e.what());
}
return std::nullopt;
}
std::optional<Logger::BufferState> handleJSONLogMessage(
JSON & json,
const Activity & act,
std::map<ActivityId, Activity> & activities,
std::string_view source
)
{
try {
std::string action = json["action"];
if (action == "start") {
auto type = (ActivityType) json["type"];
activities.emplace(
json["id"],
act.addChild(
(Verbosity) json["level"], type, json["text"], getFields(json["fields"])
)
);
}
else if (action == "stop")
activities.erase((ActivityId) json["id"]);
else if (action == "result") {
auto i = activities.find((ActivityId) json["id"]);
if (i != activities.end())
return i->second.result((ResultType) json["type"], getFields(json["fields"]));
}
else if (action == "setPhase") {
std::string phase = json["phase"];
return act.result(resSetPhase, phase);
}
else if (action == "msg") {
std::string msg = json["msg"];
return logger->log((Verbosity) json["level"], msg);
}
return Logger::BufferState::HasSpace;
} catch (json::JSONError & e) {
printTaggedWarning(
"Unable to handle a JSON message from %s: %s", Uncolored(source), e.what()
);
return std::nullopt;
} catch (StructuredLogError & e) {
printTaggedWarning(
"Unable to handle a JSON message from %s: %s", Uncolored(source), e.what()
);
return std::nullopt;
}
}
std::optional<Logger::BufferState> handleJSONLogMessage(
const std::string & msg,
const Activity & act,
std::map<ActivityId, Activity> & activities,
std::string_view source
)
{
auto json = parseJSONMessage(msg, source);
if (!json) {
return std::nullopt;
}
return handleJSONLogMessage(*json, act, activities, source);
}
Activity::~Activity()
{
if (!logger) {
return;
}
try {
// NOTE we can't flush here, and async destruction of activities is bound to fail
// at some point. eventually something will flush the buffer for us (see also the
// startActivity comment, we also don't flush buffers there even when they fill.)
(void) logger->stopActivityImpl(id);
} catch (...) {
ignoreExceptionInDestructor();
}
}
void writeLogsToStderr(std::string_view s)
{
// NOTE: If this lock is a regular static item (and not something
// indestructible), then it will be destructed when Nix shuts down. When
// other threads are running, it becomes possible for a static to be
// destructed before Nix ends, leading to errors.
//
// Therefore, we use a wrapper type to block it ever getting destroyed.
//
// TODO: Audit other statics for this issue?
//
// See: https://git.lix.systems/lix-project/lix/issues/702
// See: https://stackoverflow.com/a/27671727/5719760
static ManuallyDrop<std::mutex> lock {std::in_place_t{}};
// make sure only one thread uses this function at any given time.
// multiple concurrent threads can have deleterious effects on log
// output, especially when layering structured formats (like JSON)
// on top of a SimpleLogger which is itself not thread-safe. every
// Logger instance should be thread-safe in an ideal world, but we
// cannot really enforce that on a per-logger level at this point.
std::unique_lock _lock(*lock);
try {
writeFull(STDERR_FILENO, s, false);
} catch (SysError & e) {
/* Ignore failing writes to stderr. We need to ignore write
errors to ensure that cleanup code that logs to stderr runs
to completion if the other side of stderr has been closed
unexpectedly. */
}
}
void logFatal(std::string const & s)
{
writeLogsToStderr(s + "\n");
// std::string for guaranteed null termination
syslog(LOG_CRIT, "%s", requireCString(s).asCStr());
}
std::optional<std::string> LogLineSplitter::feed(std::string_view & input)
{
for (auto [idx, c] : enumerate(input)) {
if (c == '\r') {
pos = 0;
} else if (c == '\n') {
input = input.substr(idx + 1);
return finish();
} else {
if (pos >= line.size()) {
line.resize(pos + 1);
}
line[pos++] = c;
}
}
input = {};
return std::nullopt;
}
std::string LogLineSplitter::finish()
{
pos = 0;
return std::move(line);
}
}
|