OpenMS
Loading...
Searching...
No Matches
LogStream.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- 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
13
14#include <sstream>
15#include <iostream>
16#include <list>
17#include <vector>
18#include <ctime>
19#include <map>
20
21namespace OpenMS
22{
23 class Colorizer;
24
51 namespace Logger
52 {
53 // forward declarations
54 class LogStream;
56
78 class OPENMS_DLLAPI LogStreamBuf :
79 public std::streambuf
80 {
81
82 friend class LogStream;
83
84public:
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
114 LogStreamBuf(LogStreamBuf* source_buf, Colorizer* col = nullptr);
115
119 ~LogStreamBuf() override;
120
122
124
125
136 int sync() override;
137
142 int overflow(int c = -1) override;
144
145
147
148
153 void setLevel(std::string level);
154
155
159 std::string getLevel();
161
167 struct OPENMS_DLLAPI StreamStruct
168 {
169 std::ostream * stream;
170 std::string prefix;
172
174 stream(nullptr),
175 target(nullptr)
176 {}
177
181
182 };
183
190
191protected:
192
194 void distribute_(const std::string& outstring);
195
197 std::string expandPrefix_(const std::string & prefix, time_t time) const;
198
200 std::list<StreamStruct>& getStreamList_();
201 const std::list<StreamStruct>& getStreamList_() const;
202
203 char * pbuf_ = nullptr;
204 std::string level_;
205 std::list<StreamStruct> stream_list_;
206 std::string incomplete_line_;
207 Colorizer* colorizer_ = nullptr;
209
210
215 {
218 };
219
224 Size log_cache_counter_ = 0;
225
227 std::map<std::string, LogCacheStruct> log_cache_;
229 std::map<Size, std::string> log_time_cache_;
230
232 bool isInCache_(std::string const & line);
233
242 std::string addToCache_(std::string const & line);
243
246
248 int syncLF_();
250 };
251
253 class OPENMS_DLLAPI LogStreamNotifier
254 {
255public:
256
259
262
264 virtual void logNotify();
265
267 void registerAt(LogStream & log_stream);
268
271
272protected:
273 std::stringstream stream_;
274
276 };
277
278
306 class OPENMS_DLLAPI LogStream :
307 public std::ostream
308 {
309public:
310
312
313
323 LogStream(LogStreamBuf * buf = nullptr, bool delete_buf = true, std::ostream * stream = nullptr);
324
326 ~LogStream() override;
328
330
331
339
343
344
346
347
353 void setLevel(std::string level);
354
355
359 std::string getLevel();
361
363
364
379 void insert(std::ostream & s);
380
391 void remove(std::ostream & s);
392
400
402 void insertNotification(std::ostream & s,
403 LogStreamNotifier & target);
404
425 void setPrefix(const std::ostream & s, const std::string & prefix);
426
427
429 void setPrefix(const std::string & prefix);
430
432 void flush();
433
442private:
443
444 typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
445
446 StreamIterator findStream_(const std::ostream & stream);
447 bool hasStream_(std::ostream & stream);
448 bool bound_() const;
449
455
456 }; //LogStream
457
474 class OPENMS_DLLAPI LogSinkGuard
475 {
476 public:
483 LogSinkGuard(LogStream& log_stream, std::ostream& stream)
484 : log_stream_(log_stream), stream_(stream)
485 {
486 log_stream_.remove(stream_);
487 }
488
491 {
492 log_stream_.insert(stream_);
493 }
494
495 // Non-copyable and non-movable
496 LogSinkGuard(const LogSinkGuard&) = delete;
500
501 private:
503 std::ostream& stream_;
504 };
505
506 } // namespace Logger
507
508 //
509 // Thread-Local Log Stream Accessors
510 //
511 // These functions return thread-local LogStream instances, eliminating data races
512 // in the logging system. Each thread gets its own buffers and state.
513 // See GitHub Issue #8596 for details on the race conditions this fixes.
514 //
515 // RESTRICTIONS (document these clearly):
516 // - Log configuration (via LogConfigHandler) should be done ONCE at program start,
517 // BEFORE spawning threads. Thread-local streams copy config from globals on first access.
518 // - OpenMP thread pools reuse threads, so thread_local state persists across parallel regions.
519 // - Do not reconfigure logging while threads are actively logging.
520 //
521
524
527
530
533
536
537 //
538 // Logging Macros (thread-safe via thread-local streams)
539 //
540
542#define OPENMS_LOG_FATAL_ERROR \
543 OpenMS::getThreadLocalLogFatal() << __FILE__ << "(" << __LINE__ << "): "
544
546#define OPENMS_LOG_ERROR \
547 OpenMS::getThreadLocalLogError()
548
550#define OPENMS_LOG_WARN \
551 OpenMS::getThreadLocalLogWarn()
552
554#define OPENMS_LOG_INFO \
555 OpenMS::getThreadLocalLogInfo()
556
558#define OPENMS_LOG_DEBUG \
559 OpenMS::getThreadLocalLogDebug() << past_last_slash(__FILE__) << "(" << __LINE__ << "): "
560
562#define OPENMS_LOG_DEBUG_NOFILE \
563 OpenMS::getThreadLocalLogDebug()
564
575
590
605
620
635
653
655
656} // namespace OpenMS
Color and style the fonts shown on cout/cerr (or other streams)
Definition Colorizer.h:71
RAII guard that temporarily removes a stream from a LogStream and re-inserts it on scope exit.
Definition LogStream.h:475
LogSinkGuard(LogSinkGuard &&)=delete
LogSinkGuard(const LogSinkGuard &)=delete
LogSinkGuard & operator=(const LogSinkGuard &)=delete
LogSinkGuard(LogStream &log_stream, std::ostream &stream)
Construct a guard that removes the stream and re-inserts it on destruction.
Definition LogStream.h:483
LogSinkGuard & operator=(LogSinkGuard &&)=delete
~LogSinkGuard()
Destructor re-inserts the stream.
Definition LogStream.h:490
LogStream & log_stream_
Definition LogStream.h:502
std::ostream & stream_
Definition LogStream.h:503
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:229
std::string expandPrefix_(const std::string &prefix, time_t time) const
Interpret the prefix format string and return the expanded prefix.
LogStreamBuf(LogStreamBuf *source_buf, Colorizer *col=nullptr)
std::string level_
Definition LogStream.h:204
std::list< StreamStruct > stream_list_
Stream list for this buffer.
Definition LogStream.h:205
int counter
Definition LogStream.h:217
Size timestamp
Definition LogStream.h:216
const std::list< StreamStruct > & getStreamList_() const
std::map< std::string, LogCacheStruct > log_cache_
Cache of the last two log messages.
Definition LogStream.h:227
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:206
void distribute_(const std::string &outstring)
Distribute a new message to connected streams.
std::list< StreamStruct > & getStreamList_()
Returns the stream list (owned or shared)
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:215
Definition LogStream.h:254
virtual ~LogStreamNotifier()
Destructor.
LogStreamNotifier()
Empty constructor.
LogStream * registered_at_
Definition LogStream.h:275
void registerAt(LogStream &log_stream)
std::stringstream stream_
Definition LogStream.h:273
Log Stream Class.
Definition LogStream.h:308
bool hasStream_(std::ostream &stream)
LogStream(LogStreamBuf *buf=nullptr, bool delete_buf=true, std::ostream *stream=nullptr)
void insert(std::ostream &s)
std::list< LogStreamBuf::StreamStruct >::iterator StreamIterator
Definition LogStream.h:444
~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:454
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)
LogStreamBuf * rdbuf()
void remove(std::ostream &s)
StreamIterator findStream_(const std::ostream &stream)
void flushIncomplete()
Flush any incomplete line (text not terminated by newline) to all streams.
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Logger::LogStream & getGlobalLogDebug()
Get the global debug log stream for configuration purposes.
Logger::LogStream & getThreadLocalLogDebug()
Get thread-local debug log stream.
Logger::LogStream & getGlobalLogError()
Get the global error log stream for configuration purposes.
Logger::LogStream & getThreadLocalLogInfo()
Get thread-local info log stream.
Logger::LogStream & getThreadLocalLogWarn()
Get thread-local warning log stream.
Logger::LogStream & getGlobalLogFatal()
Get the global fatal error log stream for configuration purposes.
Logger::LogStream & getThreadLocalLogFatal()
Get thread-local fatal error log stream.
Logger::LogStream & getGlobalLogWarn()
Get the global warning log stream for configuration purposes.
Logger::LogStream & getThreadLocalLogError()
Get thread-local error log stream.
Logger::LogStream & getGlobalLogInfo()
Get the global info log stream for configuration purposes.
Holds a stream that is connected to the LogStream. It also includes the minimum and maximum level at ...
Definition LogStream.h:168
std::string prefix
Definition LogStream.h:170
StreamStruct()
Definition LogStream.h:173
LogStreamNotifier * target
Definition LogStream.h:171
~StreamStruct()
Delete the notification target.
Definition LogStream.h:179
std::ostream * stream
Definition LogStream.h:169