Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
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-2017.
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 #ifndef OPENMS_CONCEPT_LOGSTREAM_H
36 #define OPENMS_CONCEPT_LOGSTREAM_H
37 
39 
40 
41 #include <sstream>
42 #include <iostream>
43 #include <list>
44 #include <vector>
45 #include <ctime>
46 #include <map>
47 
48 namespace OpenMS
49 {
76  namespace Logger
77  {
78  // forward declarations
79  class LogStream;
80  class LogStreamNotifier;
81 
103  class OPENMS_DLLAPI LogStreamBuf :
104  public std::streambuf
105  {
106 
107  friend class LogStream;
108 
109 public:
110 
112 
113  static const time_t MAX_TIME;
114  static const std::string UNKNOWN_LOG_LEVEL;
116 
118 
119 
124  explicit LogStreamBuf(std::string log_level = UNKNOWN_LOG_LEVEL);
125 
129  virtual ~LogStreamBuf();
130 
132 
134 
135 
146  virtual int sync();
147 
152  virtual int overflow(int c = -1);
154 
155 
157 
158 
163  void setLevel(std::string level);
164 
165 
169  std::string getLevel();
171 
177  struct OPENMS_DLLAPI StreamStruct
178  {
179  std::ostream * stream;
180  std::string prefix;
182 
184  stream(0),
185  target(0)
186  {}
187 
190  {}
191 
192  };
193 
199  void clearCache();
200 
201 protected:
202 
204  void distribute_(std::string outstring);
205 
207  std::string expandPrefix_(const std::string & prefix, time_t time) const;
208 
209  char * pbuf_;
210  std::string level_;
211  std::list<StreamStruct> stream_list_;
212  std::string incomplete_line_;
213 
215 
216 
221  {
223  int counter;
224  };
225 
231 
233  std::map<std::string, LogCacheStruct> log_cache_;
235  std::map<Size, std::string> log_time_cache_;
236 
238  bool isInCache_(std::string const & line);
239 
248  std::string addToCache_(std::string const & line);
249 
251  Size getNextLogCounter_();
252 
254  };
255 
257  class OPENMS_DLLAPI LogStreamNotifier
258  {
259 public:
260 
263 
265  virtual ~LogStreamNotifier();
266 
268  virtual void logNotify();
269 
271  void registerAt(LogStream & log_stream);
272 
274  void unregister();
275 
276 protected:
277  std::stringstream stream_;
278 
280  };
281 
282 
303  class OPENMS_DLLAPI LogStream :
304  public std::ostream
305  {
306 public:
307 
309 
310 
320  LogStream(LogStreamBuf * buf = 0, bool delete_buf = true, std::ostream * stream = 0);
321 
323  virtual ~LogStream();
325 
327 
328 
335  LogStreamBuf * rdbuf();
336 
338  LogStreamBuf * operator->();
340 
341 
343 
344 
350  void setLevel(std::string level);
351 
352 
356  std::string getLevel();
358 
360 
361 
376  void insert(std::ostream & s);
377 
388  void remove(std::ostream & s);
389 
391  void insertNotification(std::ostream & s,
392  LogStreamNotifier & target);
393 
414  void setPrefix(const std::ostream & s, const std::string & prefix);
415 
416 
418  void setPrefix(const std::string & prefix);
419 
421  void flush();
423 private:
424 
425  typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
426 
427  StreamIterator findStream_(const std::ostream & stream);
428  bool hasStream_(std::ostream & stream);
429  bool bound_() const;
430 
436 
437  }; //LogStream
438 
439  } // namespace Logger
440 
441 
443 #define LOG_FATAL_ERROR \
444  Log_fatal << __FILE__ << "(" << __LINE__ << "): "
445 
447 #define LOG_ERROR \
448  Log_error
449 
451 #define LOG_WARN \
452  Log_warn
453 
455 #define LOG_INFO \
456  Log_info
457 
459 #define LOG_DEBUG \
460  Log_debug << __FILE__ << "(" << __LINE__ << "): "
461 
462  OPENMS_DLLAPI extern Logger::LogStream Log_fatal;
463  OPENMS_DLLAPI extern Logger::LogStream Log_error;
464  OPENMS_DLLAPI extern Logger::LogStream Log_warn;
465  OPENMS_DLLAPI extern Logger::LogStream Log_info;
466  OPENMS_DLLAPI extern Logger::LogStream Log_debug;
467 
468 } // namespace OpenMS
469 
470 #endif // OPENMS_CONCEPT_LOGSTREAM_H
std::string level_
Definition: LogStream.h:210
Logger::LogStream 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.
Logger::LogStream Log_warn
Global static instance of a LogStream to capture messages classified as warnings. By default it is bo...
static const std::string UNKNOWN_LOG_LEVEL
Definition: LogStream.h:114
Definition: LogStream.h:257
LogStream * registered_at_
Definition: LogStream.h:279
const double c
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
StreamStruct()
Definition: LogStream.h:183
std::list< StreamStruct > stream_list_
Definition: LogStream.h:211
bool delete_buffer_
Definition: LogStream.h:435
Log Stream Class.
Definition: LogStream.h:303
Logger::LogStream Log_info
Global static instance of a LogStream to capture messages classified as information. By default it is bound to cout.
std::ostream * stream
Definition: LogStream.h:179
std::map< Size, std::string > log_time_cache_
Cache of the occurrence sequence of the last two log messages.
Definition: LogStream.h:235
std::string prefix
Definition: LogStream.h:180
Logger::LogStream Log_error
Global static instance of a LogStream to capture messages classified as errors. By default it is boun...
std::stringstream stream_
Definition: LogStream.h:277
Size log_cache_counter_
Definition: LogStream.h:230
~StreamStruct()
Delete the notification target.
Definition: LogStream.h:189
Holds a stream that is connected to the LogStream. It also includes the minimum and maximum level at ...
Definition: LogStream.h:177
std::string incomplete_line_
Definition: LogStream.h:212
std::map< std::string, LogCacheStruct > log_cache_
Cache of the last two log messages.
Definition: LogStream.h:233
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
LogStreamNotifier * target
Definition: LogStream.h:181
Logger::LogStream Log_fatal
Global static instance of a LogStream to capture messages classified as fatal errors. By default it is bound to cerr.
char * pbuf_
Definition: LogStream.h:209
static const time_t MAX_TIME
Definition: LogStream.h:113
std::list< LogStreamBuf::StreamStruct >::iterator StreamIterator
Definition: LogStream.h:425
int counter
Definition: LogStream.h:223
Size timestamp
Definition: LogStream.h:222
Stream buffer used by LogStream.
Definition: LogStream.h:103
Holds a counter of occurrences and an index for the occurrence sequence of the corresponding log mess...
Definition: LogStream.h:220

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:01 using doxygen 1.8.13