Add journald logging and refactor log system
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 3 Nov 2014 11:28:33 +0000 (12:28 +0100)
committerMaciej J. Karpiuk <m.karpiuk2@samsung.com>
Tue, 17 Feb 2015 10:00:04 +0000 (11:00 +0100)
Add journald log provider. Simplify log system and log providers. Add support
for environment flags.

[Verification] Compile and run different build configurations with different
CKM_LOG_LEVEL and CKM_LOG_PROVIDER values. For client apps just export desired
log level/provider as CKM_LOG_LEVEL/PROVIDER env variable and run it. For ckm
service use:

'echo "CKM_LOG_LEVEL=X" > /etc/sysconfig/central-key-manager'
'echo "CKM_LOG_PROVIDER=X" >> /etc/sysconfig/central-key-manager'

and restart ckm service.

Supported CKM_LOG_LEVEL values: <0,5>
Supported CKM_LOG_PROVIDER values: {CONSOLE, DLOG, JOURNALD }

Observe logs.

1.Release build
 -by default (CKM_LOG_LEVEL not set) only errors should be logged
 -by default (CKM_LOG_PROVIDER not set) dlog is used
 -logs can be disabled completely by setting CKM_LOG_LEVEL to 0
2.Debug build
 -by default Debug and higher severity logs should be logged
 -by default (CKM_LOG_PROVIDER not set) dlog is used
 -log level can be adjusted to any level <0,5>

Change-Id: I496d12309360354399cf5011680f081ce42ee58f

15 files changed:
packaging/key-manager.spec
src/manager/CMakeLists.txt
src/manager/dpl/core/src/assert.cpp
src/manager/dpl/core/src/exception.cpp
src/manager/dpl/log/include/dpl/log/abstract_log_provider.h
src/manager/dpl/log/include/dpl/log/dlog_log_provider.h
src/manager/dpl/log/include/dpl/log/journal_log_provider.h [new file with mode: 0644]
src/manager/dpl/log/include/dpl/log/log.h
src/manager/dpl/log/include/dpl/log/old_style_log_provider.h
src/manager/dpl/log/src/abstract_log_provider.cpp
src/manager/dpl/log/src/dlog_log_provider.cpp
src/manager/dpl/log/src/journal_log_provider.cpp [new file with mode: 0644]
src/manager/dpl/log/src/log.cpp
src/manager/dpl/log/src/old_style_log_provider.cpp
systemd/central-key-manager.service

index 5925b09..9bb7822 100644 (file)
@@ -18,6 +18,7 @@ BuildRequires: pkgconfig(libsmack)
 BuildRequires: pkgconfig(libsystemd-daemon)
 BuildRequires: pkgconfig(db-util)
 BuildRequires: pkgconfig(vconf)
+BuildRequires: pkgconfig(libsystemd-journal)
 BuildRequires: boost-devel
 Requires: libkey-manager-common = %{version}-%{release}
 %{?systemd_requires}
index a6b7526..74589ad 100644 (file)
@@ -5,6 +5,7 @@ PKG_CHECK_MODULES(COMMON_DEP
     db-util
     libsmack
     libcrypto
+    libsystemd-journal
     )
 
 SET(KEY_MANAGER_COMMON_VERSION_MAJOR 1)
