Logging: Add systemd journal log provider 24/24124/2
authorMarcin Lis <m.lis@samsung.com>
Tue, 8 Jul 2014 10:48:22 +0000 (12:48 +0200)
committerMarcin Lis <m.lis@samsung.com>
Wed, 9 Jul 2014 15:53:56 +0000 (17:53 +0200)
The logging style inherited from the security-server needs to be adjusted to
Tizen 3.0 logging fashion. The dlog utility is no longer available, the systemd
journal is in use now.

Change-Id: I16c3f7348b60194c31a8bdcc0897f5ee9ec5aea0
Signed-off-by: Marcin Lis <m.lis@samsung.com>
packaging/security-manager.spec
src/common/CMakeLists.txt
src/dpl/log/include/dpl/log/sd_journal_provider.h [new file with mode: 0644]
src/dpl/log/src/sd_journal_provider.cpp [new file with mode: 0644]

index a2f615b..9ba7890 100644 (file)
@@ -16,6 +16,7 @@ BuildRequires: libcap-devel
 BuildRequires: pkgconfig(libsmack)
 BuildRequires: pkgconfig(libprivilege-control)
 BuildRequires: pkgconfig(libsystemd-daemon)
+BuildRequires: pkgconfig(libsystemd-journal)
 BuildRequires: pkgconfig(libtzplatform-config)
 BuildRequires: pkgconfig(sqlite3)
 BuildRequires: pkgconfig(db-util)
index 2cdca69..f3ce1a4 100644 (file)
@@ -3,6 +3,7 @@ SET(COMMON_VERSION ${COMMON_VERSION_MAJOR}.1.0)
 
 PKG_CHECK_MODULES(COMMON_DEP
     REQUIRED
+    libsystemd-journal
     dlog
     )
 
@@ -21,6 +22,7 @@ SET(COMMON_SOURCES
     ${COMMON_PATH}/message-buffer.cpp
     ${DPL_PATH}/log/src/abstract_log_provider.cpp
     ${DPL_PATH}/log/src/dlog_log_provider.cpp
+    ${DPL_PATH}/log/src/sd_journal_provider.cpp
     ${DPL_PATH}/log/src/log.cpp
     ${DPL_PATH}/log/src/old_style_log_provider.cpp
     ${DPL_PATH}/core/src/assert.cpp
diff --git a/src/dpl/log/include/dpl/log/sd_journal_provider.h b/src/dpl/log/include/dpl/log/sd_journal_provider.h
new file mode 100644 (file)
index 0000000..7bf7bb6
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * 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        sd_journal_provider.h
+ * @author      Marcin Lis (m.lis@samsung.com)
+ * @version     1.0
+ * @brief       This file contains the implementation of systemd journal log provider
+ */
+
+#ifndef SECURITYMANAGER_SD_JOURNAL_PROVIDER_H
+#define SECURITYMANAGER_SD_JOURNAL_PROVIDER_H
+
+#include <dpl/log/abstract_log_provider.h>
+#include <memory>
+#include <string>
+
+namespace SecurityManager {
+namespace Log {
+class SdJournalProvider :
+    public AbstractLogProvider
+{
+  private:
+    std::string m_tag;
+
+    static std::string FormatMessage(const char *message,
+                                     const char *filename,
+                                     int line,
+                                     const char *function);
+
+  public:
+    SdJournalProvider();
+    virtual ~SdJournalProvider();
+
+    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);
+
+    // All Pedantic logs are translated to Debug
+    virtual void Pedantic(const char *message,
+                          const char *fileName,
+                          int line,
+                          const char *function);
+
+    // Set global Tag for all Security Manager Logs
+    void SetTag(const char *tag);
+
+}; // class SdJournalProvider
+
+} // namespace Log
+} // namespace SecurityManager
+
+#endif // SECURITYMANAGER_SD_JOURNAL_PROVIDER_H
diff --git a/src/dpl/log/src/sd_journal_provider.cpp b/src/dpl/log/src/sd_journal_provider.cpp
new file mode 100644 (file)
index 0000000..6c22929
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * 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        sd_journal_provider.cpp
+ * @author      Marcin Lis (m.lis@samsung.com)
+ * @version     1.0
+ * @brief       This file contains the implementation of systemd journal log provider
+ */
+
+#include <dpl/log/sd_journal_provider.h>
+#include <string>
+#include <sstream>
+#include <systemd/sd-journal.h>
+
+#define UNUSED __attribute__((unused))
+
+namespace SecurityManager {
+namespace Log {
+std::string SdJournalProvider::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;
+
+    return val.str();
+}
+
+SdJournalProvider::SdJournalProvider()
+{}
+
+SdJournalProvider::~SdJournalProvider()
+{}
+
+void SdJournalProvider::SetTag(const char *tag)
+{
+    m_tag = std::string(tag);
+}
+
+void SdJournalProvider::Debug(const char *message,
+                            const char *filename,
+                            int line,
+                            const char *function)
+{
+    // sd-journal imports LOG priorities from the syslog, see syslog(3) for details
+    sd_journal_print(LOG_DEBUG, "%s: %s", m_tag.c_str(),
+                     (FormatMessage(message, filename, line, function)).c_str());
+}
+
+void SdJournalProvider::Info(const char *message,
+                           const char *filename,
+                           int line,
+                           const char *function)
+{
+    sd_journal_print(LOG_INFO, "%s: %s", m_tag.c_str(),
+                     (FormatMessage(message, filename, line, function)).c_str());
+}
+
+void SdJournalProvider::Warning(const char *message,
+                              const char *filename,
+                              int line,
+                              const char *function)
+{
+    sd_journal_print(LOG_WARNING, "%s: %s", m_tag.c_str(),
+                     (FormatMessage(message, filename, line, function)).c_str());
+}
+
+void SdJournalProvider::Error(const char *message,
+                            const char *filename,
+                            int line,
+                            const char *function)
+{
+    sd_journal_print(LOG_ERR, "%s: %s", m_tag.c_str(),
+                     (FormatMessage(message, filename, line, function)).c_str());
+}
+
+// All Pedantic logs are translated to Debug
+void SdJournalProvider::Pedantic(const char *message,
+                               const char *filename,
+                               int line,
+                               const char *function)
+{
+    Debug(message, filename, line, function);
+}
+
+} // namespace Log
+} // namespace SecurityManager