RDKit
Open-source cheminformatics and machine learning.
RDLog.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005-2008 Greg Landrum and Rational Discovery LLC
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 namespace boost {
23 namespace logging {
24 
25 typedef boost::iostreams::tee_device<std::ostream, std::ostream> RDTee;
26 typedef boost::iostreams::stream<RDTee> RDTeeStream;
27 
29  public:
30  std::ostream *dp_dest;
31  bool df_owner, df_enabled;
32 
35 
36  rdLogger(std::ostream *dest, bool owner = false)
37  : dp_dest(dest),
38  df_owner(owner),
39  df_enabled(true),
40  tee(nullptr),
41  teestream(nullptr){};
42 
43  //! Sets a stream to tee the output to.
44  void SetTee(std::ostream &stream) {
45  if (dp_dest) {
46  delete teestream;
47  delete tee;
48  tee = new RDTee(*dp_dest, stream);
49  teestream = new RDTeeStream(*tee);
50  }
51  }
52  //! Remove our tee if it's set.
53  void ClearTee() {
54  if (dp_dest) {
55  delete teestream;
56  delete tee;
57  tee = nullptr;
58  teestream = nullptr;
59  }
60  }
62  if (dp_dest) {
63  dp_dest->flush();
64  if (df_owner) {
65  delete dp_dest;
66  }
67  dp_dest = nullptr;
68  }
69  delete teestream;
70  teestream = nullptr;
71  delete tee;
72  tee = nullptr;
73  }
74 
75  private:
76  // disable copy ctor and assignment
77  rdLogger(const rdLogger &);
78  rdLogger &operator=(const rdLogger &);
79 };
80 RDKIT_RDGENERAL_EXPORT void enable_logs(const char *arg);
81 RDKIT_RDGENERAL_EXPORT void enable_logs(const std::string &arg);
82 RDKIT_RDGENERAL_EXPORT void disable_logs(const char *arg);
83 RDKIT_RDGENERAL_EXPORT void disable_logs(const std::string &arg);
85 } // namespace logging
86 } // namespace boost
87 namespace RDLog {
88 RDKIT_RDGENERAL_EXPORT std::ostream &toStream(std::ostream &);
89 }
90 #define BOOST_LOG(__arg__) \
91  if ((__arg__) && (__arg__->dp_dest) && (__arg__->df_enabled)) \
92  RDLog::toStream((__arg__->teestream) ? *(__arg__->teestream) \
93  : *(__arg__->dp_dest))
94 
95 using RDLogger = std::shared_ptr<boost::logging::rdLogger>;
96 
103 
104 #else
105 #define BOOST_LOG_NO_LIB
106 #include <boost/log/log.hpp>
107 BOOST_DECLARE_LOG(rdAppLog)
108 BOOST_DECLARE_LOG(rdDebugLog)
109 BOOST_DECLARE_LOG(rdInfoLog)
110 BOOST_DECLARE_LOG(rdErrorLog)
111 BOOST_DECLARE_LOG(rdWarningLog)
112 BOOST_DECLARE_LOG(rdStatusLog)
113 #endif
114 namespace RDLog {
116 
117 // ! Temporarily block logging until this object goes out of scope
119  std::vector<RDLogger> logs_to_reenable;
122 };
123 }
124 #endif
std::shared_ptr< boost::logging::rdLogger > RDLogger
Definition: RDLog.h:95
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
rdLogger(std::ostream *dest, bool owner=false)
Definition: RDLog.h:36
void ClearTee()
Remove our tee if it's set.
Definition: RDLog.h:53
void SetTee(std::ostream &stream)
Sets a stream to tee the output to.
Definition: RDLog.h:44
RDTeeStream * teestream
Definition: RDLog.h:34
std::ostream * dp_dest
Definition: RDLog.h:30
#define RDKIT_RDGENERAL_EXPORT
Definition: export.h:567
Definition: RDLog.h:87
RDKIT_RDGENERAL_EXPORT void InitLogs()
RDKIT_RDGENERAL_EXPORT std::ostream & toStream(std::ostream &)
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:25
RDKIT_RDGENERAL_EXPORT void disable_logs(const char *arg)
boost::iostreams::stream< RDTee > RDTeeStream
Definition: RDLog.h:26
Definition: RDLog.h:22
std::vector< RDLogger > logs_to_reenable
Definition: RDLog.h:119