RDKit
Open-source cheminformatics and machine learning.
Toggle main menu visibility
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
#include "
BoostStartInclude.h
"
16
#include <boost/iostreams/tee.hpp>
17
#include <boost/iostreams/stream.hpp>
18
#include "
BoostEndInclude.h
"
19
#include <fstream>
20
#include <sstream>
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
namespace
RDLog
{
124
RDKIT_RDGENERAL_EXPORT
void
InitLogs
();
125
126
using
RDLoggerList
= std::vector<RDLogger>;
127
class
RDKIT_RDGENERAL_EXPORT
LogStateSetter
:
public
boost::noncopyable {
128
public
:
129
//! enables only the logs in the list, the current state will be restored when
130
//! this object is destroyed
131
LogStateSetter
(
RDLoggerList
toEnable);
132
//! disables all logs, the current state will be restored when this object is
133
//! destroyed
134
LogStateSetter
();
135
~LogStateSetter
();
136
137
private
:
138
std::uint64_t d_origState = 0;
139
};
140
141
//! RAII class to capture messages from a given \c RDLogger.
142
//!
143
//! The log is enabled when this object is constructed and its original enabled
144
//! state is restored when this object is destroyed. The stream destination is
145
//! also restored on destruction. Nesting is supported: inner captures shadow
146
//! outer ones.
147
//!
148
//! \b Example:
149
//! \code
150
//! RDLog::CaptureLog capture{rdErrorLog};
151
//! functionThatMayFail();
152
//! std::string errs = capture.messages();
153
//! \endcode
154
class
RDKIT_RDGENERAL_EXPORT
CaptureLog
:
public
boost::noncopyable {
155
public
:
156
explicit
CaptureLog
(
RDLogger
log);
157
~CaptureLog
();
158
159
//! Returns all messages captured since construction.
160
std::string
messages
()
const
;
161
162
private
:
163
RDLogger
d_log;
164
std::stringstream d_messages;
165
std::ostream *d_savedDest =
nullptr
;
166
boost::logging::RDTeeStream
*d_savedTeestream =
nullptr
;
167
bool
d_logWasEnabled =
true
;
168
};
169
170
//! Convenience subclass of \c CaptureLog that captures \c rdErrorLog.
171
class
RDKIT_RDGENERAL_EXPORT
CaptureErrorLog
:
public
CaptureLog
{
172
public
:
173
CaptureErrorLog
() :
CaptureLog
(
rdErrorLog
) {}
174
};
175
176
inline
void
deprecationWarning
(
const
std::string &message) {
177
BOOST_LOG
(
rdWarningLog
) <<
"DEPRECATION WARNING: "
<< message << std::endl;
178
}
179
180
}
// namespace RDLog
181
#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::CaptureErrorLog::CaptureErrorLog
CaptureErrorLog()
Definition
RDLog.h:173
RDLog::CaptureLog::~CaptureLog
~CaptureLog()
RDLog::CaptureLog::CaptureLog
CaptureLog(RDLogger log)
RDLog::CaptureLog::messages
std::string messages() const
Returns all messages captured since construction.
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:561
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:126
RDLog::deprecationWarning
void deprecationWarning(const std::string &message)
Definition
RDLog.h:176
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
ShapeInput.h:39
RDGeneral
RDLog.h
Generated on
for RDKit by
1.17.0