Included vasum logger class. 88/44088/1
authorRomanKubiak <r.kubiak@samsung.com>
Thu, 16 Jul 2015 14:56:05 +0000 (16:56 +0200)
committerRomanKubiak <r.kubiak@samsung.com>
Thu, 16 Jul 2015 15:03:03 +0000 (17:03 +0200)
Some modifications
- added an option to disable colours in stderr logger
- added a syslog backend if journal is not available
- added a file backend

Change-Id: Id6ed1c56f871be8970879277b331b26d0e3969f3

21 files changed:
include/logger/backend-file.hpp [new file with mode: 0644]
include/logger/backend-journal.hpp [new file with mode: 0644]
include/logger/backend-null.hpp [new file with mode: 0644]
include/logger/backend-stderr.hpp [new file with mode: 0644]
include/logger/backend-syslog.hpp [new file with mode: 0644]
include/logger/backend.hpp [new file with mode: 0644]
include/logger/ccolor.hpp [new file with mode: 0644]
include/logger/config.hpp [new file with mode: 0644]
include/logger/formatter.hpp [new file with mode: 0644]
include/logger/level.hpp [new file with mode: 0644]
include/logger/logger-scope.hpp [new file with mode: 0644]
include/logger/logger.hpp [new file with mode: 0644]
src/logger/backend-file.cpp [new file with mode: 0644]
src/logger/backend-journal.cpp [new file with mode: 0644]
src/logger/backend-stderr.cpp [new file with mode: 0644]
src/logger/backend-syslog.cpp [new file with mode: 0644]
src/logger/ccolor.cpp [new file with mode: 0644]
src/logger/formatter.cpp [new file with mode: 0644]
src/logger/level.cpp [new file with mode: 0644]
src/logger/logger-scope.cpp [new file with mode: 0644]
src/logger/logger.cpp [new file with mode: 0644]

diff --git a/include/logger/backend-file.hpp b/include/logger/backend-file.hpp
new file mode 100644 (file)
index 0000000..9a108d1
--- /dev/null
@@ -0,0 +1,22 @@
+#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) : filePath(_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 filePath;
+};
+
+} // namespace logger
+
+#endif
diff --git a/include/logger/backend-journal.hpp b/include/logger/backend-journal.hpp
new file mode 100644 (file)
index 0000000..e566ed1
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ *  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_LOGGER_BACKEND_JOURNAL_HPP
+#define COMMON_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 // COMMON_LOGGER_BACKEND_JOURNAL_HPP
diff --git a/include/logger/backend-null.hpp b/include/logger/backend-null.hpp
new file mode 100644 (file)
index 0000000..4a7e8a9
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Pawel Broda <p.broda@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  Pawel Broda (p.broda@partner.samsung.com)
+ * @brief   Null backend for logger
+ */
+
+#ifndef COMMON_LOGGER_BACKEND_NULL_HPP
+#define COMMON_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 // COMMON_LOGGER_BACKEND_NULL_HPP
diff --git a/include/logger/backend-stderr.hpp b/include/logger/backend-stderr.hpp
new file mode 100644 (file)
index 0000000..919e24e
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Pawel Broda <p.broda@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  Pawel Broda (p.broda@partner.samsung.com)
+ * @brief   Stderr backend for logger
+ */
+
+#ifndef COMMON_LOGGER_BACKEND_STDERR_HPP
+#define COMMON_LOGGER_BACKEND_STDERR_HPP
+
+#include "logger/backend.hpp"
+
+namespace logger {
+
+/**
+ * Stderr logging backend
+ */
+class StderrBackend : public LogBackend {
+public:
+    StderrBackend(const bool _useColours=true) : useColours(_useColours) {}
+    void log(LogLevel logLevel,
+             const std::string& file,
+             const unsigned int& line,
+             const std::string& func,
+             const std::string& message) override;
+private:
+    bool useColours;
+};
+
+} // namespace logger
+
+#endif // COMMON_LOGGER_BACKEND_STDERR_HPP
diff --git a/include/logger/backend-syslog.hpp b/include/logger/backend-syslog.hpp
new file mode 100644 (file)
index 0000000..23450b5
--- /dev/null
@@ -0,0 +1,19 @@
+#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
diff --git a/include/logger/backend.hpp b/include/logger/backend.hpp
new file mode 100644 (file)
index 0000000..99b0c49
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Pawel Broda <p.broda@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  Pawel Broda (p.broda@partner.samsung.com)
+ * @brief   Logging backend
+ */
+
+#ifndef COMMON_LOGGER_BACKEND_HPP
+#define COMMON_LOGGER_BACKEND_HPP
+
+#include "logger/level.hpp"
+
+#include <string>
+
+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 // COMMON_LOGGER_BACKEND_HPP
diff --git a/include/logger/ccolor.hpp b/include/logger/ccolor.hpp
new file mode 100644 (file)
index 0000000..47cc25e
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ *  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   Console color for StderrBackend logger
+ */
+
+#ifndef COMMON_LOGGER_CCOLOR_HPP
+#define COMMON_LOGGER_CCOLOR_HPP
+
+#include <string>
+
+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 // COMMON_LOGGER_CCOLOR_HPP
diff --git a/include/logger/config.hpp b/include/logger/config.hpp
new file mode 100644 (file)
index 0000000..753430e
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ *  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 COMMON_CONFIG_HPP
+#define COMMON_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
+#define steady_clock monotonic_clock
+#endif // GCC_VERSION < 40700
+
+#endif // GCC_VERSION
+
+// Variadic macros support for boost preprocessor should be enabled
+// manually for clang since they are marked as untested feature
+// (boost trunk if fixed but the latest 1.55 version is not,
+// see boost/preprocessor/config/config.hpp)
+#ifdef __clang__
+#define BOOST_PP_VARIADICS 1
+#endif
+
+// This has to be defined always when the boost has not been compiled
+// using C++11. Headers detect that you are compiling using C++11 and
+// blindly and wrongly assume that boost has been as well.
+#ifndef BOOST_NO_CXX11_SCOPED_ENUMS
+#define BOOST_NO_CXX11_SCOPED_ENUMS 1
+#endif
+
+#endif // COMMON_CONFIG_HPP
diff --git a/include/logger/formatter.hpp b/include/logger/formatter.hpp
new file mode 100644 (file)
index 0000000..3af0763
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ *  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   Helper formatter for logger
+ */
+
+#ifndef COMMON_LOGGER_FORMATTER_HPP
+#define COMMON_LOGGER_FORMATTER_HPP
+
+#include "logger/level.hpp"
+
+#include <string>
+
+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,
+                                       const std::string& rootDir);
+    static std::string getHeader(LogLevel logLevel,
+                                 const std::string& file,
+                                 const unsigned int& line,
+                                 const std::string& func);
+};
+
+} // namespace logger
+
+#endif // COMMON_LOGGER_FORMATTER_HPP
diff --git a/include/logger/level.hpp b/include/logger/level.hpp
new file mode 100644 (file)
index 0000000..7902301
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ *  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 COMMON_LOGGER_LEVEL_HPP
+#define COMMON_LOGGER_LEVEL_HPP
+
+#include <string>
+
+namespace logger {
+
+enum class LogLevel {
+    TRACE,
+    DEBUG,
+    INFO,
+    WARN,
+    ERROR,
+    HELP
+};
+
+/**
+ * @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 // COMMON_LOGGER_LEVEL_HPP
diff --git a/include/logger/logger-scope.hpp b/include/logger/logger-scope.hpp
new file mode 100644 (file)
index 0000000..cefd912
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Lukasz Kostyra <l.kostyra@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 Kostyra (l.kostyra@samsung.com)
+ * @brief   Scope logger class declaration
+ */
+
+#ifndef COMMON_LOGGER_LOGGER_SCOPE_HPP
+#define COMMON_LOGGER_LOGGER_SCOPE_HPP
+
+#include <string>
+#include <sstream>
+
+namespace logger {
+
+class SStreamWrapper
+{
+public:
+    operator std::string() const;
+
+    template <typename T>
+    SStreamWrapper& operator<<(const T& b)
+    {
+        this->mSStream << b;
+        return *this;
+    }
+
+private:
+    std::ostringstream mSStream;
+};
+
+/**
+ * Class specifically for scope debug logging. Should be used at the beggining of a scope.
+ * Constructor marks scope enterance, destructor marks scope leave.
+ */
+class LoggerScope
+{
+public:
+    LoggerScope(const std::string& file,
+                const unsigned int line,
+                const std::string& func,
+                const std::string& message,
+                const std::string& rootDir);
+    ~LoggerScope();
+
+private:
+    const std::string mFile;
+    const unsigned int mLine;
+    const std::string mFunc;
+    const std::string mMessage;
+    const std::string mRootDir;
+};
+
+} // namespace logger
+
+// macro to automatically create LoggerScope object
+#define LOGS(MSG)   logger::LoggerScope logScopeObj(__FILE__, __LINE__, __func__,    \
+                                                    logger::SStreamWrapper() << MSG, \
+                                                    PROJECT_SOURCE_DIR)
+
+#endif // COMMON_LOGGER_LOGGER_SCOPE_HPP
diff --git a/include/logger/logger.hpp b/include/logger/logger.hpp
new file mode 100644 (file)
index 0000000..d596a20
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Jan Olszak <j.olszak@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  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Logger
+ */
+
+#ifndef COMMON_LOGGER_LOGGER_HPP
+#define COMMON_LOGGER_LOGGER_HPP
+
+#include "logger/level.hpp"
+#include "logger/backend-file.hpp"
+#include "logger/backend-stderr.hpp"
+
+#include <sstream>
+#include <string>
+
+#ifndef PROJECT_SOURCE_DIR
+#define PROJECT_SOURCE_DIR ""
+#endif
+
+namespace logger {
+
+class LogBackend;
+
+class Logger {
+public:
+    static void logMessage(LogLevel logLevel,
+                           const std::string& message,
+                           const std::string& file,
+                           const unsigned int line,
+                           const std::string& func,
+                           const std::string& rootDir);
+
+    static void setLogLevel(const LogLevel level);
+    static void setLogLevel(const std::string& level);
+    static LogLevel getLogLevel(void);
+    static void setLogBackend(LogBackend* pBackend);
+};
+
+} // namespace logger
+
+#define LOG(SEVERITY, MESSAGE)                                             \
+    do {                                                                   \
+        if (logger::Logger::getLogLevel() <= logger::LogLevel::SEVERITY) { \
+            std::ostringstream messageStream__;                            \
+            messageStream__ << MESSAGE;                                    \
+            logger::Logger::logMessage(logger::LogLevel::SEVERITY,         \
+                                       messageStream__.str(),              \
+                                       __FILE__,                           \
+                                       __LINE__,                           \
+                                       __func__,                           \
+                                       PROJECT_SOURCE_DIR);                \
+        }                                                                  \
+    } while (0)
+
+#define LOGE(MESSAGE) LOG(ERROR, MESSAGE)
+#define LOGW(MESSAGE) LOG(WARN, MESSAGE)
+#define LOGI(MESSAGE) LOG(INFO, MESSAGE)
+#if defined(_DEBUG)
+#define LOGD(MESSAGE) LOG(DEBUG, MESSAGE)
+#else
+#define LOGD(MESSAGE) do {} while (0)
+#endif
+#define LOGH(MESSAGE) LOG(HELP, MESSAGE)
+#define LOGT(MESSAGE) LOG(TRACE, MESSAGE)
+
+#endif // COMMON_LOGGER_LOGGER_HPP
diff --git a/src/logger/backend-file.cpp b/src/logger/backend-file.cpp
new file mode 100644 (file)
index 0000000..fcfd1bf
--- /dev/null
@@ -0,0 +1,22 @@
+#include "logger/config.hpp"
+#include "logger/formatter.hpp"
+#include "logger/backend-file.hpp"
+
+#include <fstream>
+
+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(filePath, std::ios::app);
+    out << LogFormatter::getHeader(logLevel, file, line, func);
+    out << message;
+    out << "\n";
+}
+
+
+}
diff --git a/src/logger/backend-journal.cpp b/src/logger/backend-journal.cpp
new file mode 100644 (file)
index 0000000..85cb46f
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ *  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
+ */
+#ifdef HAVE_SYSTEMD_JOURNAL
+#include "logger/config.hpp"
+#include "logger/backend-journal.hpp"
+
+#define SD_JOURNAL_SUPPRESS_LOCATION
+#include <systemd/sd-journal.h>
+
+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
+    case LogLevel::HELP:
+        return LOG_DEBUG;   // 7
+    default:
+        return LOG_DEBUG;   // 7
+    }
+}
+
+} // 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
+#endif
\ No newline at end of file
diff --git a/src/logger/backend-stderr.cpp b/src/logger/backend-stderr.cpp
new file mode 100644 (file)
index 0000000..36c71ac
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Pawel Broda <p.broda@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  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 <boost/tokenizer.hpp>
+
+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<char> charSeparator;
+    typedef boost::tokenizer<charSeparator> 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",
+                    useColours ? logColor.c_str() : "",
+                    header.c_str(),
+                    messageLine.c_str(),
+                    useColours ? defaultColor.c_str() : "");
+        }
+    }
+}
+
+} // namespace logger
diff --git a/src/logger/backend-syslog.cpp b/src/logger/backend-syslog.cpp
new file mode 100644 (file)
index 0000000..0542e46
--- /dev/null
@@ -0,0 +1,42 @@
+#include "logger/config.hpp"
+#include "logger/formatter.hpp"
+#include "logger/backend-syslog.hpp"
+
+#include <syslog.h>
+#include <sstream>
+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());
+}
+
+}
diff --git a/src/logger/ccolor.cpp b/src/logger/ccolor.cpp
new file mode 100644 (file)
index 0000000..9cc652d
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ *  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   Console color for StderrBackend logger
+ */
+
+#include "logger/config.hpp"
+#include "logger/ccolor.hpp"
+
+#include <stdio.h>
+
+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[%u;%um", 0x1B, (unsigned int)attr, (unsigned int)color);
+    return std::string(command);
+}
+
+} // namespace logger
diff --git a/src/logger/formatter.cpp b/src/logger/formatter.cpp
new file mode 100644 (file)
index 0000000..815a111
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ *  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   Helper formatter for logger
+ */
+
+#include "logger/config.hpp"
+#include "logger/formatter.hpp"
+#include "logger/ccolor.hpp"
+
+#include <sys/time.h>
+#include <cassert>
+#include <sstream>
+#include <iomanip>
+#include <thread>
+#include <atomic>
+
+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<unsigned int> 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);
+    case LogLevel::HELP:
+        return getConsoleEscapeSequence(Attributes::BOLD, Color::MAGENTA);
+    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& rootDir)
+{
+    // If rootdir is empty then return full name
+    if (rootDir.empty()) {
+        return file;
+    }
+    const std::string sourceDir = rootDir + "/";
+    // If file does not belong to rootDir then also return full name
+    if (0 != file.compare(0, sourceDir.size(), sourceDir)) {
+        return file;
+    }
+    return file.substr(sourceDir.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/level.cpp b/src/logger/level.cpp
new file mode 100644 (file)
index 0000000..3b74205
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Jan Olszak <j.olszak@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  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Functions to handle LogLevel
+ */
+
+#include "logger/config.hpp"
+#include "logger/level.hpp"
+
+#include <stdexcept>
+#include <boost/algorithm/string.hpp>
+
+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 if (boost::iequals(level, "HELP")) {
+        return LogLevel::HELP;
+    } 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";
+    case LogLevel::HELP:
+        return "HELP";
+    default:
+        return "UNKNOWN";
+    }
+}
+} // namespace logger
diff --git a/src/logger/logger-scope.cpp b/src/logger/logger-scope.cpp
new file mode 100644 (file)
index 0000000..a977adc
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Lukasz Kostyra <l.kostyra@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 Kostyra (l.kostyra@samsung.com)
+ * @brief   Scope logger class implementation
+ */
+
+#include "logger/logger-scope.hpp"
+#include "logger/logger.hpp"
+
+namespace logger {
+
+SStreamWrapper::operator std::string() const
+{
+    return mSStream.str();
+}
+
+LoggerScope::LoggerScope(const std::string& file,
+                         const unsigned int line,
+                         const std::string& func,
+                         const std::string& message,
+                         const std::string& rootDir):
+        mFile(file),
+        mLine(line),
+        mFunc(func),
+        mMessage(message),
+        mRootDir(rootDir)
+{
+    if (logger::Logger::getLogLevel() <= logger::LogLevel::TRACE) {
+        logger::Logger::logMessage(logger::LogLevel::TRACE, "Entering: " + mMessage,
+                                   mFile, mLine, mFunc, mRootDir);
+    }
+}
+
+LoggerScope::~LoggerScope()
+{
+    if (logger::Logger::getLogLevel() <= logger::LogLevel::TRACE) {
+        logger::Logger::logMessage(logger::LogLevel::TRACE, "Leaving:  " + mMessage,
+                                   mFile, mLine, mFunc, mRootDir);
+    }
+}
+
+} // namespace logger
diff --git a/src/logger/logger.cpp b/src/logger/logger.cpp
new file mode 100644 (file)
index 0000000..ec0855b
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Pawel Broda <p.broda@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  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 <memory>
+#include <mutex>
+
+namespace logger {
+
+namespace {
+
+volatile LogLevel gLogLevel = LogLevel::DEBUG;
+std::unique_ptr<LogBackend> gLogBackendPtr(new NullLogger());
+std::mutex gLogMutex;
+
+} // namespace
+
+void Logger::logMessage(LogLevel logLevel,
+                        const std::string& message,
+                        const std::string& file,
+                        const unsigned int line,
+                        const std::string& func,
+                        const std::string& rootDir)
+{
+    std::string sfile = LogFormatter::stripProjectDir(file, rootDir);
+    std::unique_lock<std::mutex> lock(gLogMutex);
+    gLogBackendPtr->log(logLevel, sfile, line, func, 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<std::mutex> lock(gLogMutex);
+    gLogBackendPtr.reset(pBackend);
+}
+
+} // namespace logger