From 6da281c3247ed2ac10ce15df1535fbbd3fe22f9c Mon Sep 17 00:00:00 2001 From: RomanKubiak Date: Thu, 16 Jul 2015 13:43:08 +0200 Subject: [PATCH] Added file and syslog backends [Feature] New logger backends (file and syslog) [Cause] N/A [Solution] N/A [Verification] Build, install, run tests Change-Id: I0de1f4e121ca5ff24b5a0f148015afda7dc9e503 --- libs/logger/CMakeLists.txt | 7 ++++- libs/logger/backend-file.cpp | 46 ++++++++++++++++++++++++++++ libs/logger/backend-file.hpp | 46 ++++++++++++++++++++++++++++ libs/logger/backend-journal.cpp | 3 +- libs/logger/backend-stderr.cpp | 6 ++-- libs/logger/backend-stderr.hpp | 3 ++ libs/logger/backend-syslog.cpp | 66 +++++++++++++++++++++++++++++++++++++++++ libs/logger/backend-syslog.hpp | 43 +++++++++++++++++++++++++++ libs/logger/logger.hpp | 2 ++ 9 files changed, 217 insertions(+), 5 deletions(-) create mode 100644 libs/logger/backend-file.cpp create mode 100644 libs/logger/backend-file.hpp create mode 100644 libs/logger/backend-syslog.cpp create mode 100644 libs/logger/backend-syslog.hpp diff --git a/libs/logger/CMakeLists.txt b/libs/logger/CMakeLists.txt index 6b3352d..d1e82eb 100644 --- a/libs/logger/CMakeLists.txt +++ b/libs/logger/CMakeLists.txt @@ -36,11 +36,16 @@ SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES ) ## Link libraries ############################################################## -PKG_CHECK_MODULES(LOGGER_DEPS REQUIRED libsystemd-journal) +PKG_CHECK_MODULES(LOGGER_DEPS libsystemd-journal QUIET) INCLUDE_DIRECTORIES(${LIBS_FOLDER}) INCLUDE_DIRECTORIES(SYSTEM ${LOGGER_DEPS_INCLUDE_DIRS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} ${LOGGER_DEPS_LIBRARIES}) +## Add constants ############################################################### +if(LOGGER_DEPS_FOUND) + ADD_DEFINITIONS(-DHAVE_SYSTEMD_JOURNAL=${LOGGER_DEPS_FOUND}) +endif() + ## Generate the pc file ######################################################## CONFIGURE_FILE(${PC_FILE}.in ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} @ONLY) diff --git a/libs/logger/backend-file.cpp b/libs/logger/backend-file.cpp new file mode 100644 index 0000000..45888ff --- /dev/null +++ b/libs/logger/backend-file.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Roman Kubiak (r.kubiak@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 Roman Kubiak (r.kubiak@samsung.com) + * @brief File backend for logger + */ + +#include "logger/config.hpp" +#include "logger/formatter.hpp" +#include "logger/backend-file.hpp" + +#include + +namespace logger { + +void FileBackend::log(LogLevel logLevel, + const std::string& file, + const unsigned int& line, + const std::string& func, + const std::string& message) +{ + std::ofstream out(mfilePath, std::ios::app); + out << LogFormatter::getHeader(logLevel, file, line, func); + out << message; + out << std::endl; +} + + +} // namespace logger diff --git a/libs/logger/backend-file.hpp b/libs/logger/backend-file.hpp new file mode 100644 index 0000000..16bab1c --- /dev/null +++ b/libs/logger/backend-file.hpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Roman Kubiak (r.kubiak@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 Roman Kubiak (r.kubiak@samsung.com) + * @brief File backend for logger + */ + +#ifndef COMMON_LOGGER_BACKEND_FILE_HPP +#define COMMON_LOGGER_BACKEND_FILE_HPP + +#include "logger/backend.hpp" + +namespace logger { + +class FileBackend : public LogBackend { +public: + FileBackend(const std::string& filePath) : mfilePath(filePath) {} + void log(LogLevel logLevel, + const std::string& file, + const unsigned int& line, + const std::string& func, + const std::string& message) override; +private: + std::string mfilePath; +}; + +} // namespace logger + +#endif // COMMON_LOGGER_BACKEND_FILE_HPP diff --git a/libs/logger/backend-journal.cpp b/libs/logger/backend-journal.cpp index 74fc581..af147cd 100644 --- a/libs/logger/backend-journal.cpp +++ b/libs/logger/backend-journal.cpp @@ -21,7 +21,7 @@ * @author Dariusz Michaluk (d.michaluk@samsung.com) * @brief Systemd journal backend for logger */ - +#ifdef HAVE_SYSTEMD_JOURNAL #include "logger/config.hpp" #include "logger/backend-journal.hpp" @@ -69,3 +69,4 @@ void SystemdJournalBackend::log(LogLevel logLevel, } } // namespace logger +#endif // HAVE_SYSTEMD_JOURNAL diff --git a/libs/logger/backend-stderr.cpp b/libs/logger/backend-stderr.cpp index c0d3a3c..f063383 100644 --- a/libs/logger/backend-stderr.cpp +++ b/libs/logger/backend-stderr.cpp @@ -49,11 +49,11 @@ void StderrBackend::log(LogLevel logLevel, for (const auto& messageLine : tokens) { if (!messageLine.empty()) { fprintf(stderr, - "%s%s%s%s\n", - logColor.c_str(), + "%s%s %s%s\n", + mUseColours ? logColor.c_str() : "", header.c_str(), messageLine.c_str(), - defaultColor.c_str()); + mUseColours ? defaultColor.c_str() : ""); } } } diff --git a/libs/logger/backend-stderr.hpp b/libs/logger/backend-stderr.hpp index 4cdd0ec..efb39fe 100644 --- a/libs/logger/backend-stderr.hpp +++ b/libs/logger/backend-stderr.hpp @@ -34,11 +34,14 @@ namespace logger { */ class StderrBackend : public LogBackend { public: + StderrBackend(const bool useColours = true) : mUseColours(useColours) {} void log(LogLevel logLevel, const std::string& file, const unsigned int& line, const std::string& func, const std::string& message) override; +private: + bool mUseColours; }; } // namespace logger diff --git a/libs/logger/backend-syslog.cpp b/libs/logger/backend-syslog.cpp new file mode 100644 index 0000000..2686222 --- /dev/null +++ b/libs/logger/backend-syslog.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Roman Kubiak (r.kubiak@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 Roman Kubiak (r.kubiak@samsung.com) + * @brief Syslog backend for logger + */ + +#include "logger/config.hpp" +#include "logger/formatter.hpp" +#include "logger/backend-syslog.hpp" + +#include +#include +namespace logger { + +namespace { + +inline int toSyslogPriority(LogLevel logLevel) +{ + switch (logLevel) { + case LogLevel::ERROR: + return LOG_ERR; // 3 + case LogLevel::WARN: + return LOG_WARNING; // 4 + case LogLevel::INFO: + return LOG_INFO; // 6 + case LogLevel::DEBUG: + return LOG_DEBUG; // 7 + case LogLevel::TRACE: + return LOG_DEBUG; // 7 + case LogLevel::HELP: + return LOG_DEBUG; // 7 + default: + return LOG_DEBUG; // 7 + } +} + +} // namespace + +void SyslogBackend::log(LogLevel logLevel, + const std::string& file, + const unsigned int& line, + const std::string& func, + const std::string& message) +{ + ::syslog(toSyslogPriority(logLevel), "%s %s", LogFormatter::getHeader(logLevel, file, line, func).c_str(), message.c_str()); +} + +} // namespace logger diff --git a/libs/logger/backend-syslog.hpp b/libs/logger/backend-syslog.hpp new file mode 100644 index 0000000..2c9fde6 --- /dev/null +++ b/libs/logger/backend-syslog.hpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Roman Kubiak (r.kubiak@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 Roman Kubiak (r.kubiak@samsung.com) + * @brief Syslog backend for logger + */ + +#ifndef COMMON_LOGGER_BACKEND_SYSLOG_HPP +#define COMMON_LOGGER_BACKEND_SYSLOG_HPP + +#include "logger/backend.hpp" + +namespace logger { + +class SyslogBackend : public LogBackend { +public: + void log(LogLevel logLevel, + const std::string& file, + const unsigned int& line, + const std::string& func, + const std::string& message) override; +}; + +} // namespace logger + +#endif // COMMON_LOGGER_BACKEND_SYSLOG_HPP diff --git a/libs/logger/logger.hpp b/libs/logger/logger.hpp index 8d8d433..3a0dee4 100644 --- a/libs/logger/logger.hpp +++ b/libs/logger/logger.hpp @@ -26,6 +26,8 @@ #define COMMON_LOGGER_LOGGER_HPP #include "logger/level.hpp" +#include "logger/backend-file.hpp" +#include "logger/backend-stderr.hpp" #include #include -- 2.7.4