@@ -28,6 +29,7 @@ SET(COMMON_SOURCES
     ${COMMON_PATH}/dpl/log/src/dlog_log_provider.cpp
     ${COMMON_PATH}/dpl/log/src/log.cpp
     ${COMMON_PATH}/dpl/log/src/old_style_log_provider.cpp
+    ${COMMON_PATH}/dpl/log/src/journal_log_provider.cpp
     ${COMMON_PATH}/dpl/core/src/assert.cpp
     ${COMMON_PATH}/dpl/core/src/binary_queue.cpp
     ${COMMON_PATH}/dpl/core/src/colors.cpp
index 6a19fa9..43898bd 100644 (file)
@@ -37,7 +37,8 @@ void AssertProc(const char *condition,
     {                                                                  \
         std::ostringstream platformLog;                                \
         platformLog << message;                                        \
-        CKM::Log::LogSystemSingleton::Instance().Pedantic(             \
+        CKM::Log::LogSystemSingleton::Instance().Log(                  \
+            CKM::Log::AbstractLogProvider::LogLevel::Pedantic,         \
             platformLog.str().c_str(),                                 \
             __FILE__, __LINE__, __FUNCTION__);                         \
     } \
index 0def9bf..792c97f 100644 (file)
@@ -51,7 +51,10 @@ void LogUnhandledException(const std::string &str,
     printf("%s\n", msg.str().c_str());
 
     // Logging to dlog
-    CKM::Log::LogSystemSingleton::Instance().Error(
-        str.c_str(), filename, line, function);
+    CKM::Log::LogSystemSingleton::Instance().Log(CKM::Log::AbstractLogProvider::LogLevel::Error,
+                                                 str.c_str(),
+                                                 filename,
+                                                 line,
+                                                 function);
 }
 } // namespace CKM
index e4aacc5..4960e37 100644 (file)
@@ -27,30 +27,25 @@ namespace Log {
 class AbstractLogProvider
 {
   public:
+    enum class LogLevel {
+        None,
+        Error,
+        Warning,
+        Info,
+        Debug,
+        Pedantic
+    };
+
     virtual ~AbstractLogProvider() {}
 
     virtual void SetTag(const char *tag);
 
-    virtual void Debug(const char *message,
-                       const char *fileName,
-                       int line,
-                       const char *function) = 0;
-    virtual void Info(const char *message,
-                      const char *fileName,
-                      int line,
-                      const char *function) = 0;
-    virtual void Warning(const char *message,
-                         const char *fileName,
-                         int line,
-                         const char *function) = 0;
-    virtual void Error(const char *message,
-                       const char *fileName,
-                       int line,
-                       const char *function) = 0;
-    virtual void Pedantic(const char *message,
-                          const char *fileName,
-                          int line,
-                          const char *function) = 0;
+    virtual void Log(LogLevel level,
+                     const char *message,
+                     const char *fileName,
+                     int line,
+                     const char *function) const = 0;
+
   protected:
     static const char *LocateSourceFileName(const char *filename);
 };
index 384c191..b594aa7 100644 (file)
 
 #include <dpl/log/abstract_log_provider.h>
 #include <memory>
-#include <string>
 
 namespace CKM {
 namespace Log {
-class DLOGLogProvider :
-    public AbstractLogProvider
+class DLOGLogProvider : public AbstractLogProvider
 {
-  private:
-    std::unique_ptr<char[]> m_tag;
-
-    static std::string FormatMessage(const char *message,
-                                     const char *filename,
-                                     int line,
-                                     const char *function);
-
   public:
     DLOGLogProvider();
     virtual ~DLOGLogProvider();
 
-    virtual void Debug(const char *message,
-                       const char *fileName,
-                       int line,
-                       const char *function);
-    virtual void Info(const char *message,
-                      const char *fileName,
-                      int line,
-                      const char *function);
-    virtual void Warning(const char *message,
-                         const char *fileName,
-                         int line,
-                         const char *function);
-    virtual void Error(const char *message,
-                       const char *fileName,
-                       int line,
-                       const char *function);
-    virtual void Pedantic(const char *message,
-                          const char *fileName,
-                          int line,
-                          const char *function);
+    virtual void Log(AbstractLogProvider::LogLevel level,
+                     const char *message,
+                     const char *fileName,
+                     int line,
+                     const char *function) const;
+
     // Set global Tag according to DLOG
     void SetTag(const char *tag);
+
+  private:
+    std::unique_ptr<char[]> m_tag;
 };
 
 } // namespace Log
diff --git a/src/manager/dpl/log/include/dpl/log/journal_log_provider.h b/src/manager/dpl/log/include/dpl/log/journal_log_provider.h
new file mode 100644 (file)
index 0000000..08f56bc
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ *  Copyright (c) 2000 - 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       journal_log_provider.h
+ * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version    1.0
+ */
+
+#pragma once
+
+#include <dpl/log/abstract_log_provider.h>
+
+namespace CKM {
+namespace Log {
+
+class JournalLogProvider: public AbstractLogProvider
+{
+public:
+    JournalLogProvider();
+    virtual ~JournalLogProvider();
+
+    virtual void Log(AbstractLogProvider::LogLevel level,
+                     const char *message,
+                     const char *fileName,
+                     int line,
+                     const char *function) const;
+};
+
+} /* namespace Log */
+} /* namespace CKM */
index 7b56d82..83fd0d8 100644 (file)
@@ -32,63 +32,20 @@ namespace CKM {
 namespace Log {
 /**
  * CKM log system
- *
- * To switch logs into old style, export
- * DPL_USE_OLD_STYLE_LOGS before application start
  */
-class LogSystem :
-    private Noncopyable
+class LogSystem : private Noncopyable
 {
-  private:
-    typedef std::list<AbstractLogProvider *> AbstractLogProviderPtrList;
-    AbstractLogProviderPtrList m_providers;
-
-    bool m_isLoggingEnabled;
-
   public:
-    bool IsLoggingEnabled() const;
     LogSystem();
     virtual ~LogSystem();
 
-    /**
-     * Log debug message
-     */
-    void Debug(const char *message,
-               const char *filename,
-               int line,
-               const char *function);
+    AbstractLogProvider::LogLevel GetLogLevel() const { return m_level; }
 
-    /**
-     * Log info message
-     */
-    void Info(const char *message,
-              const char *filename,
-              int line,
-              const char *function);
-
-    /**
-     * Log warning message
-     */
-    void Warning(const char *message,
-                 const char *filename,
-                 int line,
-                 const char *function);
-
-    /**
-     * Log error message
-     */
-    void Error(const char *message,
-               const char *filename,
-               int line,
-               const char *function);
-
-    /**
-     * Log pedantic message
-     */
-    void Pedantic(const char *message,
-                  const char *filename,
-                  int line,
-                  const char *function);
+    void Log(AbstractLogProvider::LogLevel level,
+             const char *message,
+             const char *filename,
+             int line,
+             const char *function);
 
     /**
      * Set default's DLOG provider Tag
@@ -106,6 +63,11 @@ class LogSystem :
      * Remove abstract provider from providers list
      */
     void RemoveProvider(AbstractLogProvider *provider);
+
+  private:
+    typedef std::list<AbstractLogProvider *> AbstractLogProviderPtrList;
+    AbstractLogProviderPtrList m_providers;
+    AbstractLogProvider::LogLevel m_level;
 };
 
 /*
@@ -136,38 +98,50 @@ typedef Singleton<LogSystem> LogSystemSingleton;
 //
 
 /* avoid warnings about unused variables */
-#define DPL_MACRO_DUMMY_LOGGING(message, function)                         \
-    do {                                                                   \
-        CKM::Log::NullStream ns;                                \
-        ns << message;                                                     \
+#define DPL_MACRO_DUMMY_LOGGING(message, level)                                 \
+    do {                                                                        \
+        CKM::Log::NullStream ns;                                                \
+        ns << message;                                                          \
     } while (0)
 
-#define DPL_MACRO_FOR_LOGGING(message, function)                           \
-do                                                                         \
-{                                                                          \
-    if (CKM::Log::LogSystemSingleton::Instance().IsLoggingEnabled())   \
-    {                                                                      \
-        std::ostringstream platformLog;                                    \
-        platformLog << message;                                            \
-        CKM::Log::LogSystemSingleton::Instance().function(      \
-            platformLog.str().c_str(),                                     \
-            __FILE__, __LINE__, __FUNCTION__);                             \
-    }                                                                      \
+#define DPL_MACRO_FOR_LOGGING(message, level)                                   \
+do                                                                              \
+{                                                                               \
+    if (level > CKM::Log::AbstractLogProvider::LogLevel::None &&                \
+        CKM::Log::LogSystemSingleton::Instance().GetLogLevel() >= level)        \
+    {                                                                           \
+        std::ostringstream platformLog;                                         \
+        platformLog << message;                                                 \
+        CKM::Log::LogSystemSingleton::Instance().Log(level,                     \
+                                                     platformLog.str().c_str(), \
+                                                     __FILE__,                  \
+                                                     __LINE__,                  \
+                                                     __FUNCTION__);             \
+    }                                                                           \
 } while (0)
 
 /* Errors must be always logged. */
-#define  LogError(message) DPL_MACRO_FOR_LOGGING(message, Error)
+#define  LogError(message)          \
+    DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Error)
 
 #ifdef BUILD_TYPE_DEBUG
-    #define LogDebug(message) DPL_MACRO_FOR_LOGGING(message, Debug)
-    #define LogInfo(message) DPL_MACRO_FOR_LOGGING(message, Info)
-    #define LogWarning(message) DPL_MACRO_FOR_LOGGING(message, Warning)
-    #define LogPedantic(message) DPL_MACRO_FOR_LOGGING(message, Pedantic)
+    #define LogDebug(message)       \
+        DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Debug)
+    #define LogInfo(message)        \
+        DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Info)
+    #define LogWarning(message)     \
+        DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Warning)
+    #define LogPedantic(message)    \
+        DPL_MACRO_FOR_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Pedantic)
 #else
-    #define LogDebug(message) DPL_MACRO_DUMMY_LOGGING(message, Debug)
-    #define LogInfo(message) DPL_MACRO_DUMMY_LOGGING(message, Info)
-    #define LogWarning(message) DPL_MACRO_DUMMY_LOGGING(message, Warning)
-    #define LogPedantic(message) DPL_MACRO_DUMMY_LOGGING(message, Pedantic)
+    #define LogDebug(message)       \
+        DPL_MACRO_DUMMY_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Debug)
+    #define LogInfo(message)        \
+        DPL_MACRO_DUMMY_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Info)
+    #define LogWarning(message)     \
+        DPL_MACRO_DUMMY_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Warning)
+    #define LogPedantic(message)    \
+        DPL_MACRO_DUMMY_LOGGING(message, CKM::Log::AbstractLogProvider::LogLevel::Pedantic)
 #endif // BUILD_TYPE_DEBUG
 
 #endif // CENT_KEY_LOG_H
