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
20namespace tlx {
21
22/******************************************************************************/
23
24//! default output logger to cout
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
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
61static 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
76static 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) {
121}
122
124 // set old logger hook
126}
127
129 oss_.str(std::string());
130}
131
133 return oss_.str();
134}
135
136void 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/******************************************************************************/
default output logger to cerr
Definition: core.cpp:41
void append_log_line(const std::string &line) final
method the receive log lines
Definition: core.cpp:46
std::mutex mutex_
the global mutex of logger and spacing logger
Definition: core.cpp:43
default output logger to cout
Definition: core.cpp:26
void append_log_line(const std::string &line) final
method the receive log lines
Definition: core.cpp:31
std::mutex mutex_
the global mutex of logger and spacing logger
Definition: core.cpp:28
void append_log_line(const std::string &line) final
method the receive log lines
Definition: core.cpp:136
std::string get()
return transcript of log
Definition: core.cpp:132
LoggerOutputHook * next_
previous logger, will be restored by destructor
Definition: core.hpp:218
void clear()
clear transcript
Definition: core.cpp:128
std::ostringstream oss_
string stream collecting
Definition: core.hpp:224
bool echo_
whether to echo each line to next logger output
Definition: core.hpp:221
LoggerCollectOutput(bool echo=false)
Definition: core.cpp:118
Abstract class to implement output hooks for logging.
Definition: core.hpp:181
virtual void append_log_line(const std::string &line)=0
method the receive log lines
virtual ~LoggerOutputHook()
virtual destructor
Definition: core.cpp:114
Abstract class to implement prefix output hooks for logging.
Definition: core.hpp:163
virtual void add_log_prefix(std::ostream &os)=0
method to add prefix to log lines
virtual ~LoggerPrefixHook()
virtual destructor
Definition: core.cpp:110
~Logger()
destructor: output a newline
Definition: core.cpp:92
Logger()
construction: add prefix if desired
Definition: core.cpp:86
std::ostringstream oss_
collector stream
Definition: core.hpp:82
SpacingLogger()
construction: add prefix if desired
Definition: core.cpp:97
~SpacingLogger()
destructor: output a newline
Definition: core.cpp:103
std::ostringstream oss_
collector stream
Definition: core.hpp:110
static std::atomic< LoggerOutputHook * > s_logger_output_hook
global logger output hook
Definition: core.cpp:61
static DefaultLoggerOutputCErr s_default_logger_cerr
default logger singletons
Definition: core.cpp:58
LoggerOutputHook * set_logger_output_hook(LoggerOutputHook *hook)
set new LoggerOutputHook instance to receive global log lines.
Definition: core.cpp:65
static DefaultLoggerOutputCOut s_default_logger_cout
default logger singletons
Definition: core.cpp:55
LoggerPrefixHook * set_logger_prefix_hook(LoggerPrefixHook *hook)
Set new LoggerPrefixHook instance to prefix global log lines.
Definition: core.cpp:80
LoggerOutputHook * set_logger_to_stderr()
install default logger to cerr / stderr instead of stdout.
Definition: core.cpp:69
static std::atomic< LoggerPrefixHook * > s_logger_prefix_hook
global logger prefix hook
Definition: core.cpp:76