File: RDLog.cpp

package info (click to toggle)
rdkit 201203-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 37,840 kB
  • sloc: cpp: 93,902; python: 51,897; java: 5,192; ansic: 3,497; xml: 2,499; sql: 1,641; yacc: 1,518; lex: 1,076; makefile: 325; fortran: 183; sh: 153; cs: 51
file content (154 lines) | stat: -rw-r--r-- 4,995 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// $Id: RDLog.cpp 1528 2010-09-26 17:04:37Z glandrum $
//
// Copyright (C)  2005-2010 Greg Landrum and Rational Discovery LLC
//
//  @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include "RDLog.h"

#if 1
#include <iomanip>
#include <string>
#include <time.h>

namespace {
  // this is a "bit" of a hack to work around shared/static library problems
  // on windows
  boost::logging::rdLogger cerrLogger(&std::cerr);  
  boost::logging::rdLogger coutLogger(&std::cout);  
}

boost::logging::rdLogger *rdAppLog=0;
boost::logging::rdLogger *rdDebugLog=0;
boost::logging::rdLogger *rdInfoLog=&coutLogger;
boost::logging::rdLogger *rdErrorLog=&cerrLogger;
boost::logging::rdLogger *rdWarningLog=&cerrLogger;
boost::logging::rdLogger *rdStatusLog=0;

namespace boost {
  namespace logging {

    void enable_logs(const char *arg) { enable_logs(std::string(arg));};
    void enable_logs(const std::string &arg) {
      // Yes... this is extremely crude
      if(arg=="rdApp.debug"||arg=="rdApp.*"){
        if(rdDebugLog) rdDebugLog->df_enabled=true;
      }
      if(arg=="rdApp.info"||arg=="rdApp.*"){
        if(rdInfoLog) rdInfoLog->df_enabled=true;
      }
      if(arg=="rdApp.warning"||arg=="rdApp.*"){
        if(rdWarningLog) rdWarningLog->df_enabled=true;
      }
      if(arg=="rdApp.error"||arg=="rdApp.*"){
        if(rdErrorLog) rdErrorLog->df_enabled=true;
      }
    };
    void disable_logs(const char *arg) {disable_logs(std::string(arg));};
    void disable_logs(const std::string &arg) {
      // Yes... this is extremely crude
      if(arg=="rdApp.debug"||arg=="rdApp.*"){
        if(rdDebugLog) rdDebugLog->df_enabled=false;
      }
      if(arg=="rdApp.info"||arg=="rdApp.*"){
        if(rdInfoLog) rdInfoLog->df_enabled=false;
      }
      if(arg=="rdApp.warning"||arg=="rdApp.*"){
        if(rdWarningLog) rdWarningLog->df_enabled=false;
      }
      if(arg=="rdApp.error"||arg=="rdApp.*"){
        if(rdErrorLog) rdErrorLog->df_enabled=false;
      }
    };
  }
}


namespace RDLog {
  void InitLogs(){
    rdDebugLog=new boost::logging::rdLogger(&std::cerr);
    rdInfoLog=new boost::logging::rdLogger(&std::cout);
    rdWarningLog=new boost::logging::rdLogger(&std::cerr);
    rdErrorLog=new boost::logging::rdLogger(&std::cerr);
  }
  std::ostream &toStream(std::ostream &logstrm) {
    time_t t = time(0); 
    tm details = *localtime( &t);
    logstrm << "["<<std::setw(2)<<std::setfill('0')<<details.tm_hour<<":"<<std::setw(2)<<std::setfill('0')<<details.tm_min<<":"<<std::setw(2)<<std::setfill('0')<<int(details.tm_sec)<<"] ";
    return logstrm;
  }

}

#else
#include <boost/log/functions.hpp>
#if defined(BOOST_HAS_THREADS2)
#include <boost/log/extra/functions_ts.hpp>
#endif
#include <iostream>
namespace logging = boost::logging;

BOOST_DEFINE_LOG(rdAppLog,"rdApp")
BOOST_DEFINE_LOG(rdDebugLog,"rdApp.debug")
BOOST_DEFINE_LOG(rdInfoLog,"rdApp.info")
BOOST_DEFINE_LOG(rdErrorLog,"rdApp.error")
BOOST_DEFINE_LOG(rdWarningLog,"rdApp.warning")
BOOST_DEFINE_LOG(rdStatusLog,"rdApp.status")

namespace RDLog {
  void write_to_cout(const std::string &, const std::string &msg) {
    std::cout << msg; std::cout.flush();
  }
  void write_to_cerr(const std::string &, const std::string &msg) {
    std::cerr << msg; std::cerr.flush();
  }

  void InitLogs(){
    static bool callOnce=true;
    if(!callOnce) return;
    callOnce=false;

    // turn off caching:
    logging::set_log_caching(false);
    logging::manipulate_logs("rdApp.*")
      .add_modifier(logging::prepend_time("[$hh:$mm:$ss] "),
		    logging::DEFAULT_INDEX-10);
    logging::manipulate_logs("rdApp.info")
      .add_appender(write_to_cout,
		    logging::DEFAULT_INDEX+1);
#if defined(BOOST_HAS_THREADS2)
    logging::manipulate_logs("rdApp.error")
      .add_appender(logging::ts_appender(write_to_cerr,100),
		    logging::DEFAULT_INDEX+1);
    logging::manipulate_logs("rdApp.warning")
      .add_appender(logging::ts_appender(write_to_cerr,100),
		    logging::DEFAULT_INDEX+1);
    logging::manipulate_logs("rdApp.status")
      .add_appender(logging::ts_appender(write_to_cerr,100),
		    logging::DEFAULT_INDEX+1);
    logging::manipulate_logs("rdApp.debug")
      .add_appender(logging::ts_appender(write_to_cerr,100),
		    logging::DEFAULT_INDEX+1);
#else
    logging::manipulate_logs("rdApp.error")
      .add_appender(write_to_cerr,
		    logging::DEFAULT_INDEX+1);
    logging::manipulate_logs("rdApp.warning")
      .add_appender(write_to_cerr,
		    logging::DEFAULT_INDEX+1);
    logging::manipulate_logs("rdApp.status")
      .add_appender(write_to_cerr,
		    logging::DEFAULT_INDEX+1);
    logging::manipulate_logs("rdApp.debug")
      .add_appender(write_to_cerr,
		    logging::DEFAULT_INDEX+1);
#endif
    // start with the debug log disabled:
    logging::disable_logs("rdApp.debug");
  };
}
#endif