index 08dc29f..57f7ebe 100644 (file)
 #define CENT_KEY_OLD_STYLE_LOG_PROVIDER_H
 
 #include <dpl/log/abstract_log_provider.h>
-#include <string>
 
 namespace CKM {
 namespace Log {
-class OldStyleLogProvider :
-    public AbstractLogProvider
+class OldStyleLogProvider : public AbstractLogProvider
 {
-  private:
-    bool m_showDebug;
-    bool m_showInfo;
-    bool m_showWarning;
-    bool m_showError;
-    bool m_showPedantic;
-    bool m_printStdErr;
-
-    static std::string FormatMessage(const char *message,
-                                     const char *filename,
-                                     int line,
-                                     const char *function);
-
   public:
-    OldStyleLogProvider(bool showDebug,
-                        bool showInfo,
-                        bool showWarning,
-                        bool showError,
-                        bool showPedantic);
-    OldStyleLogProvider(bool showDebug,
-                        bool showInfo,
-                        bool showWarning,
-                        bool showError,
-                        bool showPedantic,
-                        bool printStdErr);
+    OldStyleLogProvider();
     virtual ~OldStyleLogProvider() {}
 
-    virtual void Debug(const char *message,
-                       const char *fileName,
-                       int line,
-                       const char *function);
-    virtual void Info(const char *message,
-                      const char *fileName,
-                      int line,
-                      const char *function);
-    virtual void Warning(const char *message,
-                         const char *fileName,
-                         int line,
-                         const char *function);
-    virtual void Error(const char *message,
-                       const char *fileName,
-                       int line,
-                       const char *function);
-    virtual void Pedantic(const char *message,
-                          const char *fileName,
-                          int line,
-                          const char *function);
+    virtual void Log(AbstractLogProvider::LogLevel level,
+                     const char *message,
+                     const char *fileName,
+                     int line,
+                     const char *function) const;
 };
 }
 } // namespace CKM
