RDKit
Open-source cheminformatics and machine learning.
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 <iostream>
21 #include <vector>
22 
23 #include <vector>
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 
31  public:
32  std::ostream *dp_dest;
33  bool df_owner, df_enabled;
34 
37 
38  rdLogger(std::ostream *dest, bool owner = false)
39  : dp_dest(dest),
40  df_owner(owner),
41  df_enabled(true),
42  tee(nullptr),
43  teestream(nullptr) {}
44 
45  //! Sets a stream to tee the output to.
46  void SetTee(std::ostream &stream) {
47  if (dp_dest) {
48  delete teestream;
49  delete tee;
50  tee = new RDTee(*dp_dest, stream);
51  teestream = new RDTeeStream(*tee);
52  }
53  }
54  //! Remove our tee if it's set.
55  void ClearTee() {
56  if (dp_dest) {
57  delete teestream;
58  delete tee;
59  tee = nullptr;
60  teestream = nullptr;
61  }
62  }
64  if (dp_dest) {
65  dp_dest->flush();
66  if (df_owner) {
67  delete dp_dest;
68  }
69  dp_dest = nullptr;
70  }
71  delete teestream;
72  teestream = nullptr;
73  delete tee;
74  tee = nullptr;
75  }
76 
77  private:
78  // disable copy ctor and assignment
79  rdLogger(const rdLogger &);
80  rdLogger &operator=(const rdLogger &);
81 };
82 RDKIT_RDGENERAL_EXPORT void enable_logs(const char *arg);
83 RDKIT_RDGENERAL_EXPORT void enable_logs(const std::string &arg);
84 RDKIT_RDGENERAL_EXPORT void disable_logs(const char *arg);
85 RDKIT_RDGENERAL_EXPORT void disable_logs(const std::string &arg);
87 } // namespace logging
88 } // namespace boost
89 namespace RDLog {
90 RDKIT_RDGENERAL_EXPORT std::ostream &toStream(std::ostream &);
91 }
92 #define BOOST_LOG(__arg__) \
93  if ((__arg__) && (__arg__->dp_dest) && (__arg__->df_enabled)) \
94  RDLog::toStream((__arg__->teestream) ? *(__arg__->teestream) \
95  : *(__arg__->dp_dest))
96 
97 using RDLogger = std::shared_ptr<boost::logging::rdLogger>;
98 
105 
106 #else
107 #define BOOST_LOG_NO_LIB
108 #include <boost/log/log.hpp>
109 BOOST_DECLARE_LOG(rdAppLog)
110 BOOST_DECLARE_LOG(rdDebugLog)
111 BOOST_DECLARE_LOG(rdInfoLog)
112 BOOST_DECLARE_LOG(rdErrorLog)
113 BOOST_DECLARE_LOG(rdWarningLog)
114 BOOST_DECLARE_LOG(rdStatusLog)
115 #endif
116 namespace RDLog {
118 
119 using RDLoggerList = std::vector<RDLogger>;
120 class RDKIT_RDGENERAL_EXPORT LogStateSetter : public boost::noncopyable {
121  public:
122  //! enables only the logs in the list, the current state will be restored when
123  //! this object is destroyed
125  //! disables all logs, the current state will be restored when this object is
126  //! destroyed
129 
130  private:
131  std::uint64_t d_origState = 0;
132 };
133 
134 } // namespace RDLog
135 #endif
std::shared_ptr< boost::logging::rdLogger > RDLogger
Definition: RDLog.h:97
RDKIT_RDGENERAL_EXPORT RDLogger rdDebugLog
RDKIT_RDGENERAL_EXPORT RDLogger rdStatusLog
RDKIT_RDGENERAL_EXPORT RDLogger rdAppLog
RDKIT_RDGENERAL_EXPORT RDLogger rdInfoLog
RDKIT_RDGENERAL_EXPORT RDLogger rdWarningLog
RDKIT_RDGENERAL_EXPORT RDLogger rdErrorLog
LogStateSetter(RDLoggerList toEnable)
rdLogger(std::ostream *dest, bool owner=false)
Definition: RDLog.h:38
void ClearTee()
Remove our tee if it's set.
Definition: RDLog.h:55
void SetTee(std::ostream &stream)
Sets a stream to tee the output to.
Definition: RDLog.h:46
RDTeeStream * teestream
Definition: RDLog.h:36
std::ostream * dp_dest
Definition: RDLog.h:32
#define RDKIT_RDGENERAL_EXPORT
Definition: export.h:369
Definition: RDLog.h:89
RDKIT_RDGENERAL_EXPORT void InitLogs()
RDKIT_RDGENERAL_EXPORT std::ostream & toStream(std::ostream &)
std::vector< RDLogger > RDLoggerList
Definition: RDLog.h:119
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