From 0fd32f8b9d707d807c236156de12118b0a695d69 Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Tue, 14 May 2024 16:48:20 +0200 Subject: logging: Do not make assumptions in emit calls The Emit method of the Logger class, when called with a string as second argument, expects it to be a format string. It should be considered a programming error to pass a string variable as that argument without knowing for certain that it does not contain any format escape character ('{', '}'); instead, one should be conservative and use the blind format string "{}" as second argument and pass the unknown string variable as third argument. --- .../serve_api/serve_service/target_utils.cpp | 43 ++++++++++------------ 1 file changed, 20 insertions(+), 23 deletions(-) (limited to 'src/buildtool/serve_api/serve_service/target_utils.cpp') diff --git a/src/buildtool/serve_api/serve_service/target_utils.cpp b/src/buildtool/serve_api/serve_service/target_utils.cpp index c9268f63..7a94c9ee 100644 --- a/src/buildtool/serve_api/serve_service/target_utils.cpp +++ b/src/buildtool/serve_api/serve_service/target_utils.cpp @@ -38,13 +38,12 @@ auto IsTreeInRepo(std::string const& tree_id, auto wrapped_logger = std::make_shared( [logger, repo_path, tree_id](auto const& msg, bool fatal) { if (fatal) { - auto err = fmt::format( - "ServeTarget: While checking existence of tree {} " - "in repository {}:\n{}", - tree_id, - repo_path.string(), - msg); - logger->Emit(LogLevel::Info, err); + logger->Emit(LogLevel::Info, + "ServeTarget: While checking existence of " + "tree {} in repository {}:\n{}", + tree_id, + repo_path.string(), + msg); } }); if (auto res = repo->CheckTreeExists(tree_id, wrapped_logger)) { @@ -238,13 +237,12 @@ auto GetBlobContent(std::filesystem::path const& repo_path, auto wrapped_logger = std::make_shared( [logger, repo_path, tree_id](auto const& msg, bool fatal) { if (fatal) { - auto err = fmt::format( - "ServeTargetVariables: While checking if tree {} " - "exists in repository {}:\n{}", - tree_id, - repo_path.string(), - msg); - logger->Emit(LogLevel::Debug, err); + logger->Emit(LogLevel::Debug, + "ServeTargetVariables: While checking if " + "tree {} exists in repository {}:\n{}", + tree_id, + repo_path.string(), + msg); } }); if (repo->CheckTreeExists(tree_id, wrapped_logger) == true) { @@ -255,26 +253,25 @@ auto GetBlobContent(std::filesystem::path const& repo_path, [logger, repo_path, blob_id = entry_info->id]( auto const& msg, bool fatal) { if (fatal) { - auto err = fmt::format( + logger->Emit( + LogLevel::Debug, "ServeTargetVariables: While retrieving " "blob {} from repository {}:\n{}", blob_id, repo_path.string(), msg); - logger->Emit(LogLevel::Debug, err); } }); // get blob content return repo->TryReadBlob(entry_info->id, wrapped_logger); } // trace failure to get entry - auto err = fmt::format( - "ServeTargetVariables: Failed to retrieve entry {} in " - "tree {} from repository {}", - rel_path, - tree_id, - repo_path.string()); - logger->Emit(LogLevel::Debug, err); + logger->Emit(LogLevel::Debug, + "ServeTargetVariables: Failed to retrieve entry " + "{} in tree {} from repository {}", + rel_path, + tree_id, + repo_path.string()); return std::pair(false, std::nullopt); // could not read blob } } -- cgit v1.2.3