From fb587297bc524bb770e34c31eafbaf296bd8621e Mon Sep 17 00:00:00 2001 From: Jacek Pielaszkiewicz Date: Mon, 16 Jun 2014 13:05:43 +0200 Subject: [PATCH] Initial library content. [Bug/Feature] Initial library content. [Cause] N/A [Solution] N/A [Verification] Build, install, run tests Change-Id: Id3336c4421c6360072a2d56aa3eadc964511cc39 Signed-off-by: Jacek Pielaszkiewicz --- CMakeLists.txt | 73 ++++++++++++++++++++++++ packaging/libLogger.spec | 43 +++++++++------ src/CMakeLists.txt | 40 ++++++++++++++ src/libLogger.pc.in | 12 ++++ src/logger/backend-journal.cpp | 69 +++++++++++++++++++++++ src/logger/backend-journal.hpp | 46 ++++++++++++++++ src/logger/backend-null.hpp | 46 ++++++++++++++++ src/logger/backend-stderr.cpp | 61 +++++++++++++++++++++ src/logger/backend-stderr.hpp | 46 ++++++++++++++++ src/logger/backend.hpp | 49 +++++++++++++++++ src/logger/ccolor.cpp | 41 ++++++++++++++ src/logger/ccolor.hpp | 53 ++++++++++++++++++ src/logger/config.hpp | 59 ++++++++++++++++++++ src/logger/formatter.cpp | 122 +++++++++++++++++++++++++++++++++++++++++ src/logger/formatter.hpp | 49 +++++++++++++++++ src/logger/level.cpp | 67 ++++++++++++++++++++++ src/logger/level.hpp | 54 ++++++++++++++++++ src/logger/logger.cpp | 81 +++++++++++++++++++++++++++ src/logger/logger.hpp | 80 +++++++++++++++++++++++++++ 19 files changed, 1075 insertions(+), 16 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 src/CMakeLists.txt create mode 100644 src/libLogger.pc.in create mode 100644 src/logger/backend-journal.cpp create mode 100644 src/logger/backend-journal.hpp create mode 100644 src/logger/backend-null.hpp create mode 100644 src/logger/backend-stderr.cpp create mode 100644 src/logger/backend-stderr.hpp create mode 100644 src/logger/backend.hpp create mode 100644 src/logger/ccolor.cpp create mode 100644 src/logger/ccolor.hpp create mode 100644 src/logger/config.hpp create mode 100644 src/logger/formatter.cpp create mode 100644 src/logger/formatter.hpp create mode 100644 src/logger/level.cpp create mode 100644 src/logger/level.hpp create mode 100644 src/logger/logger.cpp create mode 100644 src/logger/logger.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4265c41 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,73 @@ +# Copyright (c) 2014 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. +# +# +# @file CMakeLists.txt +# @author Jacek Pielaszkiewicz (j.pielaszkie@samsung.com) +# +CMAKE_MINIMUM_REQUIRED (VERSION 2.6.2) +PROJECT(Logger) + +SET(_LIB_VERSION_ "0.0.1") +SET(_LIB_SOVERSION_ "0") + +## pkgconfig ################################################################### +INCLUDE(FindPkgConfig) + +## default CMAKE_INSTALL_* variables ########################################### +INCLUDE(GNUInstallDirs) + +## Compiler flags, depending on the build type ################################# +IF(NOT CMAKE_BUILD_TYPE) + SET(CMAKE_BUILD_TYPE "DEBUG") +ENDIF(NOT CMAKE_BUILD_TYPE) +MESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}") + +# special case for a GCC < 4.7, assume rest is fine +IF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7) + SET(CXX_11_STD "c++0x") +else() + SET(CXX_11_STD "c++11") +endif() + +SET(CMAKE_C_FLAGS_PROFILING "-g -O0 -pg") +SET(CMAKE_CXX_FLAGS_PROFILING "-g -std=${CXX_11_STD} -O0 -pg") +SET(CMAKE_C_FLAGS_DEBUG "-g -O0 -ggdb") +SET(CMAKE_CXX_FLAGS_DEBUG "-g -std=${CXX_11_STD} -O0 -ggdb") +SET(CMAKE_C_FLAGS_RELEASE "-g -O2 -DNDEBUG") +SET(CMAKE_CXX_FLAGS_RELEASE "-g -std=${CXX_11_STD} -O2 -DNDEBUG") +SET(CMAKE_C_FLAGS_CCOV "-g -O2 --coverage") +SET(CMAKE_CXX_FLAGS_CCOV "-g -std=${CXX_11_STD} -O2 --coverage") + +ADD_DEFINITIONS("-fPIC") # Position Independent Code +ADD_DEFINITIONS("-Werror") # Make all warnings into errors +ADD_DEFINITIONS("-Wall") # Generate all warnings +ADD_DEFINITIONS("-Wextra") # Generate even more extra warnings +ADD_DEFINITIONS("-pedantic") # Be pedantic +ADD_DEFINITIONS("-pedantic-errors") # Make pedantic warnings into errors +ADD_DEFINITIONS(-DPROGRAM_VERSION="${VERSION}") +ADD_DEFINITIONS(-DPROJECT_SOURCE_DIR="${PROJECT_SOURCE_DIR}") + +## Subdirectories ############################################################## +SET(SRC_FOLDER ${PROJECT_SOURCE_DIR}/src) + +IF(NOT DEFINED LIB_INSTALL_DIR) + SET(LIB_INSTALL_DIR "${CMAKE_INSTALL_FULL_LIBDIR}") +ENDIF(NOT DEFINED LIB_INSTALL_DIR) + +IF(NOT DEFINED INCLUDE_INSTALL_DIR) + SET(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") +ENDIF(NOT DEFINED INCLUDE_INSTALL_DIR) + +ADD_SUBDIRECTORY(src) diff --git a/packaging/libLogger.spec b/packaging/libLogger.spec index bd94a60..6af408a 100644 --- a/packaging/libLogger.spec +++ b/packaging/libLogger.spec @@ -4,7 +4,11 @@ Release: 0 Source0: %{name}-%{version}.tar.gz License: Apache-2.0 Group: Security/Other -Summary: libLogger library. +Summary: Logger library + +BuildRequires: cmake +BuildRequires: pkgconfig(libsystemd-journal) +BuildRequires: boost-devel %description The package provides libLogger library. @@ -12,39 +16,46 @@ The package provides libLogger library. %files %manifest packaging/libLogger.manifest %defattr(644,root,root,755) +%{_libdir}/libLogger.so.0 +%attr(755,root,root) %{_libdir}/libLogger.so.0.0.1 %package devel -Summary: libLogger development package +Summary: Development logger library Group: Development/Libraries +Requires: libLogger = %{version}-%{release} %description devel The package provides libLogger development tools and libs. %files devel %defattr(644,root,root,755) - -%package tests -Summary: libLoger tests -Group: Development/Libraries - -%description tests -The package provides libLogger test tools. - -%files tests -%defattr(644,root,root,755) +%{_libdir}/libLogger.so +%{_includedir}/sc-tools +%{_libdir}/pkgconfig/*.pc %prep %setup -q %build +%{!?build_type:%define build_type "RELEASE"} + +%if %{build_type} == "DEBUG" || %{build_type} == "PROFILING" + CFLAGS="$CFLAGS -Wp,-U_FORTIFY_SOURCE" + CXXFLAGS="$CXXFLAGS -Wp,-U_FORTIFY_SOURCE" +%endif + +%cmake . \ + -DVERSION=%{version} \ + -DCMAKE_BUILD_TYPE=%{build_type} + +make -k %{?jobs:-j%jobs} %install +%make_install %clean rm -rf %{buildroot} -%post - -%preun +%post -p /sbin/ldconfig -%postun +%postun -p /sbin/ldconfig diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..6a28e68 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,40 @@ +# Copyright (c) 2014 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. +# +# +# @file CMakeLists.txt +# @author Jacek Pielaszkiewicz (j.pielaszkie@samsung.com) +# +SET(PC_FILE "${SRC_FOLDER}/lib${PROJECT_NAME}.pc") + +MESSAGE(STATUS "Generating makefile for the libLogger sources...") +FILE(GLOB_RECURSE HEADERS ${SRC_FOLDER} *.hpp) +FILE(GLOB_RECURSE SRCS ${SRC_FOLDER} *.cpp *.hpp) + +## Setup target ################################################################ +ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS}) +SET_PROPERTY(TARGET ${PROJECT_NAME} PROPERTY SOVERSION ${_LIB_SOVERSION_}) +SET_PROPERTY(TARGET ${PROJECT_NAME} PROPERTY VERSION ${_LIB_VERSION_}) + +## Link libraries ############################################################## +PKG_CHECK_MODULES(LIB_DEPS REQUIRED libsystemd-journal) +INCLUDE_DIRECTORIES(${SRC_FOLDER}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} ${LIB_DEPS_LIBRARIES}) + +## Install ##################################################################### +CONFIGURE_FILE("${PC_FILE}.in" "${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE}" @ONLY) + +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries) +INSTALL(FILES ${HEADERS} DESTINATION "${INCLUDE_INSTALL_DIR}/sc-tools/logger") +INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE}" DESTINATION "/usr/lib/pkgconfig") diff --git a/src/libLogger.pc.in b/src/libLogger.pc.in new file mode 100644 index 0000000..a165092 --- /dev/null +++ b/src/libLogger.pc.in @@ -0,0 +1,12 @@ +# Package Information for pkg-config + +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=@CMAKE_INSTALL_PREFIX@ +libdir=@LIB_INSTALL_DIR@ +includedir=@INCLUDE_INSTALL_DIR@ + +Name: libLogger +Description: Logger library +Version: @VERSION@ +Libs: -L${libdir} -lLogger +Cflags: -I${includedir}/sc-tools diff --git a/src/logger/backend-journal.cpp b/src/logger/backend-journal.cpp new file mode 100644 index 0000000..a478050 --- /dev/null +++ b/src/logger/backend-journal.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Dariusz Michaluk + * + * 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 "logger/config.hpp" +#include "logger/backend-journal.hpp" + +#define SD_JOURNAL_SUPPRESS_LOCATION +#include + +namespace logger { + +namespace { + +inline int toJournalPriority(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 + default: + return LOG_DEBUG; + } +} + +} // namespace + +void SystemdJournalBackend::log(LogLevel logLevel, + const std::string& file, + const unsigned int& line, + const std::string& func, + const std::string& message) +{ + sd_journal_send("PRIORITY=%d", toJournalPriority(logLevel), + "CODE_FILE=%s", file.c_str(), + "CODE_LINE=%d", line, + "CODE_FUNC=%s", func.c_str(), + "MESSAGE=%s", message.c_str(), + NULL); +} + +} // namespace logger diff --git a/src/logger/backend-journal.hpp b/src/logger/backend-journal.hpp new file mode 100644 index 0000000..f5b9fe6 --- /dev/null +++ b/src/logger/backend-journal.hpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Dariusz Michaluk + * + * 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 LOGGER_BACKEND_JOURNAL_HPP +#define LOGGER_BACKEND_JOURNAL_HPP + +#include "logger/backend.hpp" + +namespace logger { + +/** + * systemd journal logging backend + */ +class SystemdJournalBackend : 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 // LOGGER_BACKEND_JOURNAL_HPP diff --git a/src/logger/backend-null.hpp b/src/logger/backend-null.hpp new file mode 100644 index 0000000..32a4401 --- /dev/null +++ b/src/logger/backend-null.hpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Pawel Broda + * + * 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 Pawel Broda (p.broda@partner.samsung.com) + * @brief Null backend for logger + */ + +#ifndef LOGGER_BACKEND_NULL_HPP +#define LOGGER_BACKEND_NULL_HPP + +#include "logger/backend.hpp" + +namespace logger { + +/** + * Null logging backend + */ +class NullLogger : 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 // LOGGER_BACKEND_NULL_HPP diff --git a/src/logger/backend-stderr.cpp b/src/logger/backend-stderr.cpp new file mode 100644 index 0000000..c0d3a3c --- /dev/null +++ b/src/logger/backend-stderr.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Pawel Broda + * + * 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 Pawel Broda (p.broda@partner.samsung.com) + * @brief Stderr backend for logger + */ + +#include "logger/config.hpp" +#include "logger/backend-stderr.hpp" +#include "logger/formatter.hpp" + +#include + +namespace logger { + +void StderrBackend::log(LogLevel logLevel, + const std::string& file, + const unsigned int& line, + const std::string& func, + const std::string& message) +{ + typedef boost::char_separator charSeparator; + typedef boost::tokenizer tokenizer; + + // example log string + // 06:52:35.123 [ERROR] src/util/fs.cpp:43 readFileContent: /file/file.txt is missing + + const std::string logColor = LogFormatter::getConsoleColor(logLevel); + const std::string defaultColor = LogFormatter::getDefaultConsoleColor(); + const std::string header = LogFormatter::getHeader(logLevel, file, line, func); + tokenizer tokens(message, charSeparator("\n")); + for (const auto& messageLine : tokens) { + if (!messageLine.empty()) { + fprintf(stderr, + "%s%s%s%s\n", + logColor.c_str(), + header.c_str(), + messageLine.c_str(), + defaultColor.c_str()); + } + } +} + +} // namespace logger diff --git a/src/logger/backend-stderr.hpp b/src/logger/backend-stderr.hpp new file mode 100644 index 0000000..a141a83 --- /dev/null +++ b/src/logger/backend-stderr.hpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Pawel Broda + * + * 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 Pawel Broda (p.broda@partner.samsung.com) + * @brief Stderr backend for logger + */ + +#ifndef LOGGER_BACKEND_STDERR_HPP +#define LOGGER_BACKEND_STDERR_HPP + +#include "logger/backend.hpp" + +namespace logger { + +/** + * Stderr logging backend + */ +class StderrBackend : 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 // LOGGER_BACKEND_STDERR_HPP diff --git a/src/logger/backend.hpp b/src/logger/backend.hpp new file mode 100644 index 0000000..5adaf4a --- /dev/null +++ b/src/logger/backend.hpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Pawel Broda + * + * 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 Pawel Broda (p.broda@partner.samsung.com) + * @brief Logging backend + */ + +#ifndef LOGGER_BACKEND_HPP +#define LOGGER_BACKEND_HPP + +#include "logger/level.hpp" + +#include + +namespace logger { + +/** + * Abstract class for logger + */ +class LogBackend { +public: + virtual void log(LogLevel logLevel, + const std::string& file, + const unsigned int& line, + const std::string& func, + const std::string& message) = 0; + virtual ~LogBackend() {} +}; + +} // namespace logger + +#endif // LOGGER_BACKEND_HPP diff --git a/src/logger/ccolor.cpp b/src/logger/ccolor.cpp new file mode 100644 index 0000000..7424044 --- /dev/null +++ b/src/logger/ccolor.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Dariusz Michaluk + * + * 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 Console color for StderrBackend logger + */ + +#include "logger/config.hpp" +#include "logger/ccolor.hpp" + +#include + +namespace logger { + +std::string getConsoleEscapeSequence(Attributes attr, Color color) +{ + char command[10]; + + // Command is the control command to the terminal + snprintf(command, sizeof(command), "%c[%d;%dm", 0x1B, attr, color); + return std::string(command); +} + +} // namespace logger diff --git a/src/logger/ccolor.hpp b/src/logger/ccolor.hpp new file mode 100644 index 0000000..0f8a6f4 --- /dev/null +++ b/src/logger/ccolor.hpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Dariusz Michaluk + * + * 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 Console color for StderrBackend logger + */ + +#ifndef LOGGER_CCOLOR_HPP +#define LOGGER_CCOLOR_HPP + +#include + +namespace logger { + +enum class Color : unsigned int { + DEFAULT = 0, + BLACK = 90, + RED = 91, + GREEN = 92, + YELLOW = 93, + BLUE = 94, + MAGENTA = 95, + CYAN = 96, + WHITE = 97 +}; + +enum class Attributes : unsigned int { + DEFAULT = 0, + BOLD = 1 +}; + +std::string getConsoleEscapeSequence(Attributes attr, Color color); + +} // namespace logger + +#endif // LOGGER_CCOLOR_HPP diff --git a/src/logger/config.hpp b/src/logger/config.hpp new file mode 100644 index 0000000..a65c2ad --- /dev/null +++ b/src/logger/config.hpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk (l.pawelczyk@partner.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 Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com) + * @brief Configuration file for the code + */ + +#ifndef CONFIG_HPP +#define CONFIG_HPP + +#ifdef __clang__ +#define CLANG_VERSION (__clang__major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) +#endif // __clang__ + +#if defined __GNUC__ && !defined __clang__ // clang also defines GCC versions +#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) +#endif // __GNUC__ + +#ifdef GCC_VERSION + +#if GCC_VERSION < 40800 +// GCC 4.8 is the first where those defines are not required for +// std::this_thread::sleep_for() and ::yield(). They might exist though +// in previous versions depending on the build configuration of the GCC. +#ifndef _GLIBCXX_USE_NANOSLEEP +#define _GLIBCXX_USE_NANOSLEEP +#endif // _GLIBCXX_USE_NANOSLEEP +#ifndef _GLIBCXX_USE_SCHED_YIELD +#define _GLIBCXX_USE_SCHED_YIELD +#endif // _GLIBCXX_USE_SCHED_YIELD +#endif // GCC_VERSION < 40800 + +#if GCC_VERSION < 40700 +// Those appeared in 4.7 with full c++11 support +#define final +#define override +#define thread_local __thread // use GCC extension instead of C++11 +#endif // GCC_VERSION < 40700 + +#endif // GCC_VERSION + +#endif // CONFIG_HPP diff --git a/src/logger/formatter.cpp b/src/logger/formatter.cpp new file mode 100644 index 0000000..c621e21 --- /dev/null +++ b/src/logger/formatter.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Dariusz Michaluk + * + * 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 Helper formatter for logger + */ + +#include "logger/config.hpp" +#include "logger/formatter.hpp" +#include "logger/ccolor.hpp" + +#include +#include +#include +#include +#include +#include + +namespace logger { + +namespace { + +const int TIME_COLUMN_LENGTH = 12; +const int SEVERITY_COLUMN_LENGTH = 8; +const int THREAD_COLUMN_LENGTH = 3; +const int FILE_COLUMN_LENGTH = 60; + +std::atomic gNextThreadId(1); +thread_local unsigned int gThisThreadId(0); + +} // namespace + +unsigned int LogFormatter::getCurrentThread(void) +{ + unsigned int id = gThisThreadId; + if (id == 0) { + gThisThreadId = id = gNextThreadId++; + } + + return id; +} + +std::string LogFormatter::getCurrentTime(void) +{ + char time[TIME_COLUMN_LENGTH + 1]; + struct timeval tv; + gettimeofday(&tv, NULL); + struct tm* tm = localtime(&tv.tv_sec); + snprintf(time, + sizeof(time), + "%02d:%02d:%02d.%03d", + tm->tm_hour, + tm->tm_min, + tm->tm_sec, + int(tv.tv_usec / 1000)); + + return std::string(time); +} + +std::string LogFormatter::getConsoleColor(LogLevel logLevel) +{ + switch (logLevel) { + case LogLevel::ERROR: + return getConsoleEscapeSequence(Attributes::BOLD, Color::RED); + case LogLevel::WARN: + return getConsoleEscapeSequence(Attributes::BOLD, Color::YELLOW); + case LogLevel::INFO: + return getConsoleEscapeSequence(Attributes::BOLD, Color::BLUE); + case LogLevel::DEBUG: + return getConsoleEscapeSequence(Attributes::DEFAULT, Color::GREEN); + case LogLevel::TRACE: + return getConsoleEscapeSequence(Attributes::DEFAULT, Color::BLACK); + default: + return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT); + } +} + +std::string LogFormatter::getDefaultConsoleColor(void) +{ + return getConsoleEscapeSequence(Attributes::DEFAULT, Color::DEFAULT); +} + +std::string LogFormatter::stripProjectDir(const std::string& file) +{ + const std::string SOURCE_DIR = PROJECT_SOURCE_DIR "/"; + // it will work until someone use in cmake FILE(GLOB ... RELATIVE ...) + assert(0 == file.compare(0, SOURCE_DIR.size(), SOURCE_DIR)); + return file.substr(SOURCE_DIR.size()); +} + +std::string LogFormatter::getHeader(LogLevel logLevel, + const std::string& file, + const unsigned int& line, + const std::string& func) +{ + std::ostringstream logLine; + logLine << getCurrentTime() << ' ' + << std::left << std::setw(SEVERITY_COLUMN_LENGTH) << '[' + toString(logLevel) + ']' + << std::right << std::setw(THREAD_COLUMN_LENGTH) << getCurrentThread() << ": " + << std::left << std::setw(FILE_COLUMN_LENGTH) + << file + ':' + std::to_string(line) + ' ' + func + ':'; + return logLine.str(); +} + +} // namespace logger diff --git a/src/logger/formatter.hpp b/src/logger/formatter.hpp new file mode 100644 index 0000000..4325810 --- /dev/null +++ b/src/logger/formatter.hpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Dariusz Michaluk + * + * 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 Helper formatter for logger + */ + +#ifndef LOGGER_FORMATTER_HPP +#define LOGGER_FORMATTER_HPP + +#include "logger/level.hpp" + +#include + +namespace logger { + +class LogFormatter { +public: + static unsigned int getCurrentThread(void); + static std::string getCurrentTime(void); + static std::string getConsoleColor(LogLevel logLevel); + static std::string getDefaultConsoleColor(void); + static std::string stripProjectDir(const std::string& file); + static std::string getHeader(LogLevel logLevel, + const std::string& file, + const unsigned int& line, + const std::string& func); +}; + +} // namespace logger + +#endif // LOGGER_FORMATTER_HPP diff --git a/src/logger/level.cpp b/src/logger/level.cpp new file mode 100644 index 0000000..11d3bcd --- /dev/null +++ b/src/logger/level.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Jan Olszak + * + * 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 Jan Olszak (j.olszak@samsung.com) + * @brief Functions to handle LogLevel + */ + +#include "logger/config.hpp" +#include "logger/level.hpp" + +#include +#include + +namespace logger { + +LogLevel parseLogLevel(const std::string& level) +{ + if (boost::iequals(level, "ERROR")) { + return LogLevel::ERROR; + } else if (boost::iequals(level, "WARN")) { + return LogLevel::WARN; + } else if (boost::iequals(level, "INFO")) { + return LogLevel::INFO; + } else if (boost::iequals(level, "DEBUG")) { + return LogLevel::DEBUG; + } else if (boost::iequals(level, "TRACE")) { + return LogLevel::TRACE; + } else { + throw std::runtime_error("Invalid LogLevel to parse"); + } +} + +std::string toString(const LogLevel logLevel) +{ + switch (logLevel) { + case LogLevel::ERROR: + return "ERROR"; + case LogLevel::WARN: + return "WARN"; + case LogLevel::INFO: + return "INFO"; + case LogLevel::DEBUG: + return "DEBUG"; + case LogLevel::TRACE: + return "TRACE"; + default: + return "UNKNOWN"; + } +} +} // namespace logger diff --git a/src/logger/level.hpp b/src/logger/level.hpp new file mode 100644 index 0000000..2247bab --- /dev/null +++ b/src/logger/level.hpp @@ -0,0 +1,54 @@ +/* + * 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 LogLevel + */ + +#ifndef LOGGER_LEVEL_HPP +#define LOGGER_LEVEL_HPP + +#include + +namespace logger { + +enum class LogLevel { + TRACE, + DEBUG, + INFO, + WARN, + ERROR +}; + +/** + * @param logLevel LogLevel + * @return std::sting representation of the LogLevel value + */ +std::string toString(const LogLevel logLevel); + +/** + * @param level string representation of log level + * @return parsed LogLevel value + */ +LogLevel parseLogLevel(const std::string& level); + +} // namespace logger + +#endif // LOGGER_LEVEL_HPP diff --git a/src/logger/logger.cpp b/src/logger/logger.cpp new file mode 100644 index 0000000..a3bcf70 --- /dev/null +++ b/src/logger/logger.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Pawel Broda + * + * 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 Pawel Broda (p.broda@partner.samsung.com) + * @brief Logger + */ + +#include "logger/config.hpp" +#include "logger/logger.hpp" +#include "logger/formatter.hpp" +#include "logger/backend-null.hpp" + +#include +#include + +namespace logger { + +namespace { + +volatile LogLevel gLogLevel = LogLevel::DEBUG; +std::unique_ptr gLogBackendPtr(new NullLogger()); +std::mutex gLogMutex; + +} // namespace + +Logger::Logger(LogLevel logLevel, + const std::string& file, + const unsigned int line, + const std::string& func) + : mLogLevel(logLevel), + mFile(LogFormatter::stripProjectDir(file)), + mLine(line), + mFunc(func) +{ +} + +void Logger::logMessage(const std::string& message) +{ + std::unique_lock lock(gLogMutex); + gLogBackendPtr->log(mLogLevel, mFile, mLine, mFunc, message); +} + +void Logger::setLogLevel(const LogLevel level) +{ + gLogLevel = level; +} + +void Logger::setLogLevel(const std::string& level) +{ + gLogLevel = parseLogLevel(level); +} + +LogLevel Logger::getLogLevel(void) +{ + return gLogLevel; +} + +void Logger::setLogBackend(LogBackend* pBackend) +{ + std::unique_lock lock(gLogMutex); + gLogBackendPtr.reset(pBackend); +} + +} // namespace logger diff --git a/src/logger/logger.hpp b/src/logger/logger.hpp new file mode 100644 index 0000000..a155e27 --- /dev/null +++ b/src/logger/logger.hpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Jan Olszak + * + * 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 Jan Olszak (j.olszak@samsung.com) + * @brief Logger + */ + +#ifndef LOGGER_LOGGER_HPP +#define LOGGER_LOGGER_HPP + +#include "logger/level.hpp" + +#include +#include + +namespace logger { + +class LogBackend; + +class Logger { +public: + Logger(LogLevel logLevel, + const std::string& file, + const unsigned int line, + const std::string& func); + + void logMessage(const std::string& message); + + static void setLogLevel(const LogLevel level); + static void setLogLevel(const std::string& level); + static LogLevel getLogLevel(void); + static void setLogBackend(LogBackend* pBackend); + +private: + LogLevel mLogLevel; + std::string mFile; + unsigned int mLine; + std::string mFunc; +}; + +} // namespace logger + +#define LOG(SEVERITY, MESSAGE) \ + do { \ + if (logger::Logger::getLogLevel() <= \ + logger::LogLevel::SEVERITY) { \ + std::ostringstream messageStream__; \ + messageStream__ << MESSAGE; \ + logger::Logger logger(logger::LogLevel::SEVERITY, \ + __FILE__, \ + __LINE__, \ + __func__); \ + logger.logMessage(messageStream__.str()); \ + } \ + } while(0) + +#define LOGE(MESSAGE) LOG(ERROR, MESSAGE) +#define LOGW(MESSAGE) LOG(WARN, MESSAGE) +#define LOGI(MESSAGE) LOG(INFO, MESSAGE) +#define LOGD(MESSAGE) LOG(DEBUG, MESSAGE) +#define LOGT(MESSAGE) LOG(TRACE, MESSAGE) + +#endif // LOGGER_LOGGER_HPP -- 2.7.4