MeVisLab Toolbox Reference
SeverityChannelLogging.h
Go to the documentation of this file.
1// Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
2// **InsertLicense** code
3
4#pragma once
5
6#ifdef _MSC_VER
7#pragma warning(disable: 4714) // not inlined despite forceinline (logger)
8#endif
9
11
12#include <FMEThirdPartyWarningsDisable.h>
13#include <string>
14#include <boost/log/common.hpp>
15#include <boost/log/expressions.hpp>
16#include <boost/log/keywords/format.hpp>
17#include <boost/log/keywords/filter.hpp>
18#include <boost/log/keywords/severity.hpp>
19#include <boost/log/keywords/channel.hpp>
20#include <boost/log/utility/setup/console.hpp>
21#include <boost/log/utility/setup/file.hpp>
22#include <boost/log/utility/setup/common_attributes.hpp>
23#include <boost/log/utility/setup/filter_parser.hpp>
24#include <boost/log/utility/setup/formatter_parser.hpp>
25#include <boost/log/utility/exception_handler.hpp>
26#include <FMEThirdPartyWarningsRestore.h>
27
28
29namespace scl {
30
38
39 template<typename CharT, typename TraitsT>
40 std::basic_ostream<CharT, TraitsT>& operator<<(std::basic_ostream<CharT, TraitsT>& stream, SeverityLevel level)
41 {
42 static const char* const str[] =
43 {
44 "Debug",
45 "Info",
46 "Warning",
47 "Error",
48 "Critical"
49 };
50
51 if (level < (sizeof(str) / sizeof(*str))) {
52 stream << str[level];
53 }
54 else {
55 stream << "Unknown";
56 }
57
58 return stream;
59 }
60
62 BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string);
63
64 typedef boost::log::sources::severity_channel_logger<SeverityLevel> Logger;
65 typedef boost::log::sources::severity_channel_logger_mt<SeverityLevel> ThreadSafeLogger;
66
67 typedef boost::log::sinks::sink Sink;
68 typedef boost::shared_ptr<Sink> SinkPtr;
69
70 namespace keywords = boost::log::keywords;
71
72 using boost::log::add_console_log;
73 using boost::log::add_file_log;
74 using boost::log::parse_formatter;
75 using boost::log::parse_filter;
76 using boost::log::formatter;
77 using boost::log::filter;
78
79 template<typename SinkType>
80 void remove_log(boost::shared_ptr<SinkType> sink)
81 {
82 auto core = boost::log::core::get();
83 core->remove_sink(sink);
84 }
85
87}
88
89#define LOG_WITH_SEVERITY BOOST_LOG_SEV
90#define LOG_DEBUG(logger) LOG_WITH_SEVERITY(logger, ::scl::Debug)
91#define LOG_INFO(logger) LOG_WITH_SEVERITY(logger, ::scl::Info)
92#define LOG_WARNING(logger) LOG_WITH_SEVERITY(logger, ::scl::Warning)
93#define LOG_ERROR(logger) LOG_WITH_SEVERITY(logger, ::scl::Error)
94#define LOG_CRITICAL(logger) LOG_WITH_SEVERITY(logger, ::scl::Critical)
95
96
97namespace scl {
98
99 template<typename LoggerType, SeverityLevel level>
101 {
102 public:
103 template<typename ConstructionMsgType, typename DestructionMsgType>
106 : logger(&logger_), destructionMsg(destructionMsg_)
107 {
108 LOG_WITH_SEVERITY(*logger, level) << creationMsg;
109 }
110
112 {
113 LOG_WITH_SEVERITY(*logger, level) << destructionMsg;
114 }
115
116 private:
117 LoggerType* logger;
118 std::string destructionMsg;
119 };
120
121}
#define SEVERITYCHANNELLOGGING_EXPORT
#define LOG_WITH_SEVERITY
ScopedLog(LoggerType &logger_, const ConstructionMsgType &creationMsg, const DestructionMsgType &destructionMsg_)
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
MLCSO_EXPORT void level(CSO *cso)
Determines the normal and the center of gravity of the given CSO.
boost::shared_ptr< Sink > SinkPtr
boost::log::sinks::sink Sink
SEVERITYCHANNELLOGGING_EXPORT void initialize()
boost::log::sources::severity_channel_logger_mt< SeverityLevel > ThreadSafeLogger
boost::log::sources::severity_channel_logger< SeverityLevel > Logger
std::basic_ostream< CharT, TraitsT > & operator<<(std::basic_ostream< CharT, TraitsT > &stream, SeverityLevel level)
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", SeverityLevel)
void remove_log(boost::shared_ptr< SinkType > sink)