blob: 6451a32126dc3dc79c2f145aed12007c843ace52 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque674b0b12014-05-20 14:33:28 -050023#include <log4cxx/logger.h>
24#include <log4cxx/basicconfigurator.h>
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050025#include <log4cxx/xml/domconfigurator.h>
26#include <log4cxx/propertyconfigurator.h>
akmhoque674b0b12014-05-20 14:33:28 -050027#include <log4cxx/patternlayout.h>
28#include <log4cxx/level.h>
29#include <log4cxx/helpers/exception.h>
30#include <log4cxx/rollingfileappender.h>
31
Vince Lehmanf99b87f2014-08-26 15:54:27 -050032#include <boost/algorithm/string.hpp>
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050033#include <boost/filesystem.hpp>
Vince Lehmanf99b87f2014-08-26 15:54:27 -050034
akmhoque674b0b12014-05-20 14:33:28 -050035#include "logger.hpp"
36
37void
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050038INIT_LOG4CXX(const std::string& log4cxxConfPath)
39{
40 if (boost::filesystem::path(log4cxxConfPath).extension().string() == ".xml") {
41 log4cxx::xml::DOMConfigurator::configure(log4cxxConfPath);
42 }
43 else {
44 log4cxx::PropertyConfigurator::configure(log4cxxConfPath);
45 }
46}
47
48void
Vince Lehmanf99b87f2014-08-26 15:54:27 -050049INIT_LOGGERS(const std::string& logDir, const std::string& logLevel)
akmhoque674b0b12014-05-20 14:33:28 -050050{
51 static bool configured = false;
Vince Lehmanf99b87f2014-08-26 15:54:27 -050052
53 if (configured) {
54 return;
55 }
akmhoque674b0b12014-05-20 14:33:28 -050056
57 log4cxx::PatternLayoutPtr
Vince Lehmanf99b87f2014-08-26 15:54:27 -050058 layout(new log4cxx::PatternLayout("%date{yyyyMMddHHmmssSSS} %p: [%c] %m%n"));
59
akmhoque674b0b12014-05-20 14:33:28 -050060 log4cxx::RollingFileAppender* rollingFileAppender =
61 new log4cxx::RollingFileAppender(layout, logDir+"/nlsr.log", true);
Vince Lehmanf99b87f2014-08-26 15:54:27 -050062
akmhoque674b0b12014-05-20 14:33:28 -050063 rollingFileAppender->setMaxFileSize("10MB");
64 rollingFileAppender->setMaxBackupIndex(10);
Vince Lehmanf99b87f2014-08-26 15:54:27 -050065
akmhoque674b0b12014-05-20 14:33:28 -050066 log4cxx::helpers::Pool p;
67 rollingFileAppender->activateOptions(p);
Vince Lehmanf99b87f2014-08-26 15:54:27 -050068
akmhoque674b0b12014-05-20 14:33:28 -050069 log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(rollingFileAppender));
Vince Lehmanf99b87f2014-08-26 15:54:27 -050070
71 if (boost::iequals(logLevel, "none")) {
72 log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getOff());
73 }
74 else {
75 log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::toLevel(logLevel));
76 }
akmhoque674b0b12014-05-20 14:33:28 -050077
78 configured = true;
79}
Vince Lehmanf99b87f2014-08-26 15:54:27 -050080
81bool
82isValidLogLevel(const std::string& logLevel)
83{
84 return boost::iequals(logLevel, "all") || boost::iequals(logLevel, "trace") ||
85 boost::iequals(logLevel, "debug") || boost::iequals(logLevel, "info") ||
86 boost::iequals(logLevel, "warn") || boost::iequals(logLevel, "error") ||
87 boost::iequals(logLevel, "none");
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050088}