From e90c744fd637f686a69f040810968c38fb6a28e4 Mon Sep 17 00:00:00 2001 From: Martin Mory Date: Thu, 19 May 2022 18:51:15 +0200 Subject: [PATCH] remove indirection for streams in Logger.cpp #1 Co-authored-by: fabianbs96 <52407375+fabianbs96@users.noreply.github.com> --- lib/Utils/Logger.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/Utils/Logger.cpp b/lib/Utils/Logger.cpp index 7bb8abfe3..82d80b177 100644 --- a/lib/Utils/Logger.cpp +++ b/lib/Utils/Logger.cpp @@ -76,25 +76,30 @@ void Logger::initializeStderrLogger( } else { LevelsToStreamVariant[Level] = Filename.str(); } - if (LogfileStreams[Filename] == nullptr) { - std::error_code EC; + + std::error_code EC; + auto [It, Inserted] = [&] { if (Append) { - LogfileStreams[Filename] = std::make_unique( - Filename, EC, + return LogfileStreams.try_emplace( + Filename, Filename, EC, llvm::sys::fs::OpenFlags::OF_Append | llvm::sys::fs::OpenFlags::OF_ChildInherit); - } else { - LogfileStreams[Filename] = std::make_unique( - Filename, EC, llvm::sys::fs::OpenFlags::OF_ChildInherit); - } - // Following - // https://stackoverflow.com/questions/41699343/how-do-i-test-that-an-stderror-code-is-not-an-error - if (EC) { - LogfileStreams[Filename] = nullptr; - llvm::errs() << "Failed to open logfile: " << Filename << '\n'; - llvm::errs() << EC.message() << '\n'; - return false; } + + return LogfileStreams.try_emplace( + Filename, Filename, EC, llvm::sys::fs::OpenFlags::OF_ChildInherit); + }(); + + if (!Inserted) { + return true; + } + + if (EC) { + LogfileStreams.erase(Filename); + llvm::errs() << "Failed to open logfile: " << Filename << '\n'; + llvm::errs() << EC.message() << '\n'; + return false; + } } return true; }