diff --git a/src/application.cpp b/src/application.cpp index b72335a..995262e 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -33,12 +33,12 @@ static std::mutex app_mutex; Application::Logger::Logger(std::string name) { auto app = Application::GetInstance(); - if (app->HasMonitor(name)) { - cur_monitor_ = app->GetMonitor(name); - cur_monitor_->Enter(); - } else { - throw NotFoundError("Logger", "Monitor " + name); + if (!app->HasMonitor(name)) { + app->InstallMonitor(name, "stdout", "stderr"); } + + cur_monitor_ = app->GetMonitor(name); + cur_monitor_->Enter(); } Application::Logger::~Logger() { cur_monitor_->Leave(); } diff --git a/src/monitor.cpp b/src/monitor.cpp index 20a3103..2641414 100644 --- a/src/monitor.cpp +++ b/src/monitor.cpp @@ -9,6 +9,10 @@ #include "application.hpp" #include "monitor.hpp" +struct NullDeleter { + void operator()(void const*) const {} +}; + static std::mutex section_mutex; void Monitor::Log(std::string const& msg) { @@ -47,6 +51,9 @@ void Monitor::Enter() { void Monitor::Leave() { std::unique_lock lock(section_mutex); + log_device_->flush(); + err_device_->flush(); + sections_.pop_back(); if (sections_.size() > 0) sections_.back() += 1; } @@ -57,7 +64,11 @@ bool Monitor::SetLogOutput(std::string const& fname) { if (app->HasDevice(fname)) { log_device_ = app->GetDevice(fname); } else { - log_device_ = std::make_shared(fname, std::ios::out); + if (fname == "stdout") { + log_device_ = std::shared_ptr(&std::cout, NullDeleter()); + } else { + log_device_ = std::make_shared(fname, std::ios::out); + } app->InstallDevice(fname, log_device_); } @@ -70,7 +81,11 @@ bool Monitor::SetErrOutput(std::string const& fname) { if (app->HasDevice(fname)) { err_device_ = app->GetDevice(fname); } else { - err_device_ = std::make_shared(fname, std::ios::out); + if (fname == "stderr") { + err_device_ = std::shared_ptr(&std::cerr, NullDeleter()); + } else { + err_device_ = std::make_shared(fname, std::ios::out); + } app->InstallDevice(fname, err_device_); } @@ -97,6 +112,13 @@ std::string Monitor::getSectionID() const { } } +void Monitor::Start() { + std::unique_lock lock(section_mutex); + + sections_.clear(); + sections_.push_back(1); +} + void Monitor::advance() { std::unique_lock lock(section_mutex); diff --git a/src/monitor.hpp b/src/monitor.hpp index fe642cf..73ad712 100644 --- a/src/monitor.hpp +++ b/src/monitor.hpp @@ -63,7 +63,7 @@ class Monitor { bool SetErrOutput(std::string const& fname); - static void Start() { advance(); } + static void Start(); protected: virtual std::string getTimeStamp() const; diff --git a/tests/03_output_to_screen.cpp b/tests/03_output_to_screen.cpp new file mode 100644 index 0000000..e790409 --- /dev/null +++ b/tests/03_output_to_screen.cpp @@ -0,0 +1,27 @@ +// C/C++ +#include + +// application +#include + +void func2() { + Application::Logger app("B"); + + app->Log("First step"); + app->Log("Second step"); +} + +void func1() { + Application::Logger app("A"); + + app->Log("First step"); + app->Log("Second step"); + + func2(); +} + +int main(int argc, char **argv) { + func1(); + + Application::Destroy(); +}