akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, The University of Memphis, |
| 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 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 | **/ |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 23 | #include <log4cxx/logger.h> |
| 24 | #include <log4cxx/basicconfigurator.h> |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 25 | #include <log4cxx/xml/domconfigurator.h> |
| 26 | #include <log4cxx/propertyconfigurator.h> |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 27 | #include <log4cxx/patternlayout.h> |
| 28 | #include <log4cxx/level.h> |
| 29 | #include <log4cxx/helpers/exception.h> |
| 30 | #include <log4cxx/rollingfileappender.h> |
| 31 | |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 32 | #include <boost/algorithm/string.hpp> |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 33 | #include <boost/filesystem.hpp> |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 34 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 35 | #include "logger.hpp" |
| 36 | |
| 37 | void |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 38 | INIT_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 | |
| 48 | void |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 49 | INIT_LOGGERS(const std::string& logDir, const std::string& logLevel) |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 50 | { |
| 51 | static bool configured = false; |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 52 | |
| 53 | if (configured) { |
| 54 | return; |
| 55 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 56 | |
| 57 | log4cxx::PatternLayoutPtr |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 58 | layout(new log4cxx::PatternLayout("%date{yyyyMMddHHmmssSSS} %p: [%c] %m%n")); |
| 59 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 60 | log4cxx::RollingFileAppender* rollingFileAppender = |
| 61 | new log4cxx::RollingFileAppender(layout, logDir+"/nlsr.log", true); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 62 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 63 | rollingFileAppender->setMaxFileSize("10MB"); |
| 64 | rollingFileAppender->setMaxBackupIndex(10); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 65 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 66 | log4cxx::helpers::Pool p; |
| 67 | rollingFileAppender->activateOptions(p); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 68 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 69 | log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(rollingFileAppender)); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 70 | |
| 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 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 77 | |
| 78 | configured = true; |
| 79 | } |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 80 | |
| 81 | bool |
| 82 | isValidLogLevel(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 Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 88 | } |