Thrill  0.1
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 <[email protected]>
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
55 static DefaultLoggerOutputCOut s_default_logger_cout;
56 
57 //! default logger singletons
58 static DefaultLoggerOutputCErr s_default_logger_cerr;
59 
60 //! global logger output hook
61 static std::atomic<LoggerOutputHook*> s_logger_output_hook {
62  &s_default_logger_cout
63 };
64 
66  return s_logger_output_hook.exchange(hook);
67 }
68 
70  return set_logger_output_hook(&s_default_logger_cerr);
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) {
121 }
122 
124  // set old logger hook
126 }
127 
129  oss_.str(std::string());
130 }
131 
133  return oss_.str();
134 }
135 
137  oss_ << line;
138  if (echo_) {
139  // pass through
140  next_->append_log_line(line);
141  }
142 }
143 
144 } // namespace tlx
145 
146 /******************************************************************************/
virtual ~LoggerPrefixHook()
virtual destructor
Definition: core.cpp:110
virtual ~LoggerOutputHook()
virtual destructor
Definition: core.cpp:114
static DefaultLoggerOutputCOut s_default_logger_cout
default logger singletons
Definition: core.cpp:55
static std::atomic< LoggerPrefixHook * > s_logger_prefix_hook
global logger prefix hook
Definition: core.cpp:76
LoggerOutputHook * set_logger_output_hook(LoggerOutputHook *hook)
Definition: core.cpp:65
LoggerOutputHook * next_
previous logger, will be restored by destructor
Definition: core.hpp:218
~SpacingLogger()
destructor: output a newline
Definition: core.cpp:103
void append_log_line(const std::string &line) final
method the receive log lines
Definition: core.cpp:136
LoggerCollectOutput(bool echo=false)
Definition: core.cpp:118
SpacingLogger()
construction: add prefix if desired
Definition: core.cpp:97
Abstract class to implement output hooks for logging.
Definition: core.hpp:180
bool echo_
whether to echo each line to next logger output
Definition: core.hpp:221
virtual void append_log_line(const std::string &line)=0
method the receive log lines
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
Abstract class to implement prefix output hooks for logging.
Definition: core.hpp:162
static DefaultLoggerOutputCErr s_default_logger_cerr
default logger singletons
Definition: core.cpp:58
~Logger()
destructor: output a newline
Definition: core.cpp:92
void clear()
clear transcript
Definition: core.cpp:128
std::string get()
return transcript of log
Definition: core.cpp:132
LoggerOutputHook * set_logger_to_stderr()
Definition: core.cpp:69
static std::atomic< LoggerOutputHook * > s_logger_output_hook
global logger output hook
Definition: core.cpp:61
LoggerPrefixHook * set_logger_prefix_hook(LoggerPrefixHook *hook)
Definition: core.cpp:80
std::ostringstream oss_
string stream collecting
Definition: core.hpp:224
virtual void add_log_prefix(std::ostream &os)=0
method to add prefix to log lines
Logger()
construction: add prefix if desired
Definition: core.cpp:86