OpenMS  3.0.0
LogStream.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2022.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow $
32 // $Authors: Chris Bielow, Stephan Aiche, Andreas Bertsch$
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Macros.h>
39 
40 #include <sstream>
41 #include <iostream>
42 #include <list>
43 #include <vector>
44 #include <ctime>
45 #include <map>
46 
47 namespace OpenMS
48 {
49  class Colorizer;
50 
77  namespace Logger
78  {
79  // forward declarations
80  class LogStream;
81  class LogStreamNotifier;
82 
104  class OPENMS_DLLAPI LogStreamBuf :
105  public std::streambuf
106  {
107 
108  friend class LogStream;
109 
110 public:
111 
113 
114  static const time_t MAX_TIME;
115  static const std::string UNKNOWN_LOG_LEVEL;
117 
119 
120 
121 
128  LogStreamBuf(const std::string& log_level = UNKNOWN_LOG_LEVEL, Colorizer* col = nullptr);
129 
133  ~LogStreamBuf() override;
134 
136 
138 
139 
150  int sync() override;
151 
156  int overflow(int c = -1) override;
158 
159 
161 
162 
167  void setLevel(std::string level);
168 
169 
173  std::string getLevel();
175 
181  struct OPENMS_DLLAPI StreamStruct
182  {
183  std::ostream * stream;
184  std::string prefix;
186 
188  stream(nullptr),
189  target(nullptr)
190  {}
191 
194  {}
195 
196  };
197 
203  void clearCache();
204 
205 protected:
206 
208  void distribute_(const std::string& outstring);
209 
211  std::string expandPrefix_(const std::string & prefix, time_t time) const;
212 
213  char * pbuf_ = nullptr;
214  std::string level_;
215  std::list<StreamStruct> stream_list_;
216  std::string incomplete_line_;
217  Colorizer* colorizer_ = nullptr;
218 
220 
225  {
227  int counter;
228  };
229 
234  Size log_cache_counter_ = 0;
235 
237  std::map<std::string, LogCacheStruct> log_cache_;
239  std::map<Size, std::string> log_time_cache_;
240 
242  bool isInCache_(std::string const & line);
243 
252  std::string addToCache_(std::string const & line);
253 
255  Size getNextLogCounter_();
256 
258  int syncLF_();
260  };
261 
263  class OPENMS_DLLAPI LogStreamNotifier
264  {
265 public:
266 
269 
271  virtual ~LogStreamNotifier();
272 
274  virtual void logNotify();
275 
277  void registerAt(LogStream & log_stream);
278 
280  void unregister();
281 
282 protected:
283  std::stringstream stream_;
284 
286  };
287 
288 
316  class OPENMS_DLLAPI LogStream :
317  public std::ostream
318  {
319 public:
320 
322 
323 
333  LogStream(LogStreamBuf * buf = nullptr, bool delete_buf = true, std::ostream * stream = nullptr);
334 
336  ~LogStream() override;
338 
340 
341 
348  LogStreamBuf * rdbuf();
349 
351  LogStreamBuf * operator->();
353 
354 
356 
357 
363  void setLevel(std::string level);
364 
365 
369  std::string getLevel();
371 
373 
374 
389  void insert(std::ostream & s);
390 
401  void remove(std::ostream & s);
402 
404  void insertNotification(std::ostream & s,
405  LogStreamNotifier & target);
406 
427  void setPrefix(const std::ostream & s, const std::string & prefix);
428 
429 
431  void setPrefix(const std::string & prefix);
432 
434  void flush();
436 private:
437 
438  typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
439 
440  StreamIterator findStream_(const std::ostream & stream);
441  bool hasStream_(std::ostream & stream);
442  bool bound_() const;
443 
449 
450  }; //LogStream
451 
452  } // namespace Logger
453 
455 #define OPENMS_LOG_FATAL_ERROR \
456  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
457  OpenMS_Log_fatal << __FILE__ << "(" << __LINE__ << "): "
458 
460 #define OPENMS_LOG_ERROR \
461  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
462  OpenMS_Log_error
463 
465 #define OPENMS_LOG_WARN \
466  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
467  OpenMS_Log_warn
468 
470 #define OPENMS_LOG_INFO \
471  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
472  OpenMS_Log_info
473 
475 #define OPENMS_LOG_DEBUG \
476  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
477  OpenMS_Log_debug << [](){ constexpr const char* x = (past_last_slash(__FILE__)); return x; }() << "(" << __LINE__ << "): "
478 
480 #define OPENMS_LOG_DEBUG_NOFILE \
481  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
482  OpenMS_Log_debug
483 
484  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_fatal;
485  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_error;
486  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_warn;
487  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_info;
488  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_debug;
489 
490 } // namespace OpenMS
std::string level_
Definition: LogStream.h:214
int counter
Definition: LogStream.h:227
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_debug
Global static instance of a LogStream to capture messages classified as debug output. By default it is not bound to any output stream. TOPP(AS)Base will connect cout, iff 0 < debug-level.
static const std::string UNKNOWN_LOG_LEVEL
Definition: LogStream.h:115
Definition: LogStream.h:263
LogStream * registered_at_
Definition: LogStream.h:285
const double c
Definition: Constants.h:214
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
StreamStruct()
Definition: LogStream.h:187
std::list< StreamStruct > stream_list_
Definition: LogStream.h:215
bool delete_buffer_
Definition: LogStream.h:448
Log Stream Class.
Definition: LogStream.h:316
std::ostream * stream
Definition: LogStream.h:183
std::map< Size, std::string > log_time_cache_
Cache of the occurrence sequence of the last two log messages.
Definition: LogStream.h:239
std::string prefix
Definition: LogStream.h:184
static String prefix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:147
std::stringstream stream_
Definition: LogStream.h:283
Holds a counter of occurrences and an index for the occurrence sequence of the corresponding log mess...
Definition: LogStream.h:224
Logger::LogStream OpenMS_Log_fatal
Global static instance of a LogStream to capture messages classified as fatal errors. By default it is bound to cerr.
~StreamStruct()
Delete the notification target.
Definition: LogStream.h:193
Color and style the fonts shown on cout/cerr (or other streams)
Definition: Colorizer.h:96
Holds a stream that is connected to the LogStream. It also includes the minimum and maximum level at ...
Definition: LogStream.h:181
std::string incomplete_line_
Definition: LogStream.h:216
Logger::LogStream OpenMS_Log_error
Global static instance of a LogStream to capture messages classified as errors. By default it is boun...
std::map< std::string, LogCacheStruct > log_cache_
Cache of the last two log messages.
Definition: LogStream.h:237
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
LogStreamNotifier * target
Definition: LogStream.h:185
static const time_t MAX_TIME
Definition: LogStream.h:114
std::list< LogStreamBuf::StreamStruct >::iterator StreamIterator
Definition: LogStream.h:438
Logger::LogStream OpenMS_Log_info
Global static instance of a LogStream to capture messages classified as information. By default it is bound to cout.
Size timestamp
Definition: LogStream.h:226
Stream buffer used by LogStream.
Definition: LogStream.h:104