blob: c51cd93c90beb573cd031b0892ce78b1bf13a1ab [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
Nick Gordond0a7df32017-05-30 16:44:34 -050020
21#include "logger.hpp"
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 -050035void
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050036INIT_LOG4CXX(const std::string& log4cxxConfPath)
37{
38 if (boost::filesystem::path(log4cxxConfPath).extension().string() == ".xml") {
39 log4cxx::xml::DOMConfigurator::configure(log4cxxConfPath);
40 }
41 else {
42 log4cxx::PropertyConfigurator::configure(log4cxxConfPath);
43 }
44}
45
46void
Vince Lehmanf99b87f2014-08-26 15:54:27 -050047INIT_LOGGERS(const std::string& logDir, const std::string& logLevel)
akmhoque674b0b12014-05-20 14:33:28 -050048{
49 static bool configured = false;
Vince Lehmanf99b87f2014-08-26 15:54:27 -050050
51 if (configured) {
52 return;
53 }
akmhoque674b0b12014-05-20 14:33:28 -050054
55 log4cxx::PatternLayoutPtr
Ashlesh Gawandef6bfb142017-07-19 15:29:23 -050056 layout(new log4cxx::PatternLayout("%date{%s}.%date{SSS} %p: [%c] %m%n"));
Vince Lehmanf99b87f2014-08-26 15:54:27 -050057
akmhoque674b0b12014-05-20 14:33:28 -050058 log4cxx::RollingFileAppender* rollingFileAppender =
59 new log4cxx::RollingFileAppender(layout, logDir+"/nlsr.log", true);
Vince Lehmanf99b87f2014-08-26 15:54:27 -050060
akmhoque674b0b12014-05-20 14:33:28 -050061 rollingFileAppender->setMaxFileSize("10MB");
62 rollingFileAppender->setMaxBackupIndex(10);
Vince Lehmanf99b87f2014-08-26 15:54:27 -050063
akmhoque674b0b12014-05-20 14:33:28 -050064 log4cxx::helpers::Pool p;
65 rollingFileAppender->activateOptions(p);
Vince Lehmanf99b87f2014-08-26 15:54:27 -050066
akmhoque674b0b12014-05-20 14:33:28 -050067 log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(rollingFileAppender));
Vince Lehmanf99b87f2014-08-26 15:54:27 -050068
69 if (boost::iequals(logLevel, "none")) {
70 log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getOff());
71 }
72 else {
73 log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::toLevel(logLevel));
74 }
akmhoque674b0b12014-05-20 14:33:28 -050075
76 configured = true;
77}
Vince Lehmanf99b87f2014-08-26 15:54:27 -050078
79bool
80isValidLogLevel(const std::string& logLevel)
81{
82 return boost::iequals(logLevel, "all") || boost::iequals(logLevel, "trace") ||
83 boost::iequals(logLevel, "debug") || boost::iequals(logLevel, "info") ||
84 boost::iequals(logLevel, "warn") || boost::iequals(logLevel, "error") ||
85 boost::iequals(logLevel, "none");
Ashlesh Gawandef6bfb142017-07-19 15:29:23 -050086}