Kernel Quantum Probability Library
The KQP library aims at providing tools for working with quantums probabilities
logging.hpp
1 /*
2  This file is part of the Kernel Quantum Probability library (KQP).
3 
4  KQP is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  KQP is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with KQP. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __KQP_LOGGING_H__
19 #define __KQP_LOGGING_H__
20 
21 #ifndef NOLOGGING
22 
23 #include <map>
24 
25 #include <kqp/kqp.hpp>
26 #include "log4cxx/logger.h"
27 #include "log4cxx/basicconfigurator.h"
28 #include "log4cxx/consoleappender.h"
29 #include "log4cxx/patternlayout.h"
30 #include "log4cxx/propertyconfigurator.h"
31 #include "log4cxx/helpers/exception.h"
32 
33 namespace kqp {
34  inline log4cxx::LoggerPtr &main_logger() {
35  static log4cxx::LoggerPtr MAIN_LOGGER = log4cxx::Logger::getLogger("kqp") ;
36  return MAIN_LOGGER;
37  };
38 
39  class Logger {
40  protected:
41  static bool& initialized() {
42  static bool INITIALIZED = false;
43  return INITIALIZED;
44  }
45 
46  public:
48  static void setLevel(const std::string &loggerId, const std::string &level) {
49  // By default, use info
50  log4cxx::Logger::getLogger(loggerId)->setLevel(log4cxx::Level::toLevel(level, log4cxx::Level::getInfo()));
51  }
52 
54  static void setDefaultLevel(const std::string &level) {
55  log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::toLevel(level, log4cxx::Level::getInfo()));
56  }
57 
59  static void prepare() {
60  if (initialized()) return;
61 
62  log4cxx::PatternLayoutPtr layout = new log4cxx::PatternLayout("%5p [%r] (%F:%L) - %m%n");
63 
64  log4cxx::ConsoleAppenderPtr appender = new log4cxx::ConsoleAppender(layout, log4cxx::ConsoleAppender::getSystemErr());
65  appender->setName("kqp-appender");
66  log4cxx::BasicConfigurator::configure(appender);
67  log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getInfo());
68  initialized() = true;
69 
70  KQP_LOG_DEBUG(kqp::main_logger(), "Initialised the logging system");
71  }
72 
73  };
74 
75 
76 }
77 
78 #endif
79 #endif