diff --git a/include/journal.hpp b/include/journal.hpp index d169a66..ca78a62 100644 --- a/include/journal.hpp +++ b/include/journal.hpp @@ -70,10 +70,10 @@ auto init_std_out_err(const LogVerbosity &min = LogVerbosity::Info) -> void; auto init() -> void; auto add_file(std::string_view file_path, std::ios_base::openmode = std::ios_base::ate, - const LogVerbosity &min = LogVerbosity::Info, + const LogVerbosity &min = LogVerbosity::Trace, const LogVerbosity &max = LogVerbosity::Error) -> void; auto add_sink(std::shared_ptr stream, - const LogVerbosity &min = LogVerbosity::Info, + const LogVerbosity &min = LogVerbosity::Trace, const LogVerbosity &max = LogVerbosity::Error) -> void; #ifdef JOURNAL_ALL diff --git a/src/journal.cc b/src/journal.cc index 793206f..aded530 100644 --- a/src/journal.cc +++ b/src/journal.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -176,10 +177,16 @@ struct Verbosity { }; namespace sinks { + +using namespace std::chrono_literals; + struct BaseSink { Verbosity min_verbosity; Verbosity max_verbosity; + std::optional last_flushed; + static constexpr auto TIME_BETWEEN_FLUSH = 2s; + BaseSink() : min_verbosity(LogVerbosity::Trace), max_verbosity(LogVerbosity::Error) { } @@ -199,8 +206,18 @@ struct BaseSink { return std::exchange(min_verbosity, new_min); } - virtual auto flush() -> void; - virtual auto operator<<(std::string_view str) -> void; + auto should_flush() -> bool { + const auto now = std::chrono::system_clock::now(); + if (!last_flushed || (now - last_flushed.value()) >= TIME_BETWEEN_FLUSH) { + last_flushed = now; + return true; + } else { + return false; + } + } + + virtual auto flush() -> void {} + virtual auto operator<<(std::string_view str) -> void {} }; struct StdOutSink : public BaseSink { @@ -235,48 +252,24 @@ struct FileSink : public OStreamSink { std::ios::ios_base::openmode open_mode = std::ios::ios_base::app, const Verbosity &min = LogVerbosity::Trace, const Verbosity &max = LogVerbosity::Error) - : OStreamSink(std::make_shared( - std::ofstream(file_path, std::ios::ios_base::out | open_mode))) {} + : OStreamSink(std::make_shared(file_path, open_mode), min, + max) {} }; } // namespace sinks -struct Sink { - std::shared_ptr stream; - Verbosity min_verbosity; - Verbosity max_verbosity; - - Sink(std::shared_ptr &&stream) - : stream(std::move(stream)), - min_verbosity(journal::detail::LogVerbosity::Trace), - max_verbosity(journal::detail::LogVerbosity::Error) {} - Sink(std::shared_ptr&& stream, const Verbosity &min, - const Verbosity &max) - : stream(std::move(stream)),min_verbosity(min), max_verbosity(max) {} - Sink(std::shared_ptr &&stream, const Verbosity &min) - : stream(std::move(stream)), min_verbosity(min), - max_verbosity(journal::detail::Error) {} - - ~Sink() { flush(); } - - auto flush() -> void { stream->flush(); } - auto operator<<(std::string_view msg) { (*stream) << msg; } -}; - class Journal { std::vector> sinks; - std::chrono::system_clock::time_point last_flushed; public: Journal() {} auto log(const Verbosity &verbosity, source_location source, std::string_view msg) { const auto now = std::chrono::system_clock::now(); - const auto do_flush = ((now - last_flushed).count() <= 5); for (auto &&sink : sinks) { if (sink->should_sink_verbosity(verbosity)) { (*sink) << format(msg, verbosity, source); } - if (do_flush) { + if (sink->should_flush()) { sink->flush(); } } diff --git a/test.log b/test.log index 62e4165..5f84d18 100644 --- a/test.log +++ b/test.log @@ -1,2 +1,3 @@ -[2022-06-20 00:49:18 UTC] [INFO ] [main.cc::8] some info! -[2022-06-20 00:49:18 UTC] [ERROR] [main.cc::9] something went wrong! +[2022-06-20 19:45:53 UTC] [INFO ] [main.cc::8] some info! +[2022-06-20 19:45:53 UTC] [ERROR] [main.cc::9] something went wrong! +[2022-06-20 19:45:59 UTC] [TRACE] [main.cc::11] just some very verbose stuff here