1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-17 07:52:43 +01:00

Apply clang-format universally.

* It is tough to contribute to a project that doesn't use a formatter,
* It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files
* Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose,

Let's rip the bandaid off?

Note that PRs currently in flight should be able to be merged relatively easily by applying `clang-format` to their tip prior to merge.
This commit is contained in:
Graham Christensen 2025-07-18 12:47:27 -04:00
parent 41bf87ec70
commit e4f62e4608
587 changed files with 23258 additions and 23135 deletions

View file

@ -26,6 +26,7 @@ ActivityId getCurActivity()
{
return curActivity;
}
void setCurActivity(const ActivityId activityId)
{
curActivity = activityId;
@ -48,7 +49,7 @@ void Logger::writeToStdout(std::string_view s)
Logger::Suspension Logger::suspend()
{
pause();
return Suspension { ._finalize = {[this](){this->resume();}} };
return Suspension{._finalize = {[this]() { this->resume(); }}};
}
std::optional<Logger::Suspension> Logger::suspendIf(bool cond)
@ -72,25 +73,42 @@ public:
tty = isTTY();
}
bool isVerbose() override {
bool isVerbose() override
{
return printBuildLogs;
}
void log(Verbosity lvl, std::string_view s) override
{
if (lvl > verbosity) return;
if (lvl > verbosity)
return;
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: c = '7'; break;
default: c = '7'; break; // should not happen, and missing enum case is reported by -Werror=switch-enum
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:
c = '7';
break;
default:
c = '7';
break; // should not happen, and missing enum case is reported by -Werror=switch-enum
}
prefix = std::string("<") + c + ">";
}
@ -106,9 +124,13 @@ public:
log(ei.level, toView(oss));
}
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
const std::string & s, const Fields & fields, ActivityId parent)
override
void startActivity(
ActivityId act,
Verbosity lvl,
ActivityType type,
const std::string & s,
const Fields & fields,
ActivityId parent) override
{
if (lvl <= verbosity && !s.empty())
log(lvl, s + "...");
@ -119,8 +141,7 @@ public:
if (type == resBuildLogLine && printBuildLogs) {
auto lastLine = fields[0].s;
printError(lastLine);
}
else if (type == resPostBuildLogLine && printBuildLogs) {
} else if (type == resPostBuildLogLine && printBuildLogs) {
auto lastLine = fields[0].s;
printError("post-build-hook: " + lastLine);
}
@ -132,9 +153,7 @@ Verbosity verbosity = lvlInfo;
void writeToStderr(std::string_view s)
{
try {
writeFull(
getStandardError(),
s, false);
writeFull(getStandardError(), s, false);
} catch (SystemError & e) {
/* Ignore failing writes to stderr. We need to ignore write
errors to ensure that cleanup code that logs to stderr runs
@ -159,9 +178,15 @@ static uint64_t getPid()
#endif
}
Activity::Activity(Logger & logger, Verbosity lvl, ActivityType type,
const std::string & s, const Logger::Fields & fields, ActivityId parent)
: logger(logger), id(nextId++ + (((uint64_t) getPid()) << 32))
Activity::Activity(
Logger & logger,
Verbosity lvl,
ActivityType type,
const std::string & s,
const Logger::Fields & fields,
ActivityId parent)
: logger(logger)
, id(nextId++ + (((uint64_t) getPid()) << 32))
{
logger.startActivity(id, lvl, type, s, fields, parent);
}
@ -181,22 +206,26 @@ void to_json(nlohmann::json & json, std::shared_ptr<const Pos> pos)
}
}
struct JSONLogger : Logger {
struct JSONLogger : Logger
{
Descriptor fd;
bool includeNixPrefix;
JSONLogger(Descriptor fd, bool includeNixPrefix)
: fd(fd)
, includeNixPrefix(includeNixPrefix)
{ }
{
}
bool isVerbose() override {
bool isVerbose() override
{
return true;
}
void addFields(nlohmann::json & json, const Fields & fields)
{
if (fields.empty()) return;
if (fields.empty())
return;
auto & arr = json["fields"] = nlohmann::json::array();
for (auto & f : fields)
if (f.type == Logger::Field::tInt)
@ -217,8 +246,7 @@ struct JSONLogger : Logger {
void write(const nlohmann::json & json)
{
auto line =
(includeNixPrefix ? "@nix " : "") +
json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
(includeNixPrefix ? "@nix " : "") + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
/* Acquire a lock to prevent log messages from clobbering each
other. */
@ -272,8 +300,13 @@ struct JSONLogger : Logger {
write(json);
}
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
const std::string & s, const Fields & fields, ActivityId parent) override
void startActivity(
ActivityId act,
Verbosity lvl,
ActivityType type,
const std::string & s,
const Fields & fields,
ActivityId parent) override
{
nlohmann::json json;
json["action"] = "start";
@ -312,19 +345,20 @@ std::unique_ptr<Logger> makeJSONLogger(Descriptor fd, bool includeNixPrefix)
std::unique_ptr<Logger> makeJSONLogger(const std::filesystem::path & path, bool includeNixPrefix)
{
struct JSONFileLogger : JSONLogger {
struct JSONFileLogger : JSONLogger
{
AutoCloseFD fd;
JSONFileLogger(AutoCloseFD && fd, bool includeNixPrefix)
: JSONLogger(fd.get(), includeNixPrefix)
, fd(std::move(fd))
{ }
{
}
};
AutoCloseFD fd =
std::filesystem::is_socket(path)
? connect(path)
: toDescriptor(open(path.string().c_str(), O_CREAT | O_APPEND | O_WRONLY, 0644));
AutoCloseFD fd = std::filesystem::is_socket(path)
? connect(path)
: toDescriptor(open(path.string().c_str(), O_CREAT | O_APPEND | O_WRONLY, 0644));
if (!fd)
throw SysError("opening log file %1%", path);
@ -346,7 +380,6 @@ void applyJSONLogger()
} catch (...) {
ignoreExceptionExceptInterrupt();
}
}
}
@ -358,27 +391,30 @@ static Logger::Fields getFields(nlohmann::json & json)
fields.emplace_back(Logger::Field(f.get<uint64_t>()));
else if (f.type() == nlohmann::json::value_t::string)
fields.emplace_back(Logger::Field(f.get<std::string>()));
else throw Error("unsupported JSON type %d", (int) f.type());
else
throw Error("unsupported JSON type %d", (int) f.type());
}
return fields;
}
std::optional<nlohmann::json> parseJSONMessage(const std::string & msg, std::string_view source)
{
if (!hasPrefix(msg, "@nix ")) return std::nullopt;
if (!hasPrefix(msg, "@nix "))
return std::nullopt;
try {
return nlohmann::json::parse(std::string(msg, 5));
} catch (std::exception & e) {
printError("bad JSON log message from %s: %s",
Uncolored(source),
e.what());
printError("bad JSON log message from %s: %s", Uncolored(source), e.what());
}
return std::nullopt;
}
bool handleJSONLogMessage(nlohmann::json & json,
const Activity & act, std::map<ActivityId, Activity> & activities,
std::string_view source, bool trusted)
bool handleJSONLogMessage(
nlohmann::json & json,
const Activity & act,
std::map<ActivityId, Activity> & activities,
std::string_view source,
bool trusted)
{
try {
std::string action = json["action"];
@ -386,10 +422,11 @@ bool handleJSONLogMessage(nlohmann::json & json,
if (action == "start") {
auto type = (ActivityType) json["type"];
if (trusted || type == actFileTransfer)
activities.emplace(std::piecewise_construct,
activities.emplace(
std::piecewise_construct,
std::forward_as_tuple(json["id"]),
std::forward_as_tuple(*logger, (Verbosity) json["level"], type,
json["text"], getFields(json["fields"]), act.id));
std::forward_as_tuple(
*logger, (Verbosity) json["level"], type, json["text"], getFields(json["fields"]), act.id));
}
else if (action == "stop")
@ -412,21 +449,22 @@ bool handleJSONLogMessage(nlohmann::json & json,
}
return true;
} catch (const nlohmann::json::exception &e) {
warn(
"Unable to handle a JSON message from %s: %s",
Uncolored(source),
e.what()
);
} catch (const nlohmann::json::exception & e) {
warn("Unable to handle a JSON message from %s: %s", Uncolored(source), e.what());
return false;
}
}
bool handleJSONLogMessage(const std::string & msg,
const Activity & act, std::map<ActivityId, Activity> & activities, std::string_view source, bool trusted)
bool handleJSONLogMessage(
const std::string & msg,
const Activity & act,
std::map<ActivityId, Activity> & activities,
std::string_view source,
bool trusted)
{
auto json = parseJSONMessage(msg, source);
if (!json) return false;
if (!json)
return false;
return handleJSONLogMessage(*json, act, activities, source, trusted);
}
@ -440,4 +478,4 @@ Activity::~Activity()
}
}
}
} // namespace nix