From 3d9f9139ff8074ad3edcb966642cb9982ef614a6 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Tue, 18 Jul 2023 06:08:49 -0400 Subject: [PATCH] move stdout sink to seperate source files --- native/projects/lucore/CMakeLists.txt | 2 + .../projects/lucore/include/lucore/Logger.hpp | 12 ------ .../lucore/include/lucore/StdoutSink.hpp | 17 ++++++++ native/projects/lucore/src/Logger.cpp | 38 ----------------- native/projects/lucore/src/StdoutSink.cpp | 41 +++++++++++++++++++ 5 files changed, 60 insertions(+), 50 deletions(-) create mode 100644 native/projects/lucore/include/lucore/StdoutSink.hpp create mode 100644 native/projects/lucore/src/StdoutSink.cpp diff --git a/native/projects/lucore/CMakeLists.txt b/native/projects/lucore/CMakeLists.txt index 3881c11..825bdee 100644 --- a/native/projects/lucore/CMakeLists.txt +++ b/native/projects/lucore/CMakeLists.txt @@ -7,7 +7,9 @@ project(lucore add_library(lucore src/Assert.cpp + src/Logger.cpp + src/StdoutSink.cpp src/OsLibrary.cpp src/Library.cpp diff --git a/native/projects/lucore/include/lucore/Logger.hpp b/native/projects/lucore/include/lucore/Logger.hpp index 29c2209..cd19400 100644 --- a/native/projects/lucore/include/lucore/Logger.hpp +++ b/native/projects/lucore/include/lucore/Logger.hpp @@ -82,18 +82,6 @@ namespace lucore { std::vector sinks; }; -#if 0 - /// A logger sink implementation that prints to standard output. - struct StdoutSink : public Logger::Sink { - static StdoutSink& The(); - - virtual void OutputMessage(const Logger::MessageData& data) override; - }; -#endif - - /// Attach the stdout logger sink to the logger. - void LoggerAttachStdout(); - template void LogInfo(std::string_view format, Args... args) { Logger::The().Info(format, std::forward(args)...); diff --git a/native/projects/lucore/include/lucore/StdoutSink.hpp b/native/projects/lucore/include/lucore/StdoutSink.hpp new file mode 100644 index 0000000..8ede41b --- /dev/null +++ b/native/projects/lucore/include/lucore/StdoutSink.hpp @@ -0,0 +1,17 @@ +#include + +namespace lucore { + + /// A logger sink implementation that prints to standard output. + /// Not used by the lcpu native module, but could be used by applications + /// using lucore later on. + struct StdoutSink : public Logger::Sink { + static StdoutSink& The(); + + virtual void OutputMessage(const Logger::MessageData& data) override; + }; + + /// Attach the stdout logger sink to the logger. + void LoggerAttachStdout(); + +} diff --git a/native/projects/lucore/src/Logger.cpp b/native/projects/lucore/src/Logger.cpp index bef86a6..26164d5 100644 --- a/native/projects/lucore/src/Logger.cpp +++ b/native/projects/lucore/src/Logger.cpp @@ -31,44 +31,6 @@ namespace lucore { for(auto sink : sinks) sink->OutputMessage(data); } -#if 0 - StdoutSink& StdoutSink::The() { - static StdoutSink sink; - return sink; - } - void StdoutSink::OutputMessage(const Logger::MessageData& data) { - // This is kinda iffy, but required until more standard libraries support the C++23 - // header. - struct FileOutIterator { - using iterator_category = std::output_iterator_tag; - using value_type = void; - using difference_type = std::ptrdiff_t; - using pointer = void; - using reference = void; - - FileOutIterator(std::FILE* file) : file(file) {} - FileOutIterator& operator*() { return *this; } - FileOutIterator& operator++() { return *this; } - FileOutIterator& operator++(int) { return *this; } - - FileOutIterator& operator=(const char& val) { - fputc(val, file); - return *this; - } - - private: - std::FILE* file; - }; - std::format_to( - FileOutIterator(data.severity < Logger::MessageSeverity::Error ? stdout : stderr), - "[Lucore/{}] [{}] {}\n", Logger::SeverityToString(data.severity), data.time, - std::vformat(data.format, data.args)); - } - - void LoggerAttachStdout() { - Logger::The().AttachSink(StdoutSink::The()); - } -#endif } // namespace lucore diff --git a/native/projects/lucore/src/StdoutSink.cpp b/native/projects/lucore/src/StdoutSink.cpp new file mode 100644 index 0000000..fbef82f --- /dev/null +++ b/native/projects/lucore/src/StdoutSink.cpp @@ -0,0 +1,41 @@ +#include + +namespace lucore { + StdoutSink& StdoutSink::The() { + static StdoutSink sink; + return sink; + } + + void StdoutSink::OutputMessage(const Logger::MessageData& data) { + // This is kinda iffy, but required until more standard libraries support the C++23 + // header. + struct FputcIterator { + using iterator_category = std::output_iterator_tag; + using value_type = void; + using difference_type = std::ptrdiff_t; + using pointer = void; + using reference = void; + + FputcIterator(std::FILE* file) : file(file) {} + FputcIterator& operator*() { return *this; } + FputcIterator& operator++() { return *this; } + FputcIterator& operator++(int) { return *this; } + + FputcIterator& operator=(const char& val) { + fputc(val, file); + return *this; + } + + private: + std::FILE* file; + }; + + auto it = FputcIterator(data.severity < Logger::MessageSeverity::Error ? stdout : stderr); + std::format_to(it, "[Lucore/{}] [{}] {}\n", Logger::SeverityToString(data.severity), + data.time, std::vformat(data.format, data.args)); + } + + void LoggerAttachStdout() { + Logger::The().AttachSink(StdoutSink::The()); + } +} // namespace lucore