Skip to content

Commit

Permalink
add stdout (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengcli authored Jun 30, 2023
1 parent ca2e3cd commit 150c00a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
26 changes: 24 additions & 2 deletions src/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -47,6 +51,9 @@ void Monitor::Enter() {
void Monitor::Leave() {
std::unique_lock<std::mutex> lock(section_mutex);

log_device_->flush();
err_device_->flush();

sections_.pop_back();
if (sections_.size() > 0) sections_.back() += 1;
}
Expand All @@ -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<std::ofstream>(fname, std::ios::out);
if (fname == "stdout") {
log_device_ = std::shared_ptr<std::ostream>(&std::cout, NullDeleter());
} else {
log_device_ = std::make_shared<std::ofstream>(fname, std::ios::out);
}
app->InstallDevice(fname, log_device_);
}

Expand All @@ -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<std::ofstream>(fname, std::ios::out);
if (fname == "stderr") {
err_device_ = std::shared_ptr<std::ostream>(&std::cerr, NullDeleter());
} else {
err_device_ = std::make_shared<std::ofstream>(fname, std::ios::out);
}
app->InstallDevice(fname, err_device_);
}

Expand All @@ -97,6 +112,13 @@ std::string Monitor::getSectionID() const {
}
}

void Monitor::Start() {
std::unique_lock<std::mutex> lock(section_mutex);

sections_.clear();
sections_.push_back(1);
}

void Monitor::advance() {
std::unique_lock<std::mutex> lock(section_mutex);

Expand Down
2 changes: 1 addition & 1 deletion src/monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions tests/03_output_to_screen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// C/C++
#include <iostream>

// application
#include <application/application.hpp>

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();
}

0 comments on commit 150c00a

Please sign in to comment.