index af05449..094eeab 100644 (file)
  */
 #include <stddef.h>
 #include <dpl/log/abstract_log_provider.h>
+#include <dpl/availability.h>
 #include <cstring>
 
-#define UNUSED __attribute__((unused))
-
 namespace CKM {
 namespace Log {
 
-void AbstractLogProvider::SetTag(const char *tag UNUSED) {}
+void AbstractLogProvider::SetTag(const char *tag CKM_UNUSED) {}
 
 const char *AbstractLogProvider::LocateSourceFileName(const char *filename)
 {
index 71e0494..1b4449e 100644 (file)
 #include <dpl/log/dlog_log_provider.h>
 #include <cstring>
 #include <sstream>
+#include <stdexcept>
+#include <map>
 #include <dlog.h>
 
-#define UNUSED __attribute__((unused))
-
 namespace CKM {
 namespace Log {
-std::string DLOGLogProvider::FormatMessage(const char *message,
-                                           const char *filename,
-                                           int line,
-                                           const char *function)
-{
-    std::ostringstream val;
 
-    val << std::string("[") <<
-    LocateSourceFileName(filename) << std::string(":") << line <<
-    std::string("] ") << function << std::string("(): ") << message;
+namespace {
+typedef void (*dlogMacro)(const char*, const char*);
 
-    return val.str();
+// I can't map LOG_ values because SLOG uses token concatenation
+void error(const char* tag, const char* msg) {
+    SLOG(LOG_ERROR, tag, "%s", msg);
+}
+void warning(const char* tag, const char* msg) {
+    SLOG(LOG_WARN, tag, "%s", msg);
+}
+void info(const char* tag, const char* msg) {
+    SLOG(LOG_INFO, tag, "%s", msg);
+}
+void debug(const char* tag, const char* msg) {
+    SLOG(LOG_DEBUG, tag, "%s", msg);
 }
+void pedantic(const char* tag, const char* msg) {
+    SLOG(LOG_VERBOSE, tag, "%s", msg);
+}
+std::map<AbstractLogProvider::LogLevel, dlogMacro> dlogMacros = {
+        // [](const char* tag, const char* msg) { SLOG(LOG_ERROR, tag, "%s", msg); } won't compile
+        { AbstractLogProvider::LogLevel::Error,     error },
+        { AbstractLogProvider::LogLevel::Warning,   warning },
+        { AbstractLogProvider::LogLevel::Info,      info },
+        { AbstractLogProvider::LogLevel::Debug,     debug},
+        { AbstractLogProvider::LogLevel::Pedantic,  pedantic}
+};
+
+} // namespace anonymous
+
 
 DLOGLogProvider::DLOGLogProvider()
 {}
@@ -58,51 +76,21 @@ void DLOGLogProvider::SetTag(const char *tag)
     m_tag.reset(buff);
 }
 
-void DLOGLogProvider::Debug(const char *message,
-                            const char *filename,
-                            int line,
-                            const char *function)
+void DLOGLogProvider::Log(AbstractLogProvider::LogLevel level,
+                          const char *message,
+                          const char *fileName,
+                          int line,
+                          const char *function) const
 {
-    SLOG(LOG_DEBUG, m_tag.get(), "%s",
-        FormatMessage(message, filename, line, function).c_str());
-}
-
-void DLOGLogProvider::Info(const char *message,
-                           const char *filename,
-                           int line,
-                           const char *function)
-{
-    SLOG(LOG_INFO, m_tag.get(), "%s",
-        FormatMessage(message, filename, line, function).c_str());
-}
-
-void DLOGLogProvider::Warning(const char *message,
-                              const char *filename,
-                              int line,
-                              const char *function)
-{
-    SLOG(LOG_WARN, m_tag.get(), "%s",
-        FormatMessage(message, filename, line, function).c_str());
-}
-
-void DLOGLogProvider::Error(const char *message,
-                            const char *filename,
-                            int line,
-                            const char *function)
-{
-    SLOG(LOG_ERROR, m_tag.get(), "%s",
-        FormatMessage(message, filename, line, function).c_str());
-}
+    std::ostringstream val;
+    val << std::string("[") << LocateSourceFileName(fileName) << std::string(":") << line <<
+           std::string("] ") << function << std::string("(): ") << message;
 
-void DLOGLogProvider::Pedantic(const char *message,
-                               const char *filename,
-                               int line,
-                               const char *function)
-{
-    SLOG(LOG_DEBUG, "CKM", "%s", FormatMessage(message,
-                                              filename,
-                                              line,
-                                              function).c_str());
+    try {
+        dlogMacros.at(level)(m_tag.get(), val.str().c_str());
+    } catch (const std::out_of_range&) {
+        SLOG(LOG_ERROR, m_tag.get(), "Unsupported log level: %d", level);
+    }
 }
 
 } // nemespace Log
diff --git a/src/manager/dpl/log/src/journal_log_provider.cpp b/src/manager/dpl/log/src/journal_log_provider.cpp
new file mode 100644 (file)
index 0000000..b298b79
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  Copyright (c) 2000 - 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       journal_log_provider.cpp
+ * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version    1.0
+ */
+
+#include <dpl/log/journal_log_provider.h>
+#include <systemd/sd-journal.h>
+#include <map>
+#include <stdexcept>
+
+namespace CKM {
+namespace Log {
+
+namespace {
+std::map<AbstractLogProvider::LogLevel, int> journalLevel = {
+        { AbstractLogProvider::LogLevel::Error,     LOG_ERR },
+        { AbstractLogProvider::LogLevel::Warning,   LOG_WARNING },
+        { AbstractLogProvider::LogLevel::Info,      LOG_INFO },
+        { AbstractLogProvider::LogLevel::Debug,     LOG_DEBUG},
+        { AbstractLogProvider::LogLevel::Pedantic,  LOG_DEBUG}
+};
+
+} // namespace anonymous
+
+JournalLogProvider::JournalLogProvider()
+{}
+
+JournalLogProvider::~JournalLogProvider()
+{}
+
+void JournalLogProvider::Log(AbstractLogProvider::LogLevel level,
+                             const char *message,
+                             const char *fileName,
+                             int line,
+                             const char *function) const
+{
+    try {
+        sd_journal_send("PRIORITY=%d", journalLevel.at(level),
+                        "CODE_FILE=%s", fileName,
+                        "CODE_FUNC=%s", function,
+                        "CODE_LINE=%d", line,
+                        "MESSAGE=%s", message,
+                        NULL);
+    } catch (const std::out_of_range&) {
+        sd_journal_send("PRIORITY=%d", LOG_ERR,
+                        "CODE_FILE=%s", fileName,
+                        "CODE_FUNC=%s", function,
+                        "CODE_LINE=%d", line,
+                        "MESSAGE=Unsupported log level %d", level,
+                        NULL);
+    }
+}
+
+} /* namespace Log */
+} /* namespace CKM */
index 15f7169..1e91ff1 100644 (file)
 #include <stddef.h>
 #include <string.h>
 
+#include <string>
+#include <stdexcept>
+#include <unordered_map>
+#include <cassert>
+
 #include <dpl/log/log.h>
 #include <dpl/singleton_impl.h>
-#include <dpl/log/dlog_log_provider.h>
 #include <dpl/log/old_style_log_provider.h>
+#include <dpl/log/dlog_log_provider.h>
+#include <dpl/log/journal_log_provider.h>
 
 IMPLEMENT_SINGLETON(CKM::Log::LogSystem)
 
@@ -33,97 +39,43 @@ namespace CKM {
 namespace Log {
 namespace // anonymous
 {
-#ifdef BUILD_TYPE_DEBUG
-const char *OLD_STYLE_LOGS_ENV_NAME = "DPL_USE_OLD_STYLE_LOGS";
-const char *OLD_STYLE_PEDANTIC_LOGS_ENV_NAME =
-    "DPL_USE_OLD_STYLE_PEDANTIC_LOGS";
-const char *OLD_STYLE_LOGS_MASK_ENV_NAME = "DPL_USE_OLD_STYLE_LOGS_MASK";
-#endif // BUILD_TYPE_DEBUG
-const char *CENT_KEY_LOG_OFF = "DPL_LOG_OFF";
-} // namespace anonymous
+const char * const CKM_LOG_LEVEL =      "CKM_LOG_LEVEL";
+const char * const CKM_LOG_PROVIDER =   "CKM_LOG_PROVIDER";
 
-bool LogSystem::IsLoggingEnabled() const
-{
-    return m_isLoggingEnabled;
-}
+const std::string CONSOLE =     "CONSOLE";
+const std::string DLOG =        "DLOG";
+const std::string JOURNALD =    "JOURNALD";
 
-LogSystem::LogSystem() :
-    m_isLoggingEnabled(!getenv(CENT_KEY_LOG_OFF))
-{
+typedef AbstractLogProvider*(*provider_fn)();
+std::unordered_map<std::string, provider_fn> new_provider = {
 #ifdef BUILD_TYPE_DEBUG
-    bool oldStyleLogs = false;
-    bool oldStyleDebugLogs = true;
-    bool oldStyleInfoLogs = true;
-    bool oldStyleWarningLogs = true;
-    bool oldStyleErrorLogs = true;
-    bool oldStylePedanticLogs = false;
-
-    // Check environment settings about pedantic logs
-    const char *value = getenv(OLD_STYLE_LOGS_ENV_NAME);
-
-    if (value != NULL && !strcmp(value, "1")) {
-        oldStyleLogs = true;
-    }
-
-    value = getenv(OLD_STYLE_PEDANTIC_LOGS_ENV_NAME);
+        { CONSOLE,  []{ return static_cast<AbstractLogProvider*>(new OldStyleLogProvider()); } },
+#endif // BUILD_TYPE_DEBUG
+        { DLOG,     []{ return static_cast<AbstractLogProvider*>(new DLOGLogProvider()); } },
+        { JOURNALD, []{ return static_cast<AbstractLogProvider*>(new JournalLogProvider()); } }
+};
 
-    if (value != NULL && !strcmp(value, "1")) {
-        oldStylePedanticLogs = true;
-    }
+} // namespace anonymous
 
-    value = getenv(OLD_STYLE_LOGS_MASK_ENV_NAME);
-
-    if (value != NULL) {
-        size_t len = strlen(value);
-
-        if (len >= 1) {
-            if (value[0] == '0') {
-                oldStyleDebugLogs = false;
-            } else if (value[0] == '1') {
-                oldStyleDebugLogs = true;
-            }
-        }
-
-        if (len >= 2) {
-            if (value[1] == '0') {
-                oldStyleInfoLogs = false;
-            } else if (value[1] == '1') {
-                oldStyleInfoLogs = true;
-            }
-        }
-
-        if (len >= 3) {
-            if (value[2] == '0') {
-                oldStyleWarningLogs = false;
-            } else if (value[2] == '1') {
-                oldStyleWarningLogs = true;
-            }
-        }
-
-        if (len >= 4) {
-            if (value[3] == '0') {
-                oldStyleErrorLogs = false;
-            } else if (value[3] == '1') {
-                oldStyleErrorLogs = true;
-            }
-        }
+LogSystem::LogSystem()
+{
+    try {
+        m_level = static_cast<AbstractLogProvider::LogLevel>(std::stoi(getenv(CKM_LOG_LEVEL)));
+    } catch(const std::exception&) {
+        m_level = AbstractLogProvider::LogLevel::Debug;
     }
+#ifndef BUILD_TYPE_DEBUG
+    if (m_level > AbstractLogProvider::LogLevel::Error)
+        m_level = AbstractLogProvider::LogLevel::Error;
+#endif // BUILD_TYPE_DEBUG
 
-    // Setup default DLOG and old style logging
-    if (oldStyleLogs) {
-        // Old style
-        AddProvider(new OldStyleLogProvider(oldStyleDebugLogs,
-                                            oldStyleInfoLogs,
-                                            oldStyleWarningLogs,
-                                            oldStyleErrorLogs,
-                                            oldStylePedanticLogs));
-    } else {
-        // DLOG
-        AddProvider(new DLOGLogProvider());
+    AbstractLogProvider* prv = NULL;
+    try {
+        prv = new_provider.at(getenv(CKM_LOG_PROVIDER))();
+    } catch(const std::exception&) {
+        prv = new_provider[DLOG]();
     }
-#else // BUILD_TYPE_DEBUG
-    AddProvider(new DLOGLogProvider());
-#endif // BUILD_TYPE_DEBUG
+    AddProvider(prv);
 }
 
 LogSystem::~LogSystem()
@@ -159,69 +111,14 @@ void LogSystem::RemoveProvider(AbstractLogProvider *provider)
     m_providers.remove(provider);
 }
 
-void LogSystem::Debug(const char *message,
-                      const char *filename,
-                      int line,
-                      const char *function)
-{
-    for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
-         iterator != m_providers.end();
-         ++iterator)
-    {
-        (*iterator)->Debug(message, filename, line, function);
-    }
-}
-
-void LogSystem::Info(const char *message,
-                     const char *filename,
-                     int line,
-                     const char *function)
-{
-    for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
-         iterator != m_providers.end();
-         ++iterator)
-    {
-        (*iterator)->Info(message, filename, line, function);
-    }
-}
-
-void LogSystem::Warning(const char *message,
-                        const char *filename,
-                        int line,
-                        const char *function)
-{
-    for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
-         iterator != m_providers.end();
-         ++iterator)
-    {
-        (*iterator)->Warning(message, filename, line, function);
-    }
-}
-
-void LogSystem::Error(const char *message,
-                      const char *filename,
-                      int line,
-                      const char *function)
+void LogSystem::Log(AbstractLogProvider::LogLevel level,
+                    const char *message,
+                    const char *filename,
+                    int line,
+                    const char *function)
 {
-    for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
-         iterator != m_providers.end();
-         ++iterator)
-    {
-        (*iterator)->Error(message, filename, line, function);
-    }
-}
-
-void LogSystem::Pedantic(const char *message,
-                         const char *filename,
-                         int line,
-                         const char *function)
-{
-    for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
-         iterator != m_providers.end();
-         ++iterator)
-    {
-        (*iterator)->Pedantic(message, filename, line, function);
-    }
+    for (const auto& it : m_providers )
+        it->Log(level, message, filename, line, function);
 }
 
 }
index 0ce1cb3..66b18f1 100644 (file)
@@ -25,6 +25,8 @@
 #include <cstdio>
 #include <cstring>
 #include <sstream>
+#include <map>
+#include <stdexcept>
 #include <sys/time.h>
 #include <unistd.h>
 #include <dlog.h>
@@ -63,139 +65,44 @@ std::string GetFormattedTime()
              static_cast<int>(tv.tv_usec / 1000));
     return format;
 }
