forked from lballabio/quantlib-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
251 lines (208 loc) · 8.18 KB
/
Copy pathlogger.cpp
File metadata and controls
251 lines (208 loc) · 8.18 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2005, 2006, 2007 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#if defined(HAVE_CONFIG_H) // Dynamically created by configure
#include <oh/config.hpp>
#endif
#include <oh/ohdefines.hpp>
#ifdef HAVE_LOG4CXX
#include <oh/logger.hpp>
#include <oh/exception.hpp>
#include <log4cxx/helpers/exception.h>
/* Use BOOST_MSVC instead of _MSC_VER since some other vendors (Metrowerks,
for example) also #define _MSC_VER
*/
#ifdef BOOST_MSVC
# define BOOST_LIB_DIAGNOSTIC
# include <log4cxx/auto_link.hpp>
# undef BOOST_LIB_DIAGNOSTIC
#endif
#include <ostream>
#include <boost/filesystem.hpp>
#include <log4cxx/helpers/transcoder.h>
#include <iostream>
using namespace log4cxx;
namespace ObjectHandler {
Logger::Logger() {
try {
log4cxx::LoggerPtr _logger = log4cxx::Logger::getRootLogger();
//_logger->setLevel(Level::OFF);
_logger->setLevel(Level::getOff());
//_layout = LayoutPtr(new SimpleLayout());
} catch (helpers::Exception &e) {
//OH_FAIL("Logger::Logger: error initializing: " + e.getMessage());
std::string str = "Logger::Logger: error initializing: ";
str += e.what();
OH_FAIL(str);
}
}
log4cxx::LayoutPtr Logger::getLayout(){
//static log4cxx::LayoutPtr _layout = NULL;
//if(_layout == NULL){
static log4cxx::LayoutPtr _layout = 0;
if(_layout == 0){
_layout = LayoutPtr(new SimpleLayout());
}
return _layout;
}
void Logger::setFile(const std::string &logFileName,
const int &logLevel) {
// Create a boost path object from the std::string.
boost::filesystem::path path(logFileName);
// If a parent directory has been specified then ensure it exists.
if ( !path.branch_path().empty() ) {
OH_REQUIRE(boost::filesystem::exists(path.branch_path()),
"Invalid parent path : " << logFileName);
}
// deprecated branch_path() observer has been used above for boost 1.35
// backward compatibility. It should be replaced by parent_path()
try {
log4cxx::LoggerPtr _logger = log4cxx::Logger::getRootLogger();
static log4cxx::AppenderPtr _fileAppender;
_logger->removeAppender(_fileAppender);
LogString fileName;
log4cxx::helpers::Transcoder::decode(logFileName, fileName);
_fileAppender = AppenderPtr(new FileAppender(getLayout(), fileName));
_logger->addAppender(_fileAppender);
setLevel(logLevel);
filename_ = logFileName;
} catch (helpers::Exception &e) {
//OH_FAIL("Logger::logSetFile: unable to set logfile: " + e.getMessage());
std::string str = "Logger::logSetFile: unable to set logfile: ";
str += e.what();
OH_FAIL(str);
}
}
void Logger::setConsole(
const int &console,
const int &logLevel) {
try {
log4cxx::LoggerPtr _logger = log4cxx::Logger::getRootLogger();
static log4cxx::AppenderPtr _consoleAppender;
_logger->removeAppender(_consoleAppender);
if (console) {
_consoleAppender = AppenderPtr(new ConsoleAppender(getLayout()));
_logger->addAppender(_consoleAppender);
}
setLevel(logLevel);
} catch (helpers::Exception &e) {
//OH_FAIL("Logger::logSetFile: unable to set logfile: " + e.getMessage());
std::string str = "Logger::logSetFile: unable to set logfile: ";
str += e.what();
OH_FAIL(str);
}
}
void Logger::setLevel(const int &logLevel) {
try {
log4cxx::LoggerPtr _logger = log4cxx::Logger::getRootLogger();
switch (logLevel) {
case 0:
//_logger->setLevel(Level::OFF);
_logger->setLevel(Level::getOff());
break;
case 1:
//_logger->setLevel(Level::FATAL);
_logger->setLevel(Level::getFatal());
break;
case 2:
//_logger->setLevel(Level::ERROR);
_logger->setLevel(Level::getError());
break;
case 3:
//_logger->setLevel(Level::WARN);
_logger->setLevel(Level::getWarn());
break;
case 4:
//_logger->setLevel(Level::INFO);
_logger->setLevel(Level::getInfo());
break;
case 5:
//_logger->setLevel(Level::DEBUG);
_logger->setLevel(Level::getDebug());
break;
default:
OH_FAIL("Logger::setLogLevel: invalid logLevel: " << logLevel);
}
} catch (helpers::Exception &e) {
//OH_FAIL("Logger::Logger: error initializing: " + e.getMessage());
std::string str = "Logger::Logger: error initializing: ";
str += e.what();
OH_FAIL(str);
}
}
void Logger::writeMessage(const std::string &message,
const int &level) {
// client applications call this function from within their
// catch() clauses so this function must not throw.
try {
log4cxx::LoggerPtr _logger = log4cxx::Logger::getRootLogger();
switch (level) {
case 1:
LOG4CXX_FATAL(_logger, message);
break;
case 2:
LOG4CXX_ERROR(_logger, message);
break;
case 3:
LOG4CXX_WARN(_logger, message);
break;
case 4:
LOG4CXX_INFO(_logger, message);
break;
case 5:
LOG4CXX_DEBUG(_logger, message);
break;
}
} catch (...) {}
}
//get log file
const std::string Logger::file() const {
try{
/*
if(fileAppender_ == 0)
throw "";
std::string filename;
log4cxx::helpers::Transcoder::encode(fileAppender_->getName(), filename);
return filename;
*/
return filename_;
}
catch(...){
OH_FAIL("Logger::LogFile: unable to get logfile.");
}
}
//get log level
const int Logger::level() const {
const LevelPtr& level = log4cxx::Logger::getRootLogger()->getLevel();
int value = -1;
if(level == Level::getOff())
value = 0;
else if(level == Level::getFatal())
value = 1;
else if(level == Level::getError())
value = 2;
else if(level == Level::getWarn())
value = 3;
else if(level == Level::getInfo())
value = 4;
else if(level == Level::getDebug())
value = 5;
else if(level == Level::getTrace())
value = 6;
else if(level == Level::getAll())
value = 7;
return value ;
}
}
#endif