Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
logging.hh
Go to the documentation of this file.
1#pragma once
3
4#include "error.hh"
5#include "config.hh"
6
7#include <nlohmann/json_fwd.hpp>
8
9namespace nix {
10
11typedef enum {
12 actUnknown = 0,
13 actCopyPath = 100,
14 actFileTransfer = 101,
15 actRealise = 102,
16 actCopyPaths = 103,
17 actBuilds = 104,
18 actBuild = 105,
19 actOptimiseStore = 106,
20 actVerifyPaths = 107,
21 actSubstitute = 108,
22 actQueryPathInfo = 109,
23 actPostBuildHook = 110,
24 actBuildWaiting = 111,
25 actFetchTree = 112,
26} ActivityType;
27
28typedef enum {
29 resFileLinked = 100,
30 resBuildLogLine = 101,
31 resUntrustedPath = 102,
32 resCorruptedPath = 103,
33 resSetPhase = 104,
34 resProgress = 105,
35 resSetExpected = 106,
36 resPostBuildLogLine = 107,
37 resFetchStatus = 108,
38} ResultType;
39
40typedef uint64_t ActivityId;
41
42struct LoggerSettings : Config
43{
44 Setting<bool> showTrace{
45 this, false, "show-trace",
46 R"(
47 Whether Nix should print out a stack trace in case of Nix
48 expression evaluation errors.
49 )"};
50};
51
52extern LoggerSettings loggerSettings;
53
54class Logger
55{
56 friend struct Activity;
57
58public:
59
60 struct Field
61 {
62 // FIXME: use std::variant.
63 enum { tInt = 0, tString = 1 } type;
64 uint64_t i = 0;
65 std::string s;
66 Field(const std::string & s) : type(tString), s(s) { }
67 Field(const char * s) : type(tString), s(s) { }
68 Field(const uint64_t & i) : type(tInt), i(i) { }
69 };
70
71 typedef std::vector<Field> Fields;
72
73 virtual ~Logger() { }
74
75 virtual void stop() { };
76
77 virtual void pause() { };
78 virtual void resume() { };
79
80 // Whether the logger prints the whole build log
81 virtual bool isVerbose() { return false; }
82
83 virtual void log(Verbosity lvl, std::string_view s) = 0;
84
85 void log(std::string_view s)
86 {
87 log(lvlInfo, s);
88 }
89
90 virtual void logEI(const ErrorInfo & ei) = 0;
91
92 void logEI(Verbosity lvl, ErrorInfo ei)
93 {
94 ei.level = lvl;
95 logEI(ei);
96 }
97
98 virtual void warn(const std::string & msg);
99
100 virtual void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
101 const std::string & s, const Fields & fields, ActivityId parent) { };
102
103 virtual void stopActivity(ActivityId act) { };
104
105 virtual void result(ActivityId act, ResultType type, const Fields & fields) { };
106
107 virtual void writeToStdout(std::string_view s);
108
109 template<typename... Args>
110 inline void cout(const Args & ... args)
111 {
112 writeToStdout(fmt(args...));
113 }
114
115 virtual std::optional<char> ask(std::string_view s)
116 { return {}; }
117
118 virtual void setPrintBuildLogs(bool printBuildLogs)
119 { }
120};
121
127struct nop
128{
129 template<typename... T> nop(T...)
130 { }
131};
132
133ActivityId getCurActivity();
134void setCurActivity(const ActivityId activityId);
135
136struct Activity
137{
138 Logger & logger;
139
140 const ActivityId id;
141
142 Activity(Logger & logger, Verbosity lvl, ActivityType type, const std::string & s = "",
143 const Logger::Fields & fields = {}, ActivityId parent = getCurActivity());
144
145 Activity(Logger & logger, ActivityType type,
146 const Logger::Fields & fields = {}, ActivityId parent = getCurActivity())
147 : Activity(logger, lvlError, type, "", fields, parent) { };
148
149 Activity(const Activity & act) = delete;
150
151 ~Activity();
152
153 void progress(uint64_t done = 0, uint64_t expected = 0, uint64_t running = 0, uint64_t failed = 0) const
154 { result(resProgress, done, expected, running, failed); }
155
156 void setExpected(ActivityType type2, uint64_t expected) const
157 { result(resSetExpected, type2, expected); }
158
159 template<typename... Args>
160 void result(ResultType type, const Args & ... args) const
161 {
162 Logger::Fields fields;
163 nop{(fields.emplace_back(Logger::Field(args)), 1)...};
164 result(type, fields);
165 }
166
167 void result(ResultType type, const Logger::Fields & fields) const
168 {
169 logger.result(id, type, fields);
170 }
171
172 friend class Logger;
173};
174
175struct PushActivity
176{
177 const ActivityId prevAct;
178 PushActivity(ActivityId act) : prevAct(getCurActivity()) { setCurActivity(act); }
179 ~PushActivity() { setCurActivity(prevAct); }
180};
181
182extern Logger * logger;
183
184Logger * makeSimpleLogger(bool printBuildLogs = true);
185
186Logger * makeJSONLogger(Logger & prevLogger);
187
191std::optional<nlohmann::json> parseJSONMessage(const std::string & msg, std::string_view source);
192
196bool handleJSONLogMessage(nlohmann::json & json,
197 const Activity & act, std::map<ActivityId, Activity> & activities,
198 std::string_view source,
199 bool trusted);
200
204bool handleJSONLogMessage(const std::string & msg,
205 const Activity & act, std::map<ActivityId, Activity> & activities,
206 std::string_view source,
207 bool trusted);
208
212extern Verbosity verbosity;
213
220#define logErrorInfo(level, errorInfo...) \
221 do { \
222 if ((level) <= nix::verbosity) { \
223 logger->logEI((level), errorInfo); \
224 } \
225 } while (0)
226
227#define logError(errorInfo...) logErrorInfo(lvlError, errorInfo)
228#define logWarning(errorInfo...) logErrorInfo(lvlWarn, errorInfo)
229
235#define printMsgUsing(loggerParam, level, args...) \
236 do { \
237 auto __lvl = level; \
238 if (__lvl <= nix::verbosity) { \
239 loggerParam->log(__lvl, fmt(args)); \
240 } \
241 } while (0)
242#define printMsg(level, args...) printMsgUsing(logger, level, args)
243
244#define printError(args...) printMsg(lvlError, args)
245#define notice(args...) printMsg(lvlNotice, args)
246#define printInfo(args...) printMsg(lvlInfo, args)
247#define printTalkative(args...) printMsg(lvlTalkative, args)
248#define debug(args...) printMsg(lvlDebug, args)
249#define vomit(args...) printMsg(lvlVomit, args)
250
254template<typename... Args>
255inline void warn(const std::string & fs, const Args & ... args)
256{
257 boost::format f(fs);
258 formatHelper(f, args...);
259 logger->warn(f.str());
260}
261
262#define warnOnce(haveWarned, args...) \
263 if (!haveWarned) { \
264 haveWarned = true; \
265 warn(args); \
266 }
267
268void writeToStderr(std::string_view s);
269
270}
Definition args.hh:28
Definition logging.hh:55
Definition config.hh:320
This file defines two main structs/classes used in nix error handling.
void formatHelper(F &f)
Definition fmt.hh:24
std::string fmt(const std::string &s)
Definition fmt.hh:67
const Activity & act
Definition lexer.l:2371
std::optional< CanonPath > parent() const
boost::format f(fs)
return s
Definition lexer.l:459
ValueType type
Definition lexer.l:7098
std::vector< Expr * > args
Definition lexer.l:6126
void warn(const std::string &fs, const Args &... args)
Definition logging.hh:255
Definition logging.hh:137
Definition logging.hh:43
Definition logging.hh:61
Definition logging.hh:128