-} // namespace anonymous
 
-std::string OldStyleLogProvider::FormatMessage(const char *message,
-                                               const char *filename,
-                                               int line,
-                                               const char *function)
-{
-    std::ostringstream val;
+struct ColorMark {
+    const char* const begin;
+    const char* const end;
+};
 
-    val << std::string("[") << GetFormattedTime() << std::string("] [") <<
-    static_cast<unsigned long>(pthread_self()) << "/" <<
-    static_cast<int>(getpid()) << std::string("] [") <<
-    LocateSourceFileName(filename) << std::string(":") << line <<
-    std::string("] ") << function << std::string("(): ") << message;
+std::map<AbstractLogProvider::LogLevel, ColorMark> consoleLevel = {
+        { AbstractLogProvider::LogLevel::Error,     {ERROR_BEGIN,       ERROR_END} },
+        { AbstractLogProvider::LogLevel::Warning,   {WARNING_BEGIN,     WARNING_END} },
+        { AbstractLogProvider::LogLevel::Info,      {INFO_BEGIN,        INFO_END} },
+        { AbstractLogProvider::LogLevel::Debug,     {DEBUG_BEGIN,       DEBUG_END} },
+        { AbstractLogProvider::LogLevel::Pedantic,  {PEDANTIC_BEGIN,    PEDANTIC_END} }
+};
 
-    return val.str();
-}
-
-OldStyleLogProvider::OldStyleLogProvider(bool showDebug,
-                                         bool showInfo,
-                                         bool showWarning,
-                                         bool showError,
-                                         bool showPedantic) :
-    m_showDebug(showDebug),
-    m_showInfo(showInfo),
-    m_showWarning(showWarning),
-    m_showError(showError),
-    m_showPedantic(showPedantic),
-    m_printStdErr(false)
-{}
+} // namespace anonymous
 
