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
24namespace boost {
25namespace logging {
26
27typedef boost::iostreams::tee_device<std::ostream, std::ostream> RDTee;
28typedef boost::iostreams::stream<RDTee> RDTeeStream;
29
31 public:
32 std::ostream *dp_dest;
35
36 std::ofstream *dp_teeHelperStream;
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);
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);
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;
77 dp_teeHelperStream->close();
78 delete dp_teeHelperStream;
79 dp_teeHelperStream = nullptr;
80 }
81 }
82 }
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};
99RDKIT_RDGENERAL_EXPORT void enable_logs(const char *arg);
100RDKIT_RDGENERAL_EXPORT void enable_logs(const std::string &arg);
102RDKIT_RDGENERAL_EXPORT void disable_logs(const std::string &arg);
104} // namespace logging
105} // namespace boost
106namespace RDLog {
107RDKIT_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
114using RDLogger = std::shared_ptr<boost::logging::rdLogger>;
115
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 {
135
136using RDLoggerList = std::vector<RDLogger>;
137class 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
142 //! disables all logs, the current state will be restored when this object is
143 //! destroyed
146
147 private:
148 std::uint64_t d_origState = 0;
149};
150
151inline void deprecationWarning(const std::string &message) {
152 BOOST_LOG(rdWarningLog) << "DEPRECATION WARNING: " << message << std::endl;
153}
154
155} // namespace RDLog
156#endif
std::shared_ptr< boost::logging::rdLogger > RDLogger
Definition RDLog.h:114
RDKIT_RDGENERAL_EXPORT RDLogger rdDebugLog
RDKIT_RDGENERAL_EXPORT RDLogger rdStatusLog
#define BOOST_LOG(__arg__)
Definition RDLog.h:109
RDKIT_RDGENERAL_EXPORT RDLogger rdAppLog
RDKIT_RDGENERAL_EXPORT RDLogger rdInfoLog
RDKIT_RDGENERAL_EXPORT RDLogger rdWarningLog
RDKIT_RDGENERAL_EXPORT RDLogger rdErrorLog
LogStateSetter(RDLoggerList toEnable)
std::ofstream * dp_teeHelperStream
Definition RDLog.h:36
void SetTee(const char *filename)
Sets a filename to tee the output to.
Definition RDLog.h:58
rdLogger(std::ostream *dest, bool owner=false)
Definition RDLog.h:40
void ClearTee()
Remove our tee if it's set.
Definition RDLog.h:70
void SetTee(std::ostream &stream)
Sets a stream to tee the output to.
Definition RDLog.h:49
void SetTee(const std::string &filename)
Sets a filename to tee the output to.
Definition RDLog.h:67
RDTeeStream * teestream
Definition RDLog.h:38
std::ostream * dp_dest
Definition RDLog.h:32
#define RDKIT_RDGENERAL_EXPORT
Definition export.h:441
Definition RDLog.h:106
RDKIT_RDGENERAL_EXPORT std::ostream & toStream(std::ostream &)
RDKIT_RDGENERAL_EXPORT void InitLogs()
std::vector< RDLogger > RDLoggerList
Definition RDLog.h:136
void deprecationWarning(const std::string &message)
Definition RDLog.h:151
RDKIT_RDGENERAL_EXPORT std::string log_status()
RDKIT_RDGENERAL_EXPORT void enable_logs(const char *arg)
boost::iostreams::tee_device< std::ostream, std::ostream > RDTee
Definition RDLog.h:27
RDKIT_RDGENERAL_EXPORT void disable_logs(const char *arg)
boost::iostreams::stream< RDTee > RDTeeStream
Definition RDLog.h:28
Definition RDLog.h:24