move stdout sink to seperate source files

This commit is contained in:
Lily Tsuru 2023-07-18 06:08:49 -04:00
parent 9260acce31
commit 3d9f9139ff
5 changed files with 60 additions and 50 deletions

View File

@ -7,7 +7,9 @@ project(lucore
add_library(lucore
src/Assert.cpp
src/Logger.cpp
src/StdoutSink.cpp
src/OsLibrary.cpp
src/Library.cpp

View File

@ -82,18 +82,6 @@ namespace lucore {
std::vector<Sink*> 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 <class... Args>
void LogInfo(std::string_view format, Args... args) {
Logger::The().Info(format, std::forward<Args>(args)...);

View File

@ -0,0 +1,17 @@
#include <lucore/Logger.hpp>
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();
}

View File

@ -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 <print>
// 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

View File

@ -0,0 +1,41 @@
#include <lucore/StdoutSink.hpp>
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 <print>
// 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