14#include <nlohmann/json_fwd.hpp>
23 actFileTransfer = 101,
28 actOptimiseStore = 106,
31 actQueryPathInfo = 109,
32 actPostBuildHook = 110,
33 actBuildWaiting = 111,
44 actFetchToStore = 113,
45 actLast = actFetchToStore
50 resBuildLogLine = 101,
51 resUntrustedPath = 102,
52 resCorruptedPath = 103,
56 resPostBuildLogLine = 107,
60 resFetchToStore = 109,
61 resLast = resFetchToStore,
64typedef uint64_t ActivityId;
68 void anchor()
override;
76 Whether Nix should print out a stack trace in case of Nix
77 expression evaluation errors.
85 A file or unix socket to which JSON records of Nix's log output are
86 written, in the same format as `--log-format internal-json`
87 (without the `@nix ` prefixes on each line).
88 Concurrent writes to the same file by multiple Nix processes are not supported and
89 may result in interleaved or corrupted log records.
97 friend struct Activity;
103 struct Field :
public std::variant<uint64_t, std::string>
105 Field(
const std::string & s)
110 Field(std::string && s)
111 : variant(std::move(s))
115 Field(
const char * s)
128 virtual void stop() {};
140 std::optional<Suspension> suspendIf(
bool cond);
142 virtual void pause() {};
143 virtual void resume() {};
146 virtual bool isVerbose()
156 virtual void log(Verbosity lvl, std::string_view s)
noexcept = 0;
158 void log(std::string_view s)
noexcept
163 virtual void logEI(
const ErrorInfo & ei)
noexcept = 0;
165 void logEI(Verbosity lvl, ErrorInfo ei)
noexcept
171 virtual void warn(
const std::string & msg)
noexcept;
173 virtual void startActivity(
177 const std::string & s,
178 std::span<const Field> fields,
179 ActivityId parent)
noexcept
183 virtual void stopActivity(ActivityId act)
noexcept {};
185 virtual void result(ActivityId act, ResultType type, std::span<const Field> fields)
noexcept {}
187 virtual void result(ActivityId act, ResultType type,
const nlohmann::json & json)
noexcept {}
189 virtual void writeToStdout(std::string_view s);
191 template<
typename... Args>
192 inline void cout(
const Args &... args)
194 writeToStdout(
fmt(args...));
203 template<
typename... Args>
204 inline void coutStyled(
const Args &... args)
209 virtual std::optional<char> ask(std::string_view s)
214 virtual void setPrintBuildLogs(
bool printBuildLogs) {}
217ActivityId getCurActivity();
218void setCurActivity(
const ActivityId activityId);
230 const std::string & s =
"",
231 std::span<const Logger::Field> fields = {},
232 ActivityId parent = getCurActivity());
237 std::span<const Logger::Field> fields = {},
238 ActivityId parent = getCurActivity())
239 : Activity(logger, lvlError, type,
"", fields, parent)
243 Activity(
const Activity & act) =
delete;
247 void progress(uint64_t done = 0, uint64_t expected = 0, uint64_t running = 0, uint64_t failed = 0)
const
249 result(resProgress, done, expected, running, failed);
252 void setExpected(ActivityType type2, uint64_t expected)
const
254 result(resSetExpected, type2, expected);
257 template<
typename...
Args>
258 requires(!(std::is_constructible_v<std::span<const Logger::Field>, std::remove_cvref_t<Args>> || ...))
259 void result(ResultType type,
Args &&... args)
const
261 std::array<
Logger::Field,
sizeof...(args)> fields = {std::forward<Args>(args)...};
262 result(type, fields);
265 void result(ResultType type, std::span<const Logger::Field> fields)
const
267 logger.result(
id, type, fields);
275 const ActivityId prevAct;
277 PushActivity(ActivityId act)
278 : prevAct(getCurActivity())
285 setCurActivity(prevAct);
291std::unique_ptr<Logger> makeSimpleLogger(
bool printBuildLogs =
true);
298std::unique_ptr<Logger>
299makeTeeLogger(std::unique_ptr<Logger> mainLogger, std::vector<std::unique_ptr<Logger>> && extraLoggers);
301std::unique_ptr<Logger> makeJSONLogger(
Descriptor fd,
bool includeNixPrefix =
true);
303std::unique_ptr<Logger> makeJSONLogger(
const std::filesystem::path & path,
bool includeNixPrefix =
true);
305void applyJSONLogger();
310std::optional<nlohmann::json>
parseJSONMessage(std::string_view msg, std::string_view source);
316 const nlohmann::json & json,
318 std::map<ActivityId, Activity> & activities,
319 std::string_view source,
326 std::string_view msg,
328 std::map<ActivityId, Activity> & activities,
329 std::string_view source,
333 const std::string & msg,
335 std::map<ActivityId, Activity> & activities,
336 std::string_view source,
345extern Verbosity verbosity;
353#define logErrorInfo(level, errorInfo...) \
355 if ((level) <= nix::verbosity) { \
356 logger->logEI((level), errorInfo); \
360#define logError(errorInfo...) logErrorInfo(lvlError, errorInfo)
361#define logWarning(errorInfo...) logErrorInfo(lvlWarn, errorInfo)
368#define printMsgUsing(loggerParam, level, args...) \
370 auto __lvl = level; \
371 if (__lvl <= nix::verbosity) { \
372 loggerParam->log(__lvl, fmt(args)); \
375#define printMsg(level, args...) printMsgUsing(logger, level, args)
377#define printError(args...) printMsg(lvlError, args)
378#define notice(args...) printMsg(lvlNotice, args)
379#define printInfo(args...) printMsg(lvlInfo, args)
380#define printTalkative(args...) printMsg(lvlTalkative, args)
381#define debug(args...) printMsg(lvlDebug, args)
382#define vomit(args...) printMsg(lvlVomit, args)
387template<
typename... Args>
388inline void warn(
const std::string & fs,
const Args &... args)
391 formatHelper(f, args...);
395#define warnOnce(haveWarned, args...) \
401void writeToStderr(std::string_view s)
noexcept;
void writeStyledToStdout(std::string_view s)
Definition logging.cc:61
Definition configuration.hh:476
This file defines two main structs/classes used in nix error handling.
File descriptor operations for almost arbitrary file descriptors.
int Descriptor
Definition file-descriptor.hh:36
std::string fmt(const std::string &s)
Definition fmt.hh:67
std::optional< nlohmann::json > parseJSONMessage(std::string_view msg, std::string_view source)
Definition logging.cc:440
void warn(const std::string &fs, const Args &... args)
Definition logging.hh:388
std::unique_ptr< Logger > makeTeeLogger(std::unique_ptr< Logger > mainLogger, std::vector< std::unique_ptr< Logger > > &&extraLoggers)
Definition tee-logger.cc:103
Logger * logger
Definition logging.cc:45
bool handleJSONLogMessage(const nlohmann::json &json, const Activity &act, std::map< ActivityId, Activity > &activities, std::string_view source, bool trusted)
Definition logging.cc:452
Definition logging.hh:221
Definition logging.hh:104
Definition logging.hh:134