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 {
75  namespace Logger
76  {
77  // forward declarations
78  class LogStream;
79  class LogStreamNotifier;
80 
102  class OPENMS_DLLAPI LogStreamBuf :
103  public std::streambuf
104  {
105 
106  friend class LogStream;
107 
108 public:
109 
111 
112  static const time_t MAX_TIME;
113  static const std::string UNKNOWN_LOG_LEVEL;
115 
117 
118 
123  explicit LogStreamBuf(std::string log_level = UNKNOWN_LOG_LEVEL);
124 
128  ~LogStreamBuf() override;
129 
131 
133 
134 
145  int sync() override;
146 
151  int overflow(int c = -1) override;
153 
154 
156 
157 
162  void setLevel(std::string level);
163 
164 
168  std::string getLevel();
170 
176  struct OPENMS_DLLAPI StreamStruct
177  {
178  std::ostream * stream;
179  std::string prefix;
181 
183  stream(nullptr),
184  target(nullptr)
185  {}
186 
189  {}
190 
191  };
192 
198  void clearCache();
199 
200 protected:
201 
203  void distribute_(std::string outstring);
204 
206  std::string expandPrefix_(const std::string & prefix, time_t time) const;
207 
208  char * pbuf_;
209  std::string level_;
210  std::list<StreamStruct> stream_list_;
211  std::string incomplete_line_;
212 
214 
215 
220  {
222  int counter;
223  };
224 
230 
232  std::map<std::string, LogCacheStruct> log_cache_;
234  std::map<Size, std::string> log_time_cache_;
235 
237  bool isInCache_(std::string const & line);
238 
247  std::string addToCache_(std::string const & line);
248 
250  Size getNextLogCounter_();
251 
253  int syncLF_();
255  };
256 
258  class OPENMS_DLLAPI LogStreamNotifier
259  {
260 public:
261 
264 
266  virtual ~LogStreamNotifier();
267 
269  virtual void logNotify();
270 
272  void registerAt(LogStream & log_stream);
273 
275  void unregister();
276 
277 protected:
278  std::stringstream stream_;
279 
281  };
282 
283 
311  class OPENMS_DLLAPI LogStream :
312  public std::ostream
313  {
314 public:
315 
317 
318 
328  LogStream(LogStreamBuf * buf = nullptr, bool delete_buf = true, std::ostream * stream = nullptr);
329 
331  ~LogStream() override;
333 
335 
336 
343  LogStreamBuf * rdbuf();
344 
346  LogStreamBuf * operator->();
348 
349 
351 
352 
358  void setLevel(std::string level);
359 
360 
364  std::string getLevel();
366 
368 
369 
384  void insert(std::ostream & s);
385 
396  void remove(std::ostream & s);
397 
399  void insertNotification(std::ostream & s,
400  LogStreamNotifier & target);
401 
422  void setPrefix(const std::ostream & s, const std::string & prefix);
423 
424 
426  void setPrefix(const std::string & prefix);
427 
429  void flush();
431 private:
432 
433  typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
434 
435  StreamIterator findStream_(const std::ostream & stream);
436  bool hasStream_(std::ostream & stream);
437  bool bound_() const;
438 
444 
445  }; //LogStream
446 
447  } // namespace Logger
448 
450 #define OPENMS_LOG_FATAL_ERROR \
451  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
452  OpenMS_Log_fatal << __FILE__ << "(" << __LINE__ << "): "
453 
455 #define OPENMS_LOG_ERROR \
456  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
457  OpenMS_Log_error
458 
460 #define OPENMS_LOG_WARN \
461  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
462  OpenMS_Log_warn
463 
465 #define OPENMS_LOG_INFO \
466  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
467  OpenMS_Log_info
468 
470 #define OPENMS_LOG_DEBUG \
471  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
472  OpenMS_Log_debug << [](){ constexpr const char* x = (past_last_slash(__FILE__)); return x; }() << "(" << __LINE__ << "): "
473 
475 #define OPENMS_LOG_DEBUG_NOFILE \
476  OPENMS_THREAD_CRITICAL(LOGSTREAM) \
477  OpenMS_Log_debug
478 
479  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_fatal;
480  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_error;
481  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_warn;
482  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_info;
483  OPENMS_DLLAPI extern Logger::LogStream OpenMS_Log_debug;
484 
485 } // namespace OpenMS
OpenMS::Logger::LogStreamBuf::StreamStruct::StreamStruct
StreamStruct()
Definition: LogStream.h:182
OpenMS::OpenMS_Log_fatal
Logger::LogStream OpenMS_Log_fatal
Global static instance of a LogStream to capture messages classified as fatal errors....
OpenMS::Logger::LogStreamBuf::LogCacheStruct::timestamp
Size timestamp
Definition: LogStream.h:221
OpenMS::Logger::LogStreamBuf::StreamStruct::prefix
std::string prefix
Definition: LogStream.h:179
OpenMS::Logger::LogStreamBuf::incomplete_line_
std::string incomplete_line_
Definition: LogStream.h:211
OpenMS::Logger::LogStreamBuf::LogCacheStruct
Holds a counter of occurrences and an index for the occurrence sequence of the corresponding log mess...
Definition: LogStream.h:219
OpenMS::Logger::LogStream::delete_buffer_
bool delete_buffer_
Definition: LogStream.h:443
OpenMS::Logger::LogStreamBuf::MAX_TIME
static const time_t MAX_TIME
Definition: LogStream.h:112
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::Constants::c
const double c
Definition: Constants.h:209
OpenMS::Logger::LogStreamBuf::StreamStruct::stream
std::ostream * stream
Definition: LogStream.h:178
OpenMS::Logger::LogStreamBuf::log_cache_counter_
Size log_cache_counter_
Definition: LogStream.h:229
Macros.h
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
OpenMS::Logger::LogStreamBuf
Stream buffer used by LogStream.
Definition: LogStream.h:102
OpenMS::StringUtils::remove
static String & remove(String &this_s, char what)
Definition: StringUtilsSimple.h:595
OpenMS::Logger::LogStreamNotifier::registered_at_
LogStream * registered_at_
Definition: LogStream.h:280
OpenMS::Logger::LogStreamBuf::StreamStruct::target
LogStreamNotifier * target
Definition: LogStream.h:180
OpenMS::Logger::LogStreamBuf::LogCacheStruct::counter
int counter
Definition: LogStream.h:222
OpenMS::Logger::LogStreamNotifier
Definition: LogStream.h:258
OpenMS::Logger::LogStreamBuf::level_
std::string level_
Definition: LogStream.h:209
OpenMS::Logger::LogStream::StreamIterator
std::list< LogStreamBuf::StreamStruct >::iterator StreamIterator
Definition: LogStream.h:433
OpenMS::Logger::LogStream
Log Stream Class.
Definition: LogStream.h:311
OpenMS::Logger::LogStreamBuf::StreamStruct::~StreamStruct
~StreamStruct()
Delete the notification target.
Definition: LogStream.h:188
OpenMS::Logger::LogStreamBuf::pbuf_
char * pbuf_
Definition: LogStream.h:208
OpenMS::OpenMS_Log_error
Logger::LogStream OpenMS_Log_error
Global static instance of a LogStream to capture messages classified as errors. By default it is boun...
OpenMS::OpenMS_Log_debug
Logger::LogStream OpenMS_Log_debug
Global static instance of a LogStream to capture messages classified as debug output....
OpenMS::Logger::LogStreamBuf::UNKNOWN_LOG_LEVEL
static const std::string UNKNOWN_LOG_LEVEL
Definition: LogStream.h:113
OpenMS::Logger::LogStreamBuf::log_time_cache_
std::map< Size, std::string > log_time_cache_
Cache of the occurrence sequence of the last two log messages.
Definition: LogStream.h:234
OpenMS::OpenMS_Log_info
Logger::LogStream OpenMS_Log_info
Global static instance of a LogStream to capture messages classified as information....
String.h
OpenMS::Logger::LogStreamBuf::log_cache_
std::map< std::string, LogCacheStruct > log_cache_
Cache of the last two log messages.
Definition: LogStream.h:232
OpenMS::StringUtils::prefix
static String prefix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:147
OpenMS::Logger::LogStreamBuf::stream_list_
std::list< StreamStruct > stream_list_
Definition: LogStream.h:210
OpenMS::Logger::LogStreamBuf::StreamStruct
Holds a stream that is connected to the LogStream. It also includes the minimum and maximum level at ...
Definition: LogStream.h:176
OpenMS::Logger::LogStreamNotifier::stream_
std::stringstream stream_
Definition: LogStream.h:278
OpenMS::OpenMS_Log_warn
Logger::LogStream OpenMS_Log_warn
Global static instance of a LogStream to capture messages classified as warnings. By default it is bo...