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 <fstream>
22 #include <vector>
23 #include <cstdint>
24 
25 namespace boost {
26 namespace logging {
27 
28 typedef boost::iostreams::tee_device<std::ostream, std::ostream> RDTee;
29 typedef boost::iostreams::stream<RDTee> RDTeeStream;
30 
32  public:
33  std::ostream *dp_dest;
34  bool df_owner;
35  bool df_enabled;
36 
37  std::ofstream *dp_teeHelperStream;
40 
41  rdLogger(std::ostream *dest, bool owner = false)
42  : dp_dest(dest),
43  df_owner(owner),
44  df_enabled(true),
45  dp_teeHelperStream(nullptr),
46  tee(nullptr),
47  teestream(nullptr) {}
48 
49  //! Sets a stream to tee the output to.
50  void SetTee(std::ostream &stream) {
51  if (dp_dest) {
52  ClearTee();
53  tee = new RDTee(*dp_dest, stream);
54  teestream = new RDTeeStream(*tee);
55  }
56  }
57 
58  //! Sets a filename to tee the output to.
59  void SetTee(const char *filename) {
60  if (dp_dest) {
61  auto s = new std::ofstream(filename);
62  SetTee(*s);
63  dp_teeHelperStream = s;
64  }
65  }
66 
67  //! Sets a filename to tee the output to.
68  void SetTee(const std::string &filename) { return SetTee(filename.c_str()); }
69 
70  //! Remove our tee if it's set.
71  void ClearTee() {
72  if (dp_dest) {
73  delete teestream;
74  delete tee;
75  tee = nullptr;
76  teestream = nullptr;
77  if (dp_teeHelperStream) {
78  dp_teeHelperStream->close();
79  delete dp_teeHelperStream;
80  dp_teeHelperStream = nullptr;
81  }
82  }
83  }
85  if (dp_dest) {
86  dp_dest->flush();
87  ClearTee();
88  if (df_owner) {
89  delete dp_dest;
90  }
91  dp_dest = nullptr;
92  }
93  }
94 
95  private:
96  // disable copy ctor and assignment
97  rdLogger(const rdLogger &);
98  rdLogger &operator=(const rdLogger &);
99 };
100 RDKIT_RDGENERAL_EXPORT void enable_logs(const char *arg);
101 RDKIT_RDGENERAL_EXPORT void enable_logs(const std::string &arg);
102 RDKIT_RDGENERAL_EXPORT void disable_logs(const char *arg);
103 RDKIT_RDGENERAL_EXPORT void disable_logs(const std::string &arg);
105 } // namespace logging
106 } // namespace boost
107 namespace RDLog {
108 RDKIT_RDGENERAL_EXPORT std::ostream &toStream(std::ostream &);
109 }
110 #define BOOST_LOG(__arg__) \
111  if ((__arg__) && (__arg__->dp_dest) && (__arg__->df_enabled)) \
112  RDLog::toStream((__arg__->teestream) ? *(__arg__->teestream) \
113  : *(__arg__->dp_dest))
114 
115 using RDLogger = std::shared_ptr<boost::logging::rdLogger>;
116 
123 
124 #else
125 #define BOOST_LOG_NO_LIB
126 #include <boost/log/log.hpp>
127 BOOST_DECLARE_LOG(rdAppLog)
128 BOOST_DECLARE_LOG(rdDebugLog)
129 BOOST_DECLARE_LOG(rdInfoLog)
130 BOOST_DECLARE_LOG(rdErrorLog)
131 BOOST_DECLARE_LOG(rdWarningLog)
132 BOOST_DECLARE_LOG(rdStatusLog)
133 #endif
134 namespace RDLog {
136 
137 using RDLoggerList = std::vector<RDLogger>;
138 class RDKIT_RDGENERAL_EXPORT LogStateSetter : public boost::noncopyable {
139  public:
140  //! enables only the logs in the list, the current state will be restored when
141  //! this object is destroyed
143  //! disables all logs, the current state will be restored when this object is
144  //! destroyed
147 
148  private:
149  std::uint64_t d_origState = 0;
150 };
151 
152 } // namespace RDLog
153 #endif
std::shared_ptr< boost::logging::rdLogger > RDLogger
Definition: RDLog.h:115
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)
std::ofstream * dp_teeHelperStream
Definition: RDLog.h:37
void SetTee(const char *filename)
Sets a filename to tee the output to.
Definition: RDLog.h:59
rdLogger(std::ostream *dest, bool owner=false)
Definition: RDLog.h:41
void ClearTee()
Remove our tee if it's set.
Definition: RDLog.h:71
void SetTee(std::ostream &stream)
Sets a stream to tee the output to.
Definition: RDLog.h:50
void SetTee(const std::string &filename)
Sets a filename to tee the output to.
Definition: RDLog.h:68
RDTeeStream * teestream
Definition: RDLog.h:39
std::ostream * dp_dest
Definition: RDLog.h:33
#define RDKIT_RDGENERAL_EXPORT
Definition: export.h:369
Definition: RDLog.h:107
RDKIT_RDGENERAL_EXPORT void InitLogs()
RDKIT_RDGENERAL_EXPORT std::ostream & toStream(std::ostream &)
std::vector< RDLogger > RDLoggerList
Definition: RDLog.h:137
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:28
RDKIT_RDGENERAL_EXPORT void disable_logs(const char *arg)
boost::iostreams::stream< RDTee > RDTeeStream
Definition: RDLog.h:29
Definition: RDLog.h:25