Skip to content

Commit

Permalink
remove indirection for streams in Logger.cpp #1
Browse files Browse the repository at this point in the history
Co-authored-by: fabianbs96 <[email protected]>
  • Loading branch information
MMory and fabianbs96 authored May 19, 2022
1 parent d60e191 commit e90c744
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/Utils/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<llvm::raw_fd_ostream>(
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<llvm::raw_fd_ostream>(
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;
}
Expand Down

0 comments on commit e90c744

Please sign in to comment.