--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Dariusz Michaluk <d.michaluk@samsung.com>
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @author Dariusz Michaluk (d.michaluk@samsung.com)
+ * @brief Systemd journal backend for logger
+ */
+
+#include "log/backend-journal.hpp"
+#include <systemd/sd-journal.h>
+
+namespace security_containers {
+namespace log {
+
+void SystemdJournalBackend::log(const std::string& severity,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message)
+{
+#define SD_JOURNAL_SUPPRESS_LOCATION
+ sd_journal_send("PRIORITY=%s", severity.c_str(),
+ "CODE_FILE=%s", file.c_str(),
+ "CODE_LINE=%d", line,
+ "CODE_FUNC=%s", func.c_str(),
+ "MESSAGE=%s", message.c_str(),
+ NULL);
+#undef SD_JOURNAL_SUPPRESS_LOCATION
+}
+
+
+} // namespace log
+} // namespace security_containers
+
+
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Dariusz Michaluk <d.michaluk@samsung.com>
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @author Dariusz Michaluk (d.michaluk@samsung.com)
+ * @brief Systemd journal backend for logger
+ */
+
+#ifndef COMMON_LOG_BACKEND_JOURNAL_HPP
+#define COMMON_LOG_BACKEND_JOURNAL_HPP
+
+#include "log/backend.hpp"
+
+
+namespace security_containers {
+namespace log {
+
+
+/**
+ systemd journal logging backend
+ */
+class SystemdJournalBackend : public LogBackend {
+public:
+ void log(const std::string& severity,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message) override;
+};
+
+
+} // namespace log
+} // namespace security_containers
+
+
+#endif // COMMON_LOG_BACKEND_JOURNAL_HPP
*/
class NullLogger : public LogBackend {
public:
- void log(const std::string& /*message*/) override {}
+ void log(const std::string& /*severity*/,
+ const std::string& /*file*/,
+ const unsigned int& /*line*/,
+ const std::string& /*func*/,
+ const std::string& /*message*/) override {}
};
#include "log/backend-stderr.hpp"
-#include <cstdio>
-
+#include <sstream>
+#include <iomanip>
+#include <sys/time.h>
namespace security_containers {
namespace log {
+namespace {
-void StderrBackend::log(const std::string& message)
+inline std::string getCurrentTime(void)
{
- fprintf(stderr, "%s", message.c_str());
+ char time[13];
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ struct tm* tm = localtime(&tv.tv_sec);
+ sprintf(time, "%02d:%02d:%02d.%03d", tm->tm_hour, tm->tm_min, tm->tm_sec, int(tv.tv_usec / 1000));
+
+ return std::string(time);
+}
+
+} // namespace
+
+void StderrBackend::log(const std::string& severity,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message)
+{
+ std::ostringstream logLine;
+
+ // example log string
+ // 06:52:35.123 [ERROR] src/util/fs.cpp:43 readFileContent: /file/file.txt is missing
+
+ logLine << getCurrentTime() << ' '
+ << std::left << std::setw(8) << '[' + severity + ']'
+ << std::left << std::setw(52) << file + ':' + std::to_string(line) + ' ' + func + ':'
+ << message << std::endl;
+ fprintf(stderr, "%s", logLine.str().c_str());
}
} // namespace log
} // namespace security_containers
+
*/
class StderrBackend : public LogBackend {
public:
- void log(const std::string& message) override;
+ void log(const std::string& severity,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message) override;
};
*/
class LogBackend {
public:
- virtual void log(const std::string& message) = 0;
+ virtual void log(const std::string& severity,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message) = 0;
virtual ~LogBackend() {}
};
#include "log/logger.hpp"
#include "log/backend-null.hpp"
-#include <iomanip>
#include <memory>
#include <mutex>
#include <cassert>
} // namespace
-Logger::Logger(const std::string& severity, const std::string& file, const int line)
+Logger::Logger(const std::string& severity,
+ const std::string& file,
+ const unsigned int line,
+ const std::string& func)
+ : mSeverity(severity),
+ mFile(stripProjectDir(file)),
+ mLine(line),
+ mFunc(func)
{
- mLogLine << std::left << std::setw(8) << '[' + severity + ']'
- << std::left << std::setw(40) << stripProjectDir(file) + ':' + std::to_string(line)
- << ' ';
+
}
void Logger::logMessage(const std::string& message)
{
- mLogLine << message << std::endl;
std::unique_lock<std::mutex> lock(gLogMutex);
- gLogBackendPtr->log(mLogLine.str());
+ gLogBackendPtr->log(mSeverity, mFile, mLine, mFunc, message);
}
void Logger::setLogLevel(LogLevel level)
} // namespace log
} // namespace security_containers
+
class Logger {
public:
- Logger(const std::string& severity, const std::string& file, const int line);
+ Logger(const std::string& severity,
+ const std::string& file,
+ const unsigned int line,
+ const std::string& func);
+
void logMessage(const std::string& message);
static void setLogLevel(LogLevel level);
static void setLogBackend(LogBackend* pBackend);
private:
- std::ostringstream mLogLine;
+ std::string mSeverity;
+ std::string mFile;
+ unsigned int mLine;
+ std::string mFunc;
};
} // namespace log
security_containers::log::LogLevel::SEVERITY) { \
std::ostringstream messageStream__; \
messageStream__ << MESSAGE; \
- security_containers::log::Logger logger(#SEVERITY, __FILE__, __LINE__); \
+ security_containers::log::Logger logger(#SEVERITY, __FILE__, __LINE__, __func__); \
logger.logMessage(messageStream__.str()); \
} \
} while(0)
BuildRequires: libvirt-devel
BuildRequires: libjson-devel
BuildRequires: pkgconfig(glib-2.0)
+BuildRequires: pkgconfig(libsystemd-journal)
%description
This package provides a daemon used to manage containers - start, stop and switch
## Link libraries ##############################################################
FIND_PACKAGE (Boost COMPONENTS program_options REQUIRED)
-PKG_CHECK_MODULES(SERVER_DEPS REQUIRED libvirt json gio-2.0)
+PKG_CHECK_MODULES(SERVER_DEPS REQUIRED libvirt json gio-2.0 libsystemd-journal)
INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${SERVER_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${SERVER_CODENAME} ${SERVER_DEPS_LIBRARIES} ${Boost_LIBRARIES})
* @brief Main file for the Security Containers Daemon
*/
+// Always log to console in DEBUG mode
+#if !defined(LOG_TO_CONSOLE) && !defined(NDEBUG)
+#define LOG_TO_CONSOLE
+#endif
+
#include "log/logger.hpp"
#include "log/backend-stderr.hpp"
+#include "log/backend-journal.hpp"
#include "utils/typeinfo.hpp"
#include "exception.hpp"
#include "server.hpp"
LogLevel level = validateLogLevel(vm["log-level"].as<std::string>());
Logger::setLogLevel(level);
+#ifdef LOG_TO_CONSOLE
Logger::setLogBackend(new StderrBackend());
+#else
+ Logger::setLogBackend(new SystemdJournalBackend());
+#endif
configPath = vm["config"].as<std::string>();
return 0;
}
+
## Link libraries ##############################################################
FIND_PACKAGE (Boost COMPONENTS unit_test_framework REQUIRED)
-PKG_CHECK_MODULES(UT_SERVER_DEPS REQUIRED libvirt json gio-2.0)
+PKG_CHECK_MODULES(UT_SERVER_DEPS REQUIRED libvirt json gio-2.0 libsystemd-journal)
INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${SERVER_FOLDER} ${UNIT_TESTS_FOLDER}
${UT_SERVER_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${UT_SERVER_CODENAME} ${UT_SERVER_DEPS_LIBRARIES} ${Boost_LIBRARIES})
*/
#include "ut.hpp"
-
#include "log/logger.hpp"
#include "log/backend.hpp"
#include "log/backend-stderr.hpp"
StubbedBackend(std::ostringstream& s) : mLogStream(s) {}
// stubbed function
- void log(const std::string& s) override
+ void log(const std::string& severity,
+ const std::string& file,
+ const unsigned int& line,
+ const std::string& func,
+ const std::string& message) override
{
- mLogStream << s;
+ mLogStream << '[' + severity + ']' + ' '
+ << file + ':' + std::to_string(line) + ' ' + func + ':'
+ << message << std::endl;
}
private:
BOOST_AUTO_TEST_SUITE_END()
+