SET(CMAKE_CXX_FLAGS_PROFILING "${COMPILE_BASE_FLAGS} -O0 -pg -std=${CXX_STD} -fno-rtti")
SET(CMAKE_C_FLAGS_DEBUG "${COMPILE_BASE_FLAGS} -O0 -ggdb")
SET(CMAKE_CXX_FLAGS_DEBUG "${COMPILE_BASE_FLAGS} -O0 -ggdb -std=${CXX_STD} -fno-rtti")
-SET(CMAKE_C_FLAGS_RELEASE "${COMPILE_BASE_FLAGS} -O2 -DNDEBUG")
-SET(CMAKE_CXX_FLAGS_RELEASE "${COMPILE_BASE_FLAGS} -O2 -DNDEBUG -std=${CXX_STD} -fno-rtti")
+SET(CMAKE_C_FLAGS_RELEASE "${COMPILE_BASE_FLAGS} -O2 ")
+SET(CMAKE_CXX_FLAGS_RELEASE "${COMPILE_BASE_FLAGS} -O2 -std=${CXX_STD} -fno-rtti")
SET(CMAKE_C_FLAGS_CCOV "${COMPILE_BASE_FLAGS} -O0 --coverage")
SET(CMAKE_CXX_FLAGS_CCOV "${COMPILE_BASE_FLAGS} -O0 --coverage -std=${CXX_STD} -fno-rtti")
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#ifndef __AUDIT_DLOG_LOGSINK_H__
+#define __AUDIT_DLOG_LOGSINK_H__
+
+#include <string>
+
+#include "logsink.h"
+
+namespace audit {
+
+class DlogLogSink : public LogSink {
+public:
+ void sink(const std::string& message) override;
+};
+
+} // namespace audit
+#endif //__AUDIT_DLOG_LOGSINK_H__
public:
static void setLogLevel(const LogLevel level);
static LogLevel getLogLevel(void);
+ static void setTag(const std::string& tag);
+ static std::string getTag(void);
+ static void setBackend(LogSink *logSink);
static void log(LogLevel severity,
const std::string& file,
const unsigned int line,
const std::string& message);
private:
+ static LogSink *getBackend(void);
+
static LogLevel logLevel;
+ static std::unique_ptr<std::string> tag;
static std::unique_ptr<LogSink> backend;
};
(::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
#endif
+#define FORMAT(ITEMS) \
+(static_cast<std::ostringstream &>(std::ostringstream() << ITEMS)).str()
+
#define LOG(SEVERITY, MESSAGE) \
do { \
if (audit::LogLevel::SEVERITY <= audit::Logger::getLogLevel()) { \
audit::Logger::log(audit::LogLevel::SEVERITY, \
- __FILENAME__, __LINE__, __func__, MESSAGE); \
+ __FILENAME__, __LINE__, __func__, \
+ FORMAT(MESSAGE)); \
} \
} while (0)
#endif //NDEBUG
std::string LogLevelToString(const LogLevel level);
+LogLevel StringToLogLevel(const std::string& level);
} // namespace audit
#endif //__AUDIT_LOGGER_H__
BuildRequires: cmake
BuildRequires: pkgconfig(glib-2.0)
BuildRequires: pkgconfig(sqlite3)
+BuildRequires: pkgconfig(dlog)
BuildRequires: pkgconfig(libxml-2.0)
BuildRequires: pkgconfig(libtzplatform-config)
${KLAY_SRC}/audit/logger.cpp
${KLAY_SRC}/audit/null-sink.cpp
${KLAY_SRC}/audit/console-sink.cpp
+ ${KLAY_SRC}/audit/dlog-sink.cpp
)
SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack")
PKG_CHECK_MODULES(KLAY_DEPS REQUIRED gio-2.0
libxml-2.0
sqlite3
+ dlog
)
INCLUDE_DIRECTORIES(SYSTEM ${KLAY_INCLUDE}
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+#include <iostream>
+
+#include <dlog.h>
+
+#include <klay/audit/dlog-sink.h>
+#include <klay/audit/logger.h>
+
+namespace audit {
+
+void DlogLogSink::sink(const std::string &message)
+{
+ auto level = message.substr(0, message.find('<'));
+ auto subMsg = message.substr(message.find(':') + 1);
+ auto tag = Logger::getTag();
+
+ switch (StringToLogLevel(level)) {
+ case LogLevel::Error:
+ SLOG(LOG_ERROR, tag.c_str(), "%s", subMsg.c_str());
+ return;
+ case LogLevel::Warning:
+ SLOG(LOG_WARN, tag.c_str(), "%s", subMsg.c_str());
+ return;
+ case LogLevel::Debug:
+ SLOG(LOG_DEBUG, tag.c_str(), "%s", subMsg.c_str());
+ return;
+ case LogLevel::Info:
+ SLOG(LOG_INFO, tag.c_str(), "%s", subMsg.c_str());
+ return;
+ case LogLevel::Trace:
+ SLOG(LOG_VERBOSE, tag.c_str(), "%s", subMsg.c_str());
+ return;
+ default:
+ SLOG(LOG_SILENT, tag.c_str(), "%s", subMsg.c_str());
+ return;
+ }
+}
+
+} // namespace audit
namespace audit {
LogLevel Logger::logLevel = LogLevel::Trace;
-std::unique_ptr<LogSink> Logger::backend(new ConsoleLogSink());
+std::unique_ptr<std::string> Logger::tag([]() {
+ auto tag = Logger::getTag();
+ return tag.empty() ? new std::string("KLAY") : new std::string(tag);
+}());
+
+std::unique_ptr<LogSink> Logger::backend([]() {
+ auto *backend = Logger::getBackend();
+ return backend != nullptr ? std::move(backend) : new ConsoleLogSink();
+}());
std::string LogLevelToString(const LogLevel level)
{
}
}
+LogLevel StringToLogLevel(const std::string& level)
+{
+ if (level == "ERROR")
+ return LogLevel::Error;
+ else if (level == "WARN")
+ return LogLevel::Warning;
+ else if (level == "DEBUG")
+ return LogLevel::Debug;
+ else if (level == "INFO")
+ return LogLevel::Info;
+ else
+ return LogLevel::Trace;
+}
+
void Logger::setLogLevel(const LogLevel level)
{
Logger::logLevel = level;
return Logger::logLevel;
}
+void Logger::setTag(const std::string& tag)
+{
+ Logger::tag.reset(new std::string(tag));
+}
+
+std::string Logger::getTag(void)
+{
+ auto *pTag = Logger::tag.get();
+ return (pTag != nullptr) ? *pTag : std::string();
+}
+
+void Logger::setBackend(LogSink *logSink)
+{
+ if (logSink == nullptr)
+ return;
+
+ Logger::backend.reset(logSink);
+}
+
+LogSink *Logger::getBackend(void)
+{
+ auto *pBackend = Logger::backend.get();
+ return (pBackend != nullptr) ? pBackend : nullptr;
+}
+
void Logger::log(LogLevel severity,
const std::string& file,
const unsigned int line,
buffer << LogLevelToString(severity)
<< "<" << ::getpid() << ">:"
<< file << ":" << line
- << " " << func << " " << message
+ << ", " << func << "() > " << message
<< std::endl;
Logger::backend->sink(buffer.str());
#include <klay/error.h>
#include <klay/exception.h>
#include <klay/audit/logger.h>
+#include <klay/audit/console-sink.h>
+#include <klay/audit/dlog-sink.h>
+#include <klay/audit/null-sink.h>
#include <klay/testbench.h>
} catch (runtime::Exception& e) {
}
}
+
+TESTCASE(BackendTest)
+{
+ audit::Logger::setBackend(new audit::DlogLogSink());
+ audit::Logger::setTag("KLAY");
+ INFO("Dlog Test : " << "Info");
+ DEBUG("Dlog Test : " << "Debug");
+ WARN("Dlog Test : " << "Warning");
+ ERROR("Dlog Test : " << "Error");
+
+ audit::Logger::setBackend(new audit::ConsoleLogSink());
+ INFO("Console Test : " << "Info");
+ DEBUG("Console Test : " << "Debug");
+ WARN("Console Test : " << "Warning");
+ ERROR("Console Test : " << "Error");
+
+ audit::Logger::setBackend(new audit::NullLogSink());
+ INFO("Null Test : " << "Info");
+ DEBUG("Null Test : " << "Debug");
+ WARN("Null Test : " << "Warning");
+ ERROR("Null Test : " << "Error");
+}