Created and integrated levelized log
Change-Id: Ibfed76796a880bab2709b77b0c9540be7b75fad7
diff --git a/src/util/io-util.cpp b/src/util/io-util.cpp
index 0d02475..12572b3 100644
--- a/src/util/io-util.cpp
+++ b/src/util/io-util.cpp
@@ -2,6 +2,7 @@
#include "file-manifest.hpp"
#include "torrent-file.hpp"
+#include "util/logging.hpp"
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
@@ -148,7 +149,7 @@
return true;
}
catch (io::Error &e) {
- std::cerr << e.what() << std::endl;
+ LOG_ERROR << e.what() << std::endl;
return false;
}
}
@@ -166,14 +167,14 @@
is.sync();
is.seekg(start_offset + packetNum * dataPacketSize);
if (is.tellg() < 0) {
- std::cerr << "bad seek" << std::endl;
+ LOG_ERROR << "bad seek" << std::endl;
}
// read contents
std::vector<char> bytes(dataPacketSize);
is.read(&bytes.front(), dataPacketSize);
auto read_size = is.gcount();
if (is.bad() || read_size < 0) {
- std::cerr << "Bad read" << std::endl;
+ LOG_ERROR << "Bad read" << std::endl;
return nullptr;
}
// construct packet
diff --git a/src/util/logging.cpp b/src/util/logging.cpp
new file mode 100644
index 0000000..b067c80
--- /dev/null
+++ b/src/util/logging.cpp
@@ -0,0 +1,71 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+
+#include "util/logging.hpp"
+
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+#include <boost/log/core.hpp>
+#include <boost/log/expressions.hpp>
+#include <boost/log/sinks/text_file_backend.hpp>
+#include <boost/log/sources/record_ostream.hpp>
+#include <boost/log/sources/severity_logger.hpp>
+#include <boost/log/support/date_time.hpp>
+#include <boost/log/trivial.hpp>
+#include <boost/log/utility/setup/file.hpp>
+
+// ===== log macros =====
+namespace logging = boost::log;
+namespace src = boost::log::sources;
+namespace sinks = boost::log::sinks;
+namespace keywords = boost::log::keywords;
+namespace expr = boost::log::expressions;
+
+namespace ndn {
+namespace ntorrent {
+
+void LoggingUtil::init()
+{
+ logging::core::get()->set_filter
+ (
+ logging::trivial::severity >= SEVERITY_THRESHOLD
+ );
+
+ logging::add_file_log
+ (
+ keywords::file_name = "sample_%N.log", // < file name pattern >
+ keywords::rotation_size = 10 * 1024 * 1024, // < rotate files every 10 MiB... >
+ keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), // < ...or at midnight >
+ keywords::format = // < log record format >
+ (
+ expr::stream
+ << expr::attr< unsigned int >("LineID")
+ << ": [" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") << "]"
+ << ": <" << logging::trivial::severity
+ << "> " << expr::smessage
+ )
+ );
+}
+
+} // end ntorrent
+} // end ndn
+
+
diff --git a/src/util/logging.hpp b/src/util/logging.hpp
new file mode 100644
index 0000000..bb926a4
--- /dev/null
+++ b/src/util/logging.hpp
@@ -0,0 +1,58 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+#ifndef UTIL_LOGGING_HPP
+#define UTIL_LOGGING_HPP
+
+#define BOOST_LOG_DYN_LINK 1
+
+#include <boost/log/core.hpp>
+#include <boost/log/sources/global_logger_storage.hpp>
+#include <boost/log/sources/severity_logger.hpp>
+#include <boost/log/trivial.hpp>
+
+enum { SEVERITY_THRESHOLD = boost::log::trivial::warning };
+
+// register a global logger
+BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(logger, boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level>)
+
+// just a helper macro used by the macros below - don't use it in your code
+#define LOG(severity) BOOST_LOG_SEV(logger::get(), boost::log::trivial::severity)
+
+// ===== log macros =====
+#define LOG_TRACE LOG(trace)
+#define LOG_DEBUG LOG(debug)
+#define LOG_INFO LOG(info)
+#define LOG_WARNING LOG(warning)
+#define LOG_ERROR LOG(error)
+#define LOG_FATAL LOG(fatal)
+
+namespace ndn {
+namespace ntorrent {
+
+struct LoggingUtil {
+ static void init();
+ // Initialize the log for the application. THis method must be called in the main function in
+ // the application before any logging may be performed.
+};
+
+} // end ntorrent
+} // end ndn
+#endif // UTIL_LOGGING_HPP