Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
25 class DefaultLoggerOutput : 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;
36  std::cout.flush();
37  }
38 };
39 
40 //! default logger singleton
41 static DefaultLoggerOutput s_default_logger;
42 
43 //! global logger output hook
44 static std::atomic<LoggerOutputHook*> s_logger_output_hook {
46 };
47 
49  return s_logger_output_hook.exchange(hook);
50 }
51 
52 //! global logger prefix hook
53 static std::atomic<LoggerPrefixHook*> s_logger_prefix_hook {
54  nullptr
55 };
56 
58  return s_logger_prefix_hook.exchange(hook);
59 }
60 
61 /******************************************************************************/
62 
64  LoggerPrefixHook* prefix_hook = s_logger_prefix_hook.load();
65  if (prefix_hook)
66  prefix_hook->add_log_prefix(oss_);
67 }
68 
70  oss_ << '\n';
71  (*s_logger_output_hook).append_log_line(oss_.str());
72 }
73 
75  LoggerPrefixHook* prefix_hook = s_logger_prefix_hook.load();
76  if (prefix_hook)
77  prefix_hook->add_log_prefix(oss_);
78 }
79 
81  oss_ << '\n';
82  (*s_logger_output_hook).append_log_line(oss_.str());
83 }
84 
85 /******************************************************************************/
86 
88 
89 /******************************************************************************/
90 
92 
93 /*----------------------------------------------------------------------------*/
94 
96  : echo_(echo) {
98 }
99 
101  // set old logger hook
103 }
104 
106  oss_.str(std::string());
107 }
108 
110  return oss_.str();
111 }
112 
114  oss_ << line;
115  if (echo_) {
116  // pass through
117  next_->append_log_line(line);
118  }
119 }
120 
121 } // namespace tlx
122 
123 /******************************************************************************/
virtual ~LoggerPrefixHook()
virtual destructor
Definition: core.cpp:87
virtual ~LoggerOutputHook()
virtual destructor
Definition: core.cpp:91
static DefaultLoggerOutput s_default_logger
default logger singleton
Definition: core.cpp:41
static std::atomic< LoggerPrefixHook * > s_logger_prefix_hook
global logger prefix hook
Definition: core.cpp:53
LoggerOutputHook * set_logger_output_hook(LoggerOutputHook *hook)
Definition: core.cpp:48
LoggerOutputHook * next_
previous logger, will be restored by destructor
Definition: core.hpp:214
~SpacingLogger()
destructor: output a newline
Definition: core.cpp:80
void append_log_line(const std::string &line) final
method the receive log lines
Definition: core.cpp:113
std::ostringstream oss_
collector stream
Definition: core.hpp:82
LoggerCollectOutput(bool echo=false)
Definition: core.cpp:95
SpacingLogger()
construction: add prefix if desired
Definition: core.cpp:74
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:217
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
~Logger()
destructor: output a newline
Definition: core.cpp:69
void clear()
clear transcript
Definition: core.cpp:105
std::string get()
return transcript of log
Definition: core.cpp:109
static std::atomic< LoggerOutputHook * > s_logger_output_hook
global logger output hook
Definition: core.cpp:44
std::ostringstream oss_
collector stream
Definition: core.hpp:110
LoggerPrefixHook * set_logger_prefix_hook(LoggerPrefixHook *hook)
Definition: core.cpp:57
std::ostringstream oss_
string stream collecting
Definition: core.hpp:220
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:63