-OldStyleLogProvider::OldStyleLogProvider(bool showDebug,
-                                         bool showInfo,
-                                         bool showWarning,
-                                         bool showError,
-                                         bool showPedantic,
-                                         bool printStdErr) :
-    m_showDebug(showDebug),
-    m_showInfo(showInfo),
-    m_showWarning(showWarning),
-    m_showError(showError),
-    m_showPedantic(showPedantic),
-    m_printStdErr(printStdErr)
+OldStyleLogProvider::OldStyleLogProvider()
 {}
 
-void OldStyleLogProvider::Debug(const char *message,
-                                const char *filename,
-                                int line,
-                                const char *function)
-{
-    if (m_showDebug) {
-        if (m_printStdErr) {
-            fprintf(stderr, "%s%s%s\n", DEBUG_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), DEBUG_END);
-        } else {
-            fprintf(stdout, "%s%s%s\n", DEBUG_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), DEBUG_END);
-        }
-    }
-}
-
-void OldStyleLogProvider::Info(const char *message,
-                               const char *filename,
-                               int line,
-                               const char *function)
+void OldStyleLogProvider::Log(AbstractLogProvider::LogLevel level,
+                              const char *message,
+                              const char *fileName,
+                              int line,
+                              const char *function) const
 {
-    if (m_showInfo) {
-        if (m_printStdErr) {
-            fprintf(stderr, "%s%s%s\n", INFO_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), INFO_END);
-        } else {
-            fprintf(stdout, "%s%s%s\n", INFO_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), INFO_END);
-        }
-    }
-}
-
-void OldStyleLogProvider::Warning(const char *message,
-                                  const char *filename,
-                                  int line,
-                                  const char *function)
-{
-    if (m_showWarning) {
-        if (m_printStdErr) {
-            fprintf(stderr, "%s%s%s\n", WARNING_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), WARNING_END);
-        } else {
-            fprintf(stdout, "%s%s%s\n", WARNING_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), WARNING_END);
-        }
-    }
-}
+    try {
+        const struct ColorMark& mark = consoleLevel.at(level);
 
-void OldStyleLogProvider::Error(const char *message,
-                                const char *filename,
-                                int line,
-                                const char *function)
-{
-    if (m_showError) {
-        if (m_printStdErr) {
-            fprintf(stderr, "%s%s%s\n", ERROR_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), ERROR_END);
-        } else {
-            fprintf(stdout, "%s%s%s\n", ERROR_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), ERROR_END);
-        }
+        std::ostringstream val;
+        val << mark.begin << std::string("[") << GetFormattedTime() << std::string("] [") <<
+               static_cast<unsigned long>(pthread_self()) << "/" << static_cast<int>(getpid()) <<
+               std::string("] [") << LocateSourceFileName(fileName) << std::string(":") << line <<
+               std::string("] ") << function << std::string("(): ") << message << mark.end;
+        fprintf(stdout, "%s\n", val.str().c_str());
+    } catch (const std::out_of_range&) {
+        fprintf(stdout, "Unsupported log level: %d\n", level);
     }
-}
 
-void OldStyleLogProvider::Pedantic(const char *message,
-                                   const char *filename,
-                                   int line,
-                                   const char *function)
-{
-    if (m_showPedantic) {
-        if (m_printStdErr) {
-            fprintf(stderr, "%s%s%s\n", PEDANTIC_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), PEDANTIC_END);
-        } else {
-            fprintf(stdout, "%s%s%s\n", PEDANTIC_BEGIN,
-                    FormatMessage(message, filename, line,
-                        function).c_str(), PEDANTIC_END);
-        }
-    }
 }
 
 }
index 67cf335..3b55621 100644 (file)
@@ -8,6 +8,7 @@ ExecStart=/usr/bin/key-manager
 Sockets=central-key-manager-api-storage.socket
 Sockets=central-key-manager-api-control.socket
 Sockets=central-key-manager-api-ocsp.socket
+EnvironmentFile=-/etc/sysconfig/central-key-manager
 
 [Install]
 WantedBy=multi-user.target