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 | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 19 | **/ |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 20 | |
| 21 | #include "logger.hpp" |
| 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 | void |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 36 | INIT_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 | |
| 46 | void |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 47 | INIT_LOGGERS(const std::string& logDir, const std::string& logLevel) |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 48 | { |
| 49 | static bool configured = false; |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 50 | |
| 51 | if (configured) { |
| 52 | return; |
| 53 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 54 | |
| 55 | log4cxx::PatternLayoutPtr |
Ashlesh Gawande | f6bfb14 | 2017-07-19 15:29:23 -0500 | [diff] [blame] | 56 | layout(new log4cxx::PatternLayout("%date{%s}.%date{SSS} %p: [%c] %m%n")); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 57 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 58 | log4cxx::RollingFileAppender* rollingFileAppender = |
| 59 | new log4cxx::RollingFileAppender(layout, logDir+"/nlsr.log", true); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 60 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 61 | rollingFileAppender->setMaxFileSize("10MB"); |
| 62 | rollingFileAppender->setMaxBackupIndex(10); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 63 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 64 | log4cxx::helpers::Pool p; |
| 65 | rollingFileAppender->activateOptions(p); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 66 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 67 | log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(rollingFileAppender)); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 68 | |
| 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 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 75 | |
| 76 | configured = true; |
| 77 | } |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 78 | |
| 79 | bool |
| 80 | isValidLogLevel(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 Gawande | f6bfb14 | 2017-07-19 15:29:23 -0500 | [diff] [blame] | 86 | } |