Added file and syslog backends 72/44072/8
authorRomanKubiak <r.kubiak@samsung.com>
Thu, 16 Jul 2015 11:43:08 +0000 (13:43 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Tue, 21 Jul 2015 07:49:24 +0000 (00:49 -0700)
[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
libs/logger/backend-file.cpp [new file with mode: 0644]
libs/logger/backend-file.hpp [new file with mode: 0644]
libs/logger/backend-journal.cpp
libs/logger/backend-stderr.cpp
libs/logger/backend-stderr.hpp
libs/logger/backend-syslog.cpp [new file with mode: 0644]
libs/logger/backend-syslog.hpp [new file with mode: 0644]
libs/logger/logger.hpp

index 6b3352d..d1e82eb 100644 (file)
@@ -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 (file)
index 0000000..45888ff
--- /dev/null
@@ -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 <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(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 (file)
index 0000000..16bab1c
--- /dev/null
@@ -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
index 74fc581..af147cd 100644 (file)
@@ -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
index c0d3a3c..f063383 100644 (file)
@@ -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() : "");
         }
     }
 }
index 4cdd0ec..efb39fe 100644 (file)
@@ -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 (file)
index 0000000..2686222
--- /dev/null
@@ -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 <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());
+}
+
+} // namespace logger
diff --git a/libs/logger/backend-syslog.hpp b/libs/logger/backend-syslog.hpp
new file mode 100644 (file)
index 0000000..2c9fde6
--- /dev/null
@@ -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
index 8d8d433..3a0dee4 100644 (file)
@@ -26,6 +26,8 @@
 #define COMMON_LOGGER_LOGGER_HPP
 
 #include "logger/level.hpp"
+#include "logger/backend-file.hpp"
+#include "logger/backend-stderr.hpp"
 
 #include <sstream>
 #include <string>