jni: Android-specific custom logger for NFD
Change-Id: I4d98a58b8320735e40b5da3d358b9c71b558d995
Refs: #2508
diff --git a/app/src/main/jni/nfd-android/custom-logger.hpp b/app/src/main/jni/nfd-android/custom-logger.hpp
new file mode 100644
index 0000000..5355657
--- /dev/null
+++ b/app/src/main/jni/nfd-android/custom-logger.hpp
@@ -0,0 +1,140 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_ANDROID_CUSTOM_LOGGER_HPP
+#define NFD_ANDROID_CUSTOM_LOGGER_HPP
+
+#include "common.hpp"
+#include <android/log.h>
+
+namespace nfd {
+
+/** \brief indicates a log level
+ * \note This type is internal. Logger should be accessed through NFD_LOG_* macros.
+ */
+enum LogLevel {
+ LOG_FATAL = -1, // fatal (will be logged unconditionally)
+ LOG_NONE = 0, // no messages
+ LOG_ERROR = 1, // serious error messages
+ LOG_WARN = 2, // warning messages
+ LOG_INFO = 3, // informational messages
+ LOG_DEBUG = 4, // debug messages
+ LOG_TRACE = 5, // trace messages (most verbose)
+ LOG_ALL = 255 // all messages
+};
+
+/** \brief provides logging for a module
+ * \note This type is internal. Logger should be accessed through NFD_LOG_* macros.
+ * \note This type is copyable because logger can be declared as a field of
+ * (usually template) classes, and shouldn't prevent those classes to be copyable.
+ */
+class Logger
+{
+public:
+ Logger(const std::string& name, LogLevel level);
+
+ bool
+ isEnabled(LogLevel level) const
+ {
+ return m_enabledLogLevel >= level;
+ }
+
+ void
+ setLogLevel(LogLevel level)
+ {
+ m_enabledLogLevel = level;
+ }
+
+ const std::string&
+ getName() const
+ {
+ return m_moduleName;
+ }
+
+ void
+ setName(const std::string& name)
+ {
+ m_moduleName = name;
+ }
+
+private:
+ std::string m_moduleName;
+ LogLevel m_enabledLogLevel;
+};
+
+inline std::ostream&
+operator<<(std::ostream& output, const Logger& logger)
+{
+ output << logger.getName();
+ return output;
+}
+
+} // namespace nfd
+
+#include "core/logger-factory.hpp"
+
+namespace nfd {
+
+#define NFD_LOG_INIT(name) \
+static nfd::Logger& g_logger = nfd::LoggerFactory::create(name)
+
+#define NFD_LOG_INCLASS_DECLARE() \
+static nfd::Logger& g_logger
+
+#define NFD_LOG_INCLASS_DEFINE(cls, name) \
+nfd::Logger& cls::g_logger = nfd::LoggerFactory::create(name)
+
+#define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name) \
+template<class T> \
+nfd::Logger& cls<T>::g_logger = nfd::LoggerFactory::create(name)
+
+#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name) \
+template<> \
+nfd::Logger& cls<specialization>::g_logger = nfd::LoggerFactory::create(name)
+
+#define NFD_LOG_INCLASS_2TEMPLATE_SPECIALIZATION_DEFINE(cls, s1, s2, name) \
+template<> \
+nfd::Logger& cls<s1, s2>::g_logger = nfd::LoggerFactory::create(name)
+
+#define NFD_LOG(nfdLevel, androidLevel, msg, expression) \
+do { \
+ if (g_logger.isEnabled(::nfd::LOG_##nfdLevel)) { \
+ std::ostringstream os; \
+ os << expression; \
+ __android_log_print(ANDROID_LOG_##androidLevel, \
+ g_logger.getName().c_str(), "%s", os.str().c_str()); \
+ } \
+} while (false)
+
+#define NFD_LOG_TRACE(expression) NFD_LOG(TRACE, VERBOSE, TRACE, expression)
+#define NFD_LOG_DEBUG(expression) NFD_LOG(DEBUG, DEBUG, DEBUG, expression)
+#define NFD_LOG_INFO(expression) NFD_LOG(INFO, INFO, INFO, expression)
+#define NFD_LOG_WARN(expression) NFD_LOG(WARN, WARN, WARNING, expression)
+#define NFD_LOG_ERROR(expression) NFD_LOG(ERROR, ERROR, ERROR, expression)
+#define NFD_LOG_FATAL(expression) NFD_LOG(FATAL, FATAL, FATAL, expression)
+
+} // namespace nfd
+
+#endif // NFD_ANDROID_CUSTOM_LOGGER_HPP