diff --git a/swift/logging/SwiftDiagnostics.h b/swift/logging/SwiftDiagnostics.h index c42a3ec781e..ce5dd241778 100644 --- a/swift/logging/SwiftDiagnostics.h +++ b/swift/logging/SwiftDiagnostics.h @@ -66,31 +66,6 @@ struct SwiftDiagnostic { } }; -class SwiftDiagnosticsDumper { - public: - // opens path for writing out JSON entries. Returns whether the operation was successful. - bool open(const std::filesystem::path& path) { - output.open(path); - return output.good(); - } - - void flush() { output.flush(); } - - void write(const SwiftDiagnostic& source, - const std::chrono::system_clock::time_point& timestamp, - std::string_view message) { - if (output) { - output << source.json(timestamp, message) << '\n'; - } - } - - bool good() const { return output.good(); } - explicit operator bool() const { return good(); } - - private: - std::ofstream output; -}; - constexpr SwiftDiagnostic internalError{ "internal-error", "Internal error", diff --git a/swift/logging/SwiftLogging.cpp b/swift/logging/SwiftLogging.cpp index e4db45f9872..2f01c4fa0b0 100644 --- a/swift/logging/SwiftLogging.cpp +++ b/swift/logging/SwiftLogging.cpp @@ -138,7 +138,8 @@ void Log::configure() { std::error_code ec; std::filesystem::create_directories(diagFile.parent_path(), ec); if (!ec) { - if (!diagnostics.open(diagFile)) { + diagnostics.open(diagFile); + if (!diagnostics) { problems.emplace_back("Unable to open diagnostics json file " + diagFile.string()); } } else { @@ -168,6 +169,14 @@ void Log::flushImpl() { } } +void Log::diagnoseImpl(const SwiftDiagnostic& source, + const std::chrono::system_clock::time_point& time, + std::string_view message) { + if (diagnostics) { + diagnostics << source.json(time, message) << '\n'; + } +} + Log::LoggerConfiguration Log::getLoggerConfigurationImpl(std::string_view name) { LoggerConfiguration ret{session, std::string{programName}}; ret.fullyQualifiedName += '/'; diff --git a/swift/logging/SwiftLogging.h b/swift/logging/SwiftLogging.h index 33e9d87770a..04dfbf2a8b1 100644 --- a/swift/logging/SwiftLogging.h +++ b/swift/logging/SwiftLogging.h @@ -126,7 +126,7 @@ class Log { static void diagnose(const SwiftDiagnostic& source, const std::chrono::system_clock::time_point& time, std::string_view message) { - instance().diagnostics.write(source, time, message); + instance().diagnoseImpl(source, time, message); } private: @@ -144,6 +144,9 @@ class Log { void configure(); void flushImpl(); + void diagnoseImpl(const SwiftDiagnostic& source, + const std::chrono::system_clock::time_point& time, + std::string_view message); LoggerConfiguration getLoggerConfigurationImpl(std::string_view name); @@ -183,7 +186,7 @@ class Log { FilteredOutput binary{Level::no_logs}; FilteredOutput text{Level::info, textFile, format}; FilteredOutput console{Level::warning, std::cerr, format}; - SwiftDiagnosticsDumper diagnostics{}; + std::ofstream diagnostics{}; LevelRules sourceRules; std::vector collectLevelRulesAndReturnProblems(const char* envVar); };