OpenMS
LogStream.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow $
6 // $Authors: Chris Bielow, Stephan Aiche, Andreas Bertsch$
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Macros.h>
13 
14 #include <sstream>
15 #include <iostream>
16 #include <list>
17 #include <vector>
18 #include <ctime>
19 #include <map>
20 
21 namespace OpenMS
22 {
23  class Colorizer;
24 
51  namespace Logger
52  {
53  // forward declarations
54  class LogStream;
55  class LogStreamNotifier;
56 
78  class OPENMS_DLLAPI LogStreamBuf :
79  public std::streambuf
80  {
81 
82  friend class LogStream;
83 
84 public:
85 
87 
88  static const time_t MAX_TIME;
89  static const std::string UNKNOWN_LOG_LEVEL;
91 
93 
94 
95 
102  LogStreamBuf(const std::string& log_level = UNKNOWN_LOG_LEVEL, Colorizer* col = nullptr);
103 
107  ~LogStreamBuf() override;
108 
110 
112 
113 
124  int sync() override;
125 
130  int overflow(int c = -1) override;
132 
133 
135 
136 
141  void setLevel(std::string level);
142 
143 
147  std::string getLevel();
149 
155  struct OPENMS_DLLAPI StreamStruct
156  {
157  std::ostream * stream;
158  std::string prefix;
160 
162  stream(nullptr),
163  target(nullptr)
164  {}
165 
168  {}
169 
170  };
171 
177  void clearCache();
178 
179 protected:
180 
182  void distribute_(const std::string& outstring);
183 
185  std::string expandPrefix_(const std::string & prefix, time_t time) const;
186 
187  char * pbuf_ = nullptr;
188  std::string level_;
189  std::list<StreamStruct> stream_list_;
190  std::string incomplete_line_;
191  Colorizer* colorizer_ = nullptr;
193 
194 
199  {
201  int counter;
202  };
203 
208  Size log_cache_counter_ = 0;
209 
211  std::map<std::string, LogCacheStruct> log_cache_;
213  std::map<Size, std::string> log_time_cache_;
214 
216  bool isInCache_(std::string const & line);
217 
226  std::string addToCache_(std::string const & line);
227 
230 
232  int syncLF_();
234  };
235 
237  class OPENMS_DLLAPI LogStreamNotifier
238  {
239 public:
240 
243 
246 
248  virtual void logNotify();
249 
251  void registerAt(LogStream & log_stream);
252 
254  void unregister();
255 
256 protected:
257  std::stringstream stream_;
258 
260  };
261 
262 
290  class OPENMS_DLLAPI LogStream :
291  public std::ostream
292  {
293 public:
294 
296 
297 
307  LogStream(LogStreamBuf * buf = nullptr, bool delete_buf = true, std::ostream * stream = nullptr);
308 
310  ~LogStream() override;
312 
314 
315 
323 
327 
328 
330 
331 
337  void setLevel(std::string level);
338 
339 
343  std::string getLevel();
345 
347 
348 
363  void insert(std::ostream & s);
364 
375  void remove(std::ostream & s);
376 
381 
383  void insertNotification(std::ostream & s,
384  LogStreamNotifier & target);
385 
406  void setPrefix(const std::ostream & s, const std::string & prefix);
407 
408 
410  void setPrefix(const std::string & prefix);
411 
413  void flush();
415 private:
416 
417  typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
418 
419  StreamIterator findStream_(const std::ostream & stream);
420  bool hasStream_(std::ostream & stream);
421  bool bound_() const;
422 
428 
429  }; //LogStream
430 
431  } // namespace Logger
432 
434 #define OPENMS_LOG_FATAL_ERROR \
435  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
436  OpenMS_Log_fatal << __FILE__ << "(" << __LINE__ << "): "
437 
439 #define OPENMS_LOG_ERROR \
440  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
441  OpenMS_Log_error
442 
444 #define OPENMS_LOG_WARN \
445  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
446  OpenMS_Log_warn
447 
449 #define OPENMS_LOG_INFO \
450  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
451  OpenMS_Log_info
452 
454 #define OPENMS_LOG_DEBUG \
455  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
456  OpenMS_Log_debug << [](){ constexpr const char* x = (past_last_slash(__FILE__)); return x; }() << "(" << __LINE__ << "): "
457 
459 #define OPENMS_LOG_DEBUG_NOFILE \
460  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
461  OpenMS_Log_debug
462 
463  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_fatal;
464  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_error;
465  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_warn;
466  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_info;
467  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_debug;
468 
469 } // namespace OpenMS
Color and style the fonts shown on cout/cerr (or other streams)
Definition: Colorizer.h:71
Stream buffer used by LogStream.
Definition: LogStream.h:80
Size getNextLogCounter_()
Returns the next free index for a log message.
std::map< Size, std::string > log_time_cache_
Cache of the occurrence sequence of the last two log messages.
Definition: LogStream.h:213
std::string expandPrefix_(const std::string &prefix, time_t time) const
Interpret the prefix format string and return the expanded prefix.
std::string level_
Definition: LogStream.h:188
std::list< StreamStruct > stream_list_
Definition: LogStream.h:189
int counter
Definition: LogStream.h:201
Size timestamp
Definition: LogStream.h:200
std::map< std::string, LogCacheStruct > log_cache_
Cache of the last two log messages.
Definition: LogStream.h:211
LogStreamBuf(const std::string &log_level=UNKNOWN_LOG_LEVEL, Colorizer *col=nullptr)
int syncLF_()
Non-lock acquiring sync function called in the d'tor.
void setLevel(std::string level)
std::string addToCache_(std::string const &line)
std::string incomplete_line_
Definition: LogStream.h:190
void distribute_(const std::string &outstring)
Distribute a new message to connected streams.
static const std::string UNKNOWN_LOG_LEVEL
Definition: LogStream.h:89
bool isInCache_(std::string const &line)
Checks if the line is already in the cache.
static const time_t MAX_TIME
Definition: LogStream.h:88
int overflow(int c=-1) override
Holds a counter of occurrences and an index for the occurrence sequence of the corresponding log mess...
Definition: LogStream.h:199
Definition: LogStream.h:238
virtual ~LogStreamNotifier()
Destructor.
LogStreamNotifier()
Empty constructor.
LogStream * registered_at_
Definition: LogStream.h:259
void registerAt(LogStream &log_stream)
std::stringstream stream_
Definition: LogStream.h:257
Log Stream Class.
Definition: LogStream.h:292
bool hasStream_(std::ostream &stream)
LogStream(LogStreamBuf *buf=nullptr, bool delete_buf=true, std::ostream *stream=nullptr)
LogStreamBuf * rdbuf()
void insert(std::ostream &s)
std::list< LogStreamBuf::StreamStruct >::iterator StreamIterator
Definition: LogStream.h:417
~LogStream() override
Clears all message buffers.
LogStreamBuf * operator->()
Arrow operator.
void setPrefix(const std::ostream &s, const std::string &prefix)
bool delete_buffer_
Definition: LogStream.h:427
void insertNotification(std::ostream &s, LogStreamNotifier &target)
Add a notification target.
void setPrefix(const std::string &prefix)
Set prefix of all output streams, details see setPrefix method with ostream.
void setLevel(std::string level)
void remove(std::ostream &s)
StreamIterator findStream_(const std::ostream &stream)
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
const double c
Definition: Constants.h:188
static String prefix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:122
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Logger::LogStream OpenMS_Log_warn
Global static instance of a LogStream to capture messages classified as warnings. By default it is bo...
Logger::LogStream OpenMS_Log_error
Global static instance of a LogStream to capture messages classified as errors. By default it is boun...
Logger::LogStream OpenMS_Log_info
Global static instance of a LogStream to capture messages classified as information....
Logger::LogStream OpenMS_Log_fatal
Global static instance of a LogStream to capture messages classified as fatal errors....
Logger::LogStream OpenMS_Log_debug
Global static instance of a LogStream to capture messages classified as debug output....
Holds a stream that is connected to the LogStream. It also includes the minimum and maximum level at ...
Definition: LogStream.h:156
std::string prefix
Definition: LogStream.h:158
StreamStruct()
Definition: LogStream.h:161
LogStreamNotifier * target
Definition: LogStream.h:159
~StreamStruct()
Delete the notification target.
Definition: LogStream.h:167
std::ostream * stream
Definition: LogStream.h:157