RDKit: RDLog.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
RDLog.h
Go to the documentation of this file.
1//
2// Copyright (C) 2005-2022 Greg Landrum and other RDKit contributors
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10
11#include <RDGeneral/export.h>
12#ifndef RDLOG_H_29JUNE2005
13#define RDLOG_H_29JUNE2005
14
15#if 1
16#include "BoostStartInclude.h"
17#include <boost/iostreams/tee.hpp>
18#include <boost/iostreams/stream.hpp>
19#include "BoostEndInclude.h"
20#include <fstream>
21#include <vector>
22#include <cstdint>
23
24 namespace boost {
25 namespace logging {
26
27 typedef boost::iostreams::tee_device<std::ostream, std::ostream> RDTee;
28 typedef boost::iostreams::stream<RDTee> RDTeeStream;
29
30 class RDKIT_RDGENERAL_EXPORT rdLogger {
31 public:
32 std::ostream *dp_dest;
33 bool df_owner;
34 bool df_enabled;
35
36 std::ofstream *dp_teeHelperStream;
37 RDTee *tee;
38 RDTeeStream *teestream;
39
40 rdLogger(std::ostream *dest, bool owner = false)
41 : dp_dest(dest),
42 df_owner(owner),
43 df_enabled(true),
44 dp_teeHelperStream(nullptr),
45 tee(nullptr),
46 teestream(nullptr) {}
47
48 //! Sets a stream to tee the output to.
49 void SetTee(std::ostream &stream) {
50 if (dp_dest) {
51 ClearTee();
52 tee = new RDTee(*dp_dest, stream);
53 teestream = new RDTeeStream(*tee);
54 }
55 }
56
57 //! Sets a filename to tee the output to.
58 void SetTee(const char *filename) {
59 if (dp_dest) {
60 auto s = new std::ofstream(filename);
61 SetTee(*s);
62 dp_teeHelperStream = s;
63 }
64 }
65
66 //! Sets a filename to tee the output to.
67 void SetTee(const std::string &filename) { return SetTee(filename.c_str()); }
68
69 //! Remove our tee if it's set.
70 void ClearTee() {
71 if (dp_dest) {
72 delete teestream;
73 delete tee;
74 tee = nullptr;
75 teestream = nullptr;
76 if (dp_teeHelperStream) {
77 dp_teeHelperStream->close();
78 delete dp_teeHelperStream;
79 dp_teeHelperStream = nullptr;
80 }
81 }
82 }
83 ~rdLogger() {
84 if (dp_dest) {
85 dp_dest->flush();
86 ClearTee();
87 if (df_owner) {
88 delete dp_dest;
89 }
90 dp_dest = nullptr;
91 }
92 }
93
94 private:
95 // disable copy ctor and assignment
96 rdLogger(const rdLogger &);
97 rdLogger &operator=(const rdLogger &);
98};
99 RDKIT_RDGENERAL_EXPORT void enable_logs(const char *arg);
100 RDKIT_RDGENERAL_EXPORT void enable_logs(const std::string &arg);
101 RDKIT_RDGENERAL_EXPORT void disable_logs(const char *arg);
102 RDKIT_RDGENERAL_EXPORT void disable_logs(const std::string &arg);
103 RDKIT_RDGENERAL_EXPORT std::string log_status();
104} // namespace logging
105} // namespace boost
106 namespace RDLog {
107 RDKIT_RDGENERAL_EXPORT std::ostream &toStream(std::ostream &);
108}
109#define BOOST_LOG(__arg__) \
110 if ((__arg__) && (__arg__->dp_dest) && (__arg__->df_enabled)) \
111 RDLog::toStream((__arg__->teestream) ? *(__arg__->teestream) \
112 : *(__arg__->dp_dest))
113
114 using RDLogger = std::shared_ptr<boost::logging::rdLogger>;
115
116 RDKIT_RDGENERAL_EXPORT extern RDLogger rdAppLog;
117 RDKIT_RDGENERAL_EXPORT extern RDLogger rdDebugLog;
118 RDKIT_RDGENERAL_EXPORT extern RDLogger rdInfoLog;
119 RDKIT_RDGENERAL_EXPORT extern RDLogger rdErrorLog;
120 RDKIT_RDGENERAL_EXPORT extern RDLogger rdWarningLog;
121 RDKIT_RDGENERAL_EXPORT extern RDLogger rdStatusLog;
122
123#else
124#define BOOST_LOG_NO_LIB
125#include <boost/log/log.hpp>
126BOOST_DECLARE_LOG(rdAppLog)
127BOOST_DECLARE_LOG(rdDebugLog)
128BOOST_DECLARE_LOG(rdInfoLog)
129BOOST_DECLARE_LOG(rdErrorLog)
130BOOST_DECLARE_LOG(rdWarningLog)
131BOOST_DECLARE_LOG(rdStatusLog)
132#endif
133namespace RDLog {
134 RDKIT_RDGENERAL_EXPORT void InitLogs();
135
136 using RDLoggerList = std::vector<RDLogger>;
137 class RDKIT_RDGENERAL_EXPORT LogStateSetter : public boost::noncopyable {
138 public:
139 //! enables only the logs in the list, the current state will be restored when
140 //! this object is destroyed
141 LogStateSetter(RDLoggerList toEnable);
142 //! disables all logs, the current state will be restored when this object is
143 //! destroyed
144 LogStateSetter();
145 ~LogStateSetter();
146
147 private:
148 std::uint64_t d_origState = 0;
149};
150
151 inline void deprecationWarning(const std::string &message) {
152 BOOST_LOG(rdWarningLog) << "DEPRECATION WARNING: " << message << std::endl;
153}
154
155} // namespace RDLog
156#endif
BoostEndInclude.h
BoostStartInclude.h
RDLogger
std::shared_ptr< boost::logging::rdLogger > RDLogger
Definition RDLog.h:114
rdDebugLog
RDKIT_RDGENERAL_EXPORT RDLogger rdDebugLog
rdStatusLog
RDKIT_RDGENERAL_EXPORT RDLogger rdStatusLog
BOOST_LOG
#define BOOST_LOG(__arg__)
Definition RDLog.h:109
rdAppLog
RDKIT_RDGENERAL_EXPORT RDLogger rdAppLog
rdInfoLog
RDKIT_RDGENERAL_EXPORT RDLogger rdInfoLog
rdWarningLog
RDKIT_RDGENERAL_EXPORT RDLogger rdWarningLog
rdErrorLog
RDKIT_RDGENERAL_EXPORT RDLogger rdErrorLog
RDLog::LogStateSetter::LogStateSetter
LogStateSetter(RDLoggerList toEnable)
RDLog::LogStateSetter::LogStateSetter
LogStateSetter()
RDLog::LogStateSetter::~LogStateSetter
~LogStateSetter()
boost::logging::rdLogger
Definition RDLog.h:30
boost::logging::rdLogger::dp_teeHelperStream
std::ofstream * dp_teeHelperStream
Definition RDLog.h:36
boost::logging::rdLogger::tee
RDTee * tee
Definition RDLog.h:37
boost::logging::rdLogger::SetTee
void SetTee(const char *filename)
Sets a filename to tee the output to.
Definition RDLog.h:58
boost::logging::rdLogger::~rdLogger
~rdLogger()
Definition RDLog.h:83
boost::logging::rdLogger::rdLogger
rdLogger(std::ostream *dest, bool owner=false)
Definition RDLog.h:40
boost::logging::rdLogger::df_owner
bool df_owner
Definition RDLog.h:33
boost::logging::rdLogger::ClearTee
void ClearTee()
Remove our tee if it's set.
Definition RDLog.h:70
boost::logging::rdLogger::df_enabled
bool df_enabled
Definition RDLog.h:34
boost::logging::rdLogger::SetTee
void SetTee(std::ostream &stream)
Sets a stream to tee the output to.
Definition RDLog.h:49
boost::logging::rdLogger::SetTee
void SetTee(const std::string &filename)
Sets a filename to tee the output to.
Definition RDLog.h:67
boost::logging::rdLogger::teestream
RDTeeStream * teestream
Definition RDLog.h:38
boost::logging::rdLogger::dp_dest
std::ostream * dp_dest
Definition RDLog.h:32
export.h
RDKIT_RDGENERAL_EXPORT
#define RDKIT_RDGENERAL_EXPORT
Definition export.h:441
RDLog
Definition RDLog.h:106
RDLog::toStream
RDKIT_RDGENERAL_EXPORT std::ostream & toStream(std::ostream &)
RDLog::InitLogs
RDKIT_RDGENERAL_EXPORT void InitLogs()
RDLog::RDLoggerList
std::vector< RDLogger > RDLoggerList
Definition RDLog.h:136
RDLog::deprecationWarning
void deprecationWarning(const std::string &message)
Definition RDLog.h:151
boost::logging
Definition RDLog.h:25
boost::logging::log_status
RDKIT_RDGENERAL_EXPORT std::string log_status()
boost::logging::enable_logs
RDKIT_RDGENERAL_EXPORT void enable_logs(const char *arg)
boost::logging::RDTee
boost::iostreams::tee_device< std::ostream, std::ostream > RDTee
Definition RDLog.h:27
boost::logging::disable_logs
RDKIT_RDGENERAL_EXPORT void disable_logs(const char *arg)
boost::logging::RDTeeStream
boost::iostreams::stream< RDTee > RDTeeStream
Definition RDLog.h:28
boost
Definition RDLog.h:24