tlx
core.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/logger/core.cpp
3  *
4  * Simple logging methods using ostream output.
5  *
6  * Part of tlx - http://panthema.net/tlx
7  *
8  * Copyright (C) 2015-2018 Timo Bingmann <tb@panthema.net>
9  *
10  * All rights reserved. Published under the Boost Software License, Version 1.0
11  ******************************************************************************/
12 
13 #include <tlx/logger/core.hpp>
14 
15 #include <atomic>
16 #include <iostream>
17 #include <mutex>
18 #include <string>
19 
20 namespace tlx {
21 
22 /******************************************************************************/
23 
24 //! default output logger to cout
25 class DefaultLoggerOutputCOut : public LoggerOutputHook
26 {
27  //! the global mutex of logger and spacing logger
28  std::mutex mutex_;
29 
30  //! method the receive log lines
31  void append_log_line(const std::string& line) final {
32  // lock the global mutex of logger for serialized output in
33  // multi-threaded programs.
34  std::unique_lock<std::mutex> lock(mutex_);
35  (std::cout << line).flush();
36  }
37 };
38 
39 //! default output logger to cerr
40 class DefaultLoggerOutputCErr : public LoggerOutputHook
41 {
42  //! the global mutex of logger and spacing logger
43  std::mutex mutex_;
44 
45  //! method the receive log lines
46  void append_log_line(const std::string& line) final {
47  // lock the global mutex of logger for serialized output in
48  // multi-threaded programs.
49  std::unique_lock<std::mutex> lock(mutex_);
50  (std::cerr << line).flush();
51  }
52 };
53 
54 //! default logger singletons
56 
57 //! default logger singletons
59 
60 //! global logger output hook
61 static std::atomic<LoggerOutputHook*> s_logger_output_hook {
63 };
64 
66  return s_logger_output_hook.exchange(hook);
67 }
68 
71 }
72 
73 /******************************************************************************/
74 
75 //! global logger prefix hook
76 static std::atomic<LoggerPrefixHook*> s_logger_prefix_hook {
77  nullptr
78 };
79 
81  return s_logger_prefix_hook.exchange(hook);
82 }
83 
84 /******************************************************************************/
85 
87  LoggerPrefixHook* prefix_hook = s_logger_prefix_hook.load();
88  if (prefix_hook)
89  prefix_hook->add_log_prefix(oss_);
90 }
91 
93  oss_ << '\n';
94  (*s_logger_output_hook).append_log_line(oss_.str());
95 }
96 
98  LoggerPrefixHook* prefix_hook = s_logger_prefix_hook.load();
99  if (prefix_hook)
100  prefix_hook->add_log_prefix(oss_);
101 }
102 
104  oss_ << '\n';
105  (*s_logger_output_hook).append_log_line(oss_.str());
106 }
107 
108 /******************************************************************************/
109 
111 
112 /******************************************************************************/
113 
115 
116 /*----------------------------------------------------------------------------*/
117 
119  : echo_(echo) {
120  next_ = set_logger_output_hook(this);
121 }
122 
124  // set old logger hook
126 }
127 
129  oss_.str(std::string());
130 }
131 
132 std::string LoggerCollectOutput::get() {
133  return oss_.str();
134 }
135 
136 void LoggerCollectOutput::append_log_line(const std::string& line) {
137  oss_ << line;
138  if (echo_) {
139  // pass through
140  next_->append_log_line(line);
141  }
142 }
143 
144 } // namespace tlx
145 
146 /******************************************************************************/
tlx::set_logger_output_hook
LoggerOutputHook * set_logger_output_hook(LoggerOutputHook *hook)
set new LoggerOutputHook instance to receive global log lines.
Definition: core.cpp:75
tlx::LoggerCollectOutput::LoggerCollectOutput
LoggerCollectOutput(bool echo=false)
Definition: core.cpp:128
tlx::s_default_logger_cout
static DefaultLoggerOutputCOut s_default_logger_cout
default logger singletons
Definition: core.cpp:65
tlx::DefaultLoggerOutputCOut::mutex_
std::mutex mutex_
the global mutex of logger and spacing logger
Definition: core.cpp:48
tlx::LoggerPrefixHook::~LoggerPrefixHook
virtual ~LoggerPrefixHook()
virtual destructor
Definition: core.cpp:120
tlx::LoggerCollectOutput::~LoggerCollectOutput
~LoggerCollectOutput()
Definition: core.cpp:133
tlx::SpacingLogger::oss_
std::ostringstream oss_
collector stream
Definition: core.hpp:120
tlx::DefaultLoggerOutputCOut::append_log_line
void append_log_line(const std::string &line) final
method the receive log lines
Definition: core.cpp:51
tlx::s_logger_prefix_hook
static std::atomic< LoggerPrefixHook * > s_logger_prefix_hook
global logger prefix hook
Definition: core.cpp:86
tlx::DefaultLoggerOutputCErr
default output logger to cerr
Definition: core.cpp:50
tlx::LoggerCollectOutput::clear
void clear()
clear transcript
Definition: core.cpp:138
tlx::LoggerCollectOutput::echo_
bool echo_
whether to echo each line to next logger output
Definition: core.hpp:231
tlx::Logger::Logger
Logger()
construction: add prefix if desired
Definition: core.cpp:96
tlx::LoggerCollectOutput::append_log_line
void append_log_line(const std::string &line) final
method the receive log lines
Definition: core.cpp:146
tlx::DefaultLoggerOutputCErr::append_log_line
void append_log_line(const std::string &line) final
method the receive log lines
Definition: core.cpp:56
tlx
Definition: exclusive_scan.hpp:17
tlx::Logger::~Logger
~Logger()
destructor: output a newline
Definition: core.cpp:102
tlx::DefaultLoggerOutputCOut
default output logger to cout
Definition: core.cpp:35
tlx::LoggerCollectOutput::oss_
std::ostringstream oss_
string stream collecting
Definition: core.hpp:234
tlx::Logger::oss_
std::ostringstream oss_
collector stream
Definition: core.hpp:92
tlx::LoggerCollectOutput::get
std::string get()
return transcript of log
Definition: core.cpp:142
tlx::set_logger_prefix_hook
LoggerPrefixHook * set_logger_prefix_hook(LoggerPrefixHook *hook)
Set new LoggerPrefixHook instance to prefix global log lines.
Definition: core.cpp:90
tlx::s_logger_output_hook
static std::atomic< LoggerOutputHook * > s_logger_output_hook
global logger output hook
Definition: core.cpp:71
tlx::LoggerOutputHook::append_log_line
virtual void append_log_line(const std::string &line)=0
method the receive log lines
tlx::SpacingLogger::SpacingLogger
SpacingLogger()
construction: add prefix if desired
Definition: core.cpp:107
tlx::DefaultLoggerOutputCErr::mutex_
std::mutex mutex_
the global mutex of logger and spacing logger
Definition: core.cpp:53
tlx::set_logger_to_stderr
LoggerOutputHook * set_logger_to_stderr()
install default logger to cerr / stderr instead of stdout.
Definition: core.cpp:79
tlx::SpacingLogger::~SpacingLogger
~SpacingLogger()
destructor: output a newline
Definition: core.cpp:113
tlx::LoggerPrefixHook
Abstract class to implement prefix output hooks for logging.
Definition: core.hpp:172
tlx::s_default_logger_cerr
static DefaultLoggerOutputCErr s_default_logger_cerr
default logger singletons
Definition: core.cpp:68
tlx::LoggerCollectOutput::next_
LoggerOutputHook * next_
previous logger, will be restored by destructor
Definition: core.hpp:228
core.hpp
tlx::LoggerOutputHook
Abstract class to implement output hooks for logging.
Definition: core.hpp:190
tlx::LoggerPrefixHook::add_log_prefix
virtual void add_log_prefix(std::ostream &os)=0
method to add prefix to log lines
tlx::LoggerOutputHook::~LoggerOutputHook
virtual ~LoggerOutputHook()
virtual destructor
Definition: core.cpp:124