From ed9a78fccd1e818524ac6dee1c1e67c76e5151a7 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sun, 24 Aug 2025 20:28:43 +0200 Subject: [PATCH] treewide: colorize logs by default printTaggedWarning already colorized its messages. we can do the same for most other log messages. Change-Id: Idcd31bbf4f8d0d703395b0d2b7b9bc33264d969f --- lix/legacy/build-remote.cc | 5 ++--- lix/legacy/nix-collect-garbage.cc | 2 +- lix/legacy/nix-env.cc | 8 ++++++-- lix/libcmd/repl.cc | 12 ++++++------ lix/libexpr/eval.cc | 2 +- lix/libexpr/function-trace.cc | 4 ++-- lix/libexpr/primops.cc | 4 ++-- lix/libfetchers/tarball.cc | 2 +- lix/libmain/shared.cc | 8 ++++---- lix/libstore/build/goal.cc | 2 +- lix/libstore/build/local-derivation-goal.cc | 4 ++-- lix/libstore/build/substitution-goal.cc | 4 ++-- lix/libstore/builtins/fetchurl.cc | 2 +- lix/libstore/filetransfer.cc | 12 ++++++++---- lix/libstore/gc.cc | 2 +- lix/libstore/pathlocks.cc | 6 +++--- lix/libstore/remote-store.cc | 2 +- lix/libutil/current-process.cc | 16 ++++++---------- lix/libutil/logging.cc | 4 ++-- lix/libutil/logging.hh | 4 ++-- lix/libutil/namespaces.cc | 2 +- lix/nix/daemon.cc | 2 +- lix/nix/doctor.cc | 6 +++--- lix/nix/flake.cc | 4 ++-- subprojects/nix-eval-jobs/src/nix-eval-jobs.cc | 2 +- 25 files changed, 62 insertions(+), 59 deletions(-) diff --git a/lix/legacy/build-remote.cc b/lix/legacy/build-remote.cc index 38789c3f7..bab2670cb 100644 --- a/lix/legacy/build-remote.cc +++ b/lix/legacy/build-remote.cc @@ -158,7 +158,8 @@ static void printSelectionFailureMessage( .str(); } - auto error = HintFmt( + printMsg( + level, "Failed to find a machine for remote build!\n" "derivation: %s\n" "required (system, features): (%s, [%s])\n" @@ -170,8 +171,6 @@ static void printSelectionFailureMessage( machines.size(), Uncolored(machinesFormatted) ); - - printMsg(level, "%1%", error.str()); } namespace { diff --git a/lix/legacy/nix-collect-garbage.cc b/lix/legacy/nix-collect-garbage.cc index a3b4dd835..863b90c74 100644 --- a/lix/legacy/nix-collect-garbage.cc +++ b/lix/legacy/nix-collect-garbage.cc @@ -103,7 +103,7 @@ static int main_nix_collect_garbage(AsyncIoRoot & aio, std::string programName, if (dryRun) { // Only print results for dry run; when !dryRun, paths will be printed as they're deleted. for (auto & i : results.paths) { - printInfo("%s", i); + printInfo("%s", Uncolored(i)); } } diff --git a/lix/legacy/nix-env.cc b/lix/legacy/nix-env.cc index 531812b75..9ce7fbc66 100644 --- a/lix/legacy/nix-env.cc +++ b/lix/legacy/nix-env.cc @@ -686,8 +686,12 @@ static void upgradeDerivations(Globals & globals, { const char * action = compareVersions(drvName.version, bestVersion) <= 0 ? "upgrading" : "downgrading"; - printInfo("%1% '%2%' to '%3%'", - action, i.queryName(*state), bestElem->queryName(*state)); + printInfo( + "%1% '%2%' to '%3%'", + Uncolored(action), + i.queryName(*state), + bestElem->queryName(*state) + ); newElems.push_back(*bestElem); } else newElems.push_back(i); diff --git a/lix/libcmd/repl.cc b/lix/libcmd/repl.cc index 502ccc5ec..f17f0b28c 100644 --- a/lix/libcmd/repl.cc +++ b/lix/libcmd/repl.cc @@ -290,7 +290,7 @@ ReplExitStatus NixRepl::mainLoop() if (evaluator.debug && evaluator.debug->inDebugger) { debuggerNotice = " debugger"; } - notice("Lix %1%%2%\nType :? for help.", nixVersion, debuggerNotice); + notice("Lix %1%%2%\nType :? for help.", Uncolored(nixVersion), debuggerNotice); } isFirstRepl = false; @@ -337,14 +337,14 @@ ReplExitStatus NixRepl::mainLoop() // input without clearing the input so far. continue; } else { - printMsg(lvlError, "%1%", e.msg()); + printMsg(lvlError, "%1%", Uncolored(e.msg())); } } catch (EvalError & e) { - printMsg(lvlError, "%1%", e.msg()); + printMsg(lvlError, "%1%", Uncolored(e.msg())); } catch (Error & e) { - printMsg(lvlError, "%1%", e.msg()); + printMsg(lvlError, "%1%", Uncolored(e.msg())); } catch (Interrupted & e) { - printMsg(lvlError, "%1%", e.msg()); + printMsg(lvlError, "%1%", Uncolored(e.msg())); } // We handled the current input fully, so we should clear it @@ -960,7 +960,7 @@ void NixRepl::loadReplOverlays() return; } - notice("Loading '%1%'...", Magenta("repl-overlays")); + notice("Loading '%1%'...", "repl-overlays"); auto replInitFilesFunction = getReplOverlaysEvalFunction(); Value &newAttrs(*evaluator.mem.allocValue()); diff --git a/lix/libexpr/eval.cc b/lix/libexpr/eval.cc index d372a373f..3a2140cf2 100644 --- a/lix/libexpr/eval.cc +++ b/lix/libexpr/eval.cc @@ -749,7 +749,7 @@ void DebugState::onEvalError( if (error) { - printError("%s\n", error->what()); + printError("%s\n", Uncolored(error->what())); if (trylevel > 0 && error->info().level != lvlInfo) { if (evalSettings.ignoreExceptionsDuringTry) { diff --git a/lix/libexpr/function-trace.cc b/lix/libexpr/function-trace.cc index 53c349346..fecda6b26 100644 --- a/lix/libexpr/function-trace.cc +++ b/lix/libexpr/function-trace.cc @@ -6,13 +6,13 @@ namespace nix { FunctionCallTrace::FunctionCallTrace(const Pos & pos) : pos(pos) { auto duration = std::chrono::high_resolution_clock::now().time_since_epoch(); auto ns = std::chrono::duration_cast(duration); - printMsg(lvlInfo, "function-trace entered %1% at %2%", pos, ns.count()); + printMsg(lvlInfo, "function-trace entered %1% at %2%", Uncolored(pos), Uncolored(ns.count())); } FunctionCallTrace::~FunctionCallTrace() { auto duration = std::chrono::high_resolution_clock::now().time_since_epoch(); auto ns = std::chrono::duration_cast(duration); - printMsg(lvlInfo, "function-trace exited %1% at %2%", pos, ns.count()); + printMsg(lvlInfo, "function-trace exited %1% at %2%", Uncolored(pos), Uncolored(ns.count())); } } diff --git a/lix/libexpr/primops.cc b/lix/libexpr/primops.cc index 6d66877e5..6782c4664 100644 --- a/lix/libexpr/primops.cc +++ b/lix/libexpr/primops.cc @@ -695,9 +695,9 @@ static void prim_trace(EvalState & state, Value * * args, Value & v) { state.forceValue(*args[0], noPos); if (args[0]->type() == nString) - printError("trace: %1%", args[0]->str()); + printError("trace: %1%", Uncolored(args[0]->str())); else - printError("trace: %1%", ValuePrinter(state, *args[0])); + printError("trace: %1%", Uncolored(ValuePrinter(state, *args[0]))); if (auto last = evalSettings.builtinsTraceDebugger && state.ctx.debug ? state.ctx.debug->traces().next() : std::nullopt) diff --git a/lix/libfetchers/tarball.cc b/lix/libfetchers/tarball.cc index ece4116e8..e602716f9 100644 --- a/lix/libfetchers/tarball.cc +++ b/lix/libfetchers/tarball.cc @@ -90,7 +90,7 @@ try { data = TRY_AWAIT(content->drain()); } catch (FileTransferError & e) { if (cached) { - printTaggedWarning("%s; using cached version", e.msg()); + printTaggedWarning("%s; using cached version", Uncolored(e.msg())); co_return useCached(); } else throw; diff --git a/lix/libmain/shared.cc b/lix/libmain/shared.cc index b1ba337d3..86996059b 100644 --- a/lix/libmain/shared.cc +++ b/lix/libmain/shared.cc @@ -74,7 +74,7 @@ try { auto sorted = TRY_AWAIT(store->topoSortPaths(willBuild)); reverse(sorted.begin(), sorted.end()); for (auto & i : sorted) - printMsg(lvl, " %s", store->printStorePath(i)); + printMsg(lvl, " %s", Uncolored(store->printStorePath(i))); } if (!willSubstitute.empty()) { @@ -101,14 +101,14 @@ try { return lhs->name() < rhs->name(); }); for (auto p : willSubstituteSorted) - printMsg(lvl, " %s", store->printStorePath(*p)); + printMsg(lvl, " %s", Uncolored(store->printStorePath(*p))); } if (!unknown.empty()) { printMsg(lvl, "don't know how to build these paths%s:", (settings.readOnlyMode ? " (may be caused by read-only store access)" : "")); for (auto & i : unknown) - printMsg(lvl, " %s", store->printStorePath(i)); + printMsg(lvl, " %s", Uncolored(store->printStorePath(i))); } co_return result::success(); @@ -331,7 +331,7 @@ int handleExceptions(const std::string & programName, std::function fun) if (onlyForSubcommands) printError("'%1%' is reserved for external subcommands, is your subcommand available in the PATH?", programName); else - printError("Try '%1% --help' for more information.", programName); + printError("Try '%1%' for more information.", programName + " --help"); return 1; } catch (BaseError & e) { logError(e.info()); diff --git a/lix/libstore/build/goal.cc b/lix/libstore/build/goal.cc index d5fdf4379..878d9d687 100644 --- a/lix/libstore/build/goal.cc +++ b/lix/libstore/build/goal.cc @@ -9,7 +9,7 @@ namespace nix { void Goal::trace(std::string_view s) { - debug("%1%: %2%", name, s); + debug("%1%: %2%", Uncolored(name), Uncolored(s)); } kj::Promise Goal::waitForAWhile() diff --git a/lix/libstore/build/local-derivation-goal.cc b/lix/libstore/build/local-derivation-goal.cc index 89cff969a..a9ac614d5 100644 --- a/lix/libstore/build/local-derivation-goal.cc +++ b/lix/libstore/build/local-derivation-goal.cc @@ -98,7 +98,7 @@ void handleDiffHook( statusToString(diffRes.first)); if (diffRes.second != "") { - printError("%1%", chomp(diffRes.second)); + printError("%1%", Uncolored(chomp(diffRes.second))); } } catch (Error & error) { ErrorInfo ei = error.info(); @@ -845,7 +845,7 @@ try { ex.addTrace({}, "while setting up the build environment"); throw ex; } - debug("sandbox setup: %1%", msg); + debug("sandbox setup: %1%", Uncolored(msg)); msgs.push_back(std::move(msg)); } diff --git a/lix/libstore/build/substitution-goal.cc b/lix/libstore/build/substitution-goal.cc index 8ff25c2d0..8b177d795 100644 --- a/lix/libstore/build/substitution-goal.cc +++ b/lix/libstore/build/substitution-goal.cc @@ -41,7 +41,7 @@ Goal::WorkResult PathSubstitutionGoal::done( { BuildResult buildResult{.status = status}; if (errorMsg) { - debug("%1%", *errorMsg); + debug("%1%", Uncolored(*errorMsg)); buildResult.errorMsg = *errorMsg; } return WorkResult{result, std::move(buildResult)}; @@ -263,7 +263,7 @@ try { thr.get(); break; } catch (std::exception & e) { // NOLINT(lix-foreign-exceptions) - printError("%1%", e.what()); + printError("%1%", Uncolored(e.what())); /* Cause the parent build to fail unless --fallback is given, or the substitute has disappeared. The latter case behaves diff --git a/lix/libstore/builtins/fetchurl.cc b/lix/libstore/builtins/fetchurl.cc index 029f5b636..401073d1a 100644 --- a/lix/libstore/builtins/fetchurl.cc +++ b/lix/libstore/builtins/fetchurl.cc @@ -75,7 +75,7 @@ void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData, ); return; } catch (Error & e) { - debug("%1%", e.what()); + debug("%1%", Uncolored(e.what())); } } } diff --git a/lix/libstore/filetransfer.cc b/lix/libstore/filetransfer.cc index 21db2d2f7..788ff32d8 100644 --- a/lix/libstore/filetransfer.cc +++ b/lix/libstore/filetransfer.cc @@ -1039,15 +1039,19 @@ struct curlFileTransfer : public FileTransfer if (totalReceived) { printTaggedWarning( "%s; retrying from offset %d in %d ms (attempt %d/%d)", - context, + Uncolored(context), totalReceived, waitTime, - attempt, - tries + Uncolored(attempt), + Uncolored(tries) ); } else { printTaggedWarning( - "%s; retrying in %d ms (attempt %d/%d)", context, waitTime, attempt, tries + "%s; retrying in %d ms (attempt %d/%d)", + Uncolored(context), + waitTime, + Uncolored(attempt), + Uncolored(tries) ); } diff --git a/lix/libstore/gc.cc b/lix/libstore/gc.cc index 2ce815dba..3c0eea944 100644 --- a/lix/libstore/gc.cc +++ b/lix/libstore/gc.cc @@ -830,7 +830,7 @@ try { if (!kept.empty()) { printTalkative("Paths not deleted because they are still referenced by GC roots:"); for (auto &path: kept) { - printTalkative("%1%", path); + printTalkative("%1%", Uncolored(path)); } } diff --git a/lix/libstore/pathlocks.cc b/lix/libstore/pathlocks.cc index bb790ecba..befe0c26e 100644 --- a/lix/libstore/pathlocks.cc +++ b/lix/libstore/pathlocks.cc @@ -146,7 +146,7 @@ PathLock::lockImpl(const Path & path, std::string_view waitMsg, bool wait, Never if (!tryLockFile(fd.get(), ltWrite)) { if (wait) { if (waitMsg != "") { - printError("%1%", waitMsg); + printError("%1%", Uncolored(waitMsg)); } lockFile(fd.get(), ltWrite); } else { @@ -258,7 +258,7 @@ FdLock::FdLock(AutoCloseFD & fd, LockType lockType, DontWait) FdLock::FdLock(AutoCloseFD & fd, LockType lockType, std::string_view waitMsg, NeverAsync) { if (!tryLockFile(fd.get(), lockType)) { - printInfo("%s", waitMsg); + printInfo("%s", Uncolored(waitMsg)); lockFile(fd.get(), lockType); this->fd.reset(&fd); } @@ -268,7 +268,7 @@ kj::Promise> FdLock::lockAsync(AutoCloseFD & fd, LockType lockType, std::string_view waitMsg) try { if (!tryLockFile(fd.get(), lockType)) { - printInfo("%s", waitMsg); + printInfo("%s", Uncolored(waitMsg)); TRY_AWAIT(lockFileAsyncInner(fd.get(), lockType)); } co_return FdLock{fd}; diff --git a/lix/libstore/remote-store.cc b/lix/libstore/remote-store.cc index f4b18c729..885763f9b 100644 --- a/lix/libstore/remote-store.cc +++ b/lix/libstore/remote-store.cc @@ -792,7 +792,7 @@ try { } else if (msg == STDERR_NEXT) - printError("%1%", chomp(TRY_AWAIT(readString(from)))); + printError("%1%", Uncolored(chomp(TRY_AWAIT(readString(from))))); else if (msg == STDERR_START_ACTIVITY) { auto act = TRY_AWAIT(readNum(from)); diff --git a/lix/libutil/current-process.cc b/lix/libutil/current-process.cc index 1fab32650..0c9df5b89 100644 --- a/lix/libutil/current-process.cc +++ b/lix/libutil/current-process.cc @@ -62,16 +62,12 @@ void setStackSize(rlim_t stackSize) limit.rlim_cur = std::min(stackSize, limit.rlim_max); if (setrlimit(RLIMIT_STACK, &limit) != 0) { printError( - "%1%", - HintFmt( - "Failed to increase stack size from %1% to %2% (maximum allowed " - "stack size: %3%): %4%", - savedStackSize, - stackSize, - limit.rlim_max, - std::strerror(errno) - ) - .str() + "Failed to increase stack size from %1% to %2% (maximum allowed stack size: %3%): " + "%4%", + savedStackSize, + stackSize, + limit.rlim_max, + std::strerror(errno) ); } } diff --git a/lix/libutil/logging.cc b/lix/libutil/logging.cc index 2ce7e1903..a2248395c 100644 --- a/lix/libutil/logging.cc +++ b/lix/libutil/logging.cc @@ -95,11 +95,11 @@ public: { if (type == resBuildLogLine && printBuildLogs) { auto lastLine = fields[0].s; - printError("%1%", lastLine); + printError("%1%", Uncolored(lastLine)); } else if (type == resPostBuildLogLine) { auto lastLine = fields[0].s; - printError("post-build-hook: %1%", lastLine); + printError("post-build-hook: %1%", Uncolored(lastLine)); } } }; diff --git a/lix/libutil/logging.hh b/lix/libutil/logging.hh index 28ecd7412..b034d1b43 100644 --- a/lix/libutil/logging.hh +++ b/lix/libutil/logging.hh @@ -262,7 +262,7 @@ extern Verbosity verbosity; auto _lix_logger_print_lvl = level; \ const char * _lix_format = [](const char(&_lix_fs)[N]) { return _lix_fs; }(fs); \ if (_lix_logger_print_lvl <= ::nix::verbosity) { \ - loggerParam->log(_lix_logger_print_lvl, ::nix::fmt(_lix_format, ##args)); \ + loggerParam->log(_lix_logger_print_lvl, ::nix::HintFmt(_lix_format, ##args).str()); \ } \ } while (0) #define printMsg(level, fs, args...) printMsgUsing(::nix::logger, level, fs, ##args) @@ -276,7 +276,7 @@ extern Verbosity verbosity; #define vomit(fs, args...) printMsg(::nix::lvlVomit, fs, ##args) #define printTaggedWarning(fs, args...) \ - printWarning("%1%", ::nix::HintFmt(ANSI_WARNING "warning:" ANSI_NORMAL " " fs, ##args).str()) + printWarning(ANSI_WARNING "warning:" ANSI_NORMAL " " fs, ##args) void writeLogsToStderr(std::string_view s); diff --git a/lix/libutil/namespaces.cc b/lix/libutil/namespaces.cc index f2b61b08c..97ec9029a 100644 --- a/lix/libutil/namespaces.cc +++ b/lix/libutil/namespaces.cc @@ -52,7 +52,7 @@ void restoreMountNamespace() if (chdir(savedCwd.c_str()) == -1) throw SysError("restoring cwd"); } catch (Error & e) { - debug("%1%", e.msg()); + debug("%1%", Uncolored(e.msg())); } #endif } diff --git a/lix/nix/daemon.cc b/lix/nix/daemon.cc index 723ccd348..c3cd842b4 100644 --- a/lix/nix/daemon.cc +++ b/lix/nix/daemon.cc @@ -406,7 +406,7 @@ static void daemonInstance(AsyncIoRoot & aio, std::optional forceTr printInfo( "%1% is %2% (%3%%4%)", - peer.pidKnown ? fmt("remote pid %s", peer.pid) : "remote with unknown pid", + Uncolored(peer.pidKnown ? fmt("remote pid %s", peer.pid) : "remote with unknown pid"), peer.uidKnown ? fmt("user %s", user) : "unknown user", trusted ? "trusted" : "untrusted", forceTrustClientOpt ? " by override" : "" diff --git a/lix/nix/doctor.cc b/lix/nix/doctor.cc index 0a244045c..01dcfcac1 100644 --- a/lix/nix/doctor.cc +++ b/lix/nix/doctor.cc @@ -25,17 +25,17 @@ std::string formatProtocol(unsigned int proto) } bool checkPass(const std::string & msg) { - notice(ANSI_GREEN "[PASS]" ANSI_NORMAL " %1%", msg); + notice(ANSI_GREEN "[PASS] " ANSI_NORMAL "%1%", Uncolored(msg)); return true; } bool checkFail(const std::string & msg) { - notice(ANSI_RED "[FAIL]" ANSI_NORMAL " %1%", msg); + notice(ANSI_RED "[FAIL] " ANSI_NORMAL "%1%", Uncolored(msg)); return false; } void checkInfo(const std::string & msg) { - notice(ANSI_BLUE "[INFO]" ANSI_NORMAL " %1%", msg); + notice(ANSI_BLUE "[INFO] " ANSI_NORMAL "%1%", Uncolored(msg)); } } diff --git a/lix/nix/flake.cc b/lix/nix/flake.cc index 22c0a3049..c2f83ba97 100644 --- a/lix/nix/flake.cc +++ b/lix/nix/flake.cc @@ -956,9 +956,9 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand if (welcomeText) { notice( "\n%1%", - renderMarkdownToTerminal( + Uncolored(renderMarkdownToTerminal( welcomeText->getString(*evalState), StandardOutputStream::Stderr - ) + )) ); } diff --git a/subprojects/nix-eval-jobs/src/nix-eval-jobs.cc b/subprojects/nix-eval-jobs/src/nix-eval-jobs.cc index 0602a9067..f6a5482ef 100644 --- a/subprojects/nix-eval-jobs/src/nix-eval-jobs.cc +++ b/subprojects/nix-eval-jobs/src/nix-eval-jobs.cc @@ -77,7 +77,7 @@ struct Proc { JSON err; auto msg = e.msg(); err["error"] = nix::filterANSIEscapes(msg, true); - printError("%1%", msg); + printError("%1%", Uncolored(msg)); if (tryWriteLine(to->get(), err.dump()) < 0) { return; // main process died };