Use DLOG for logging 25/68125/11
authorArkadiusz Szulakiewicz <a.szulakiewi@partner.samsung.com>
Mon, 2 May 2016 10:53:16 +0000 (12:53 +0200)
committerTomasz Iwanek <t.iwanek@samsung.com>
Wed, 11 May 2016 13:53:50 +0000 (06:53 -0700)
Change-Id: I2dc65ffed7b1dfa2300e8e6fb79cf629e643a233

CMakeLists.txt
packaging/manifest-parser.spec
src/manifest_parser/utils/CMakeLists.txt
src/manifest_parser/utils/logging.cc [new file with mode: 0644]
src/manifest_parser/utils/logging.h

index 8b8d58ea12bc96a04521d0bfc8b5fc224d610088..40386e14589d07f283e640e35849cad151ec3192 100644 (file)
@@ -34,6 +34,7 @@ ADD_DEFINITIONS("-Wall")
 ADD_DEFINITIONS("-Wextra")
 ADD_DEFINITIONS("-fPIE")
 ADD_DEFINITIONS("-fPIC")
+ADD_DEFINITIONS("-DPROJECT_TAG=\"ManifestParser\"")
 
 ADD_DEFINITIONS("-DSHAREDIR=\"${SHAREDIR}\"")
 ADD_DEFINITIONS("-DTIZEN_VERSION=\"${TIZEN_VERSION}\"")
index 04d66604776e28c5be975ad5c1544ac8b9ee47ae..510f9d5a929fe0784d50c6682460d2480733c2d9 100644 (file)
@@ -10,6 +10,7 @@ Source100:      %{name}.manifest
 BuildRequires:  boost-devel
 BuildRequires:  cmake
 BuildRequires:  gtest-devel
+BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(pkgmgr-parser)
 BuildRequires:  pkgconfig(pkgmgr-info)
 BuildRequires:  pkgconfig(libiri)
index 0917ce12ee3189182bf1be227f21b7b5d7353bde..2b44ce2e0de0e219e1b159fd260e4b91267d06b8 100644 (file)
@@ -1,5 +1,6 @@
 # Target - sources
 SET(SRCS
+  logging.cc
   iri_util.cc
   string_util.cc
   language_tag_validator.cc
diff --git a/src/manifest_parser/utils/logging.cc b/src/manifest_parser/utils/logging.cc
new file mode 100644 (file)
index 0000000..9e9481f
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include "manifest_parser/utils/logging.h"
+
+namespace utils {
+
+log_priority LogLevelToPriority(LogLevel level) {
+  switch (level) {
+    case LogLevel::LOG_ERROR:
+      return log_priority::DLOG_ERROR;
+    case LogLevel::LOG_WARNING:
+      return log_priority::DLOG_WARN;
+    case LogLevel::LOG_INFO:
+      return log_priority::DLOG_INFO;
+    case LogLevel::LOG_DEBUG:
+      return log_priority::DLOG_DEBUG;
+    default:
+      return log_priority::DLOG_UNKNOWN;
+  }
+}
+
+}  // namespace utilschce
index 101f34df06efefd9a7b12280b0b6861c46a1129b..fca5cb0bd239398aaae3086c89528908490de8ca 100644 (file)
@@ -5,6 +5,16 @@
 #ifndef MANIFEST_PARSER_UTILS_LOGGING_H_
 #define MANIFEST_PARSER_UTILS_LOGGING_H_
 
+#include <dlog/dlog.h>
+
+#ifndef PROJECT_TAG
+#define PROJECT_TAG ""
+#endif
+
+#ifdef LOG
+#undef LOG
+#endif
+
 #include <cassert>
 #include <iomanip>
 #include <iostream>
@@ -20,6 +30,8 @@ enum class LogLevel {
   LOG_DEBUG,
 };
 
+log_priority LogLevelToPriority(LogLevel level);
+
 template<LogLevel> struct LogTag;
 template<> struct LogTag<LogLevel::LOG_ERROR> {
   static constexpr const char* value = "\033[1;31m| ERROR   |\033[0m";
@@ -34,21 +46,48 @@ template<> struct LogTag<LogLevel::LOG_DEBUG> {
   static constexpr const char* value = "\033[0m| DEBUG   |\033[0m";
 };
 
+template <class charT, class traits = std::char_traits<charT>>
+class StringStream : private std::basic_ostringstream<charT, traits> {
+ public:
+  using std::basic_ostringstream<charT, traits>::str;
+
+  template <class T>
+  StringStream&  operator<<(const T& value) {
+    static_cast<std::basic_ostringstream<charT, traits> &>(*this) << value;
+    return *this;
+  }
+};
+
 class LogCatcher {
  public:
-  LogCatcher() { }
-  void operator&(const std::ostream& str) const {
-    std::cerr << static_cast<const std::ostringstream*>(&str)->str()
-              << std::endl;
+  LogCatcher(LogLevel level, const char* tag)
+    : level_(level), tag_(tag) { }
+
+  void operator&(const StringStream<char>& str) const {
+    dlog_print(LogLevelToPriority(level_), tag_.c_str(), str.str().c_str());
+
+    static const char* app_installer_log = getenv("APP_INSTALLERS_LOG");
+    if (level_ == LogLevel::LOG_ERROR || app_installer_log != nullptr) {
+      std::cerr << str.str()
+        << std::endl;
+    }
   }
+ private:
+  LogLevel level_;
+  std::string tag_;
 };
 
 }  // namespace utils
 
+
 inline static const constexpr char* __tag_for_logging() {
   return "";
 }
 
+inline static const constexpr char* __tag_for_project() {
+  return PROJECT_TAG;
+}
+
 // To be defined in class namespace if user want different log tag for given
 // scope
 #define SCOPE_LOG_TAG(TAG)                                                     \
@@ -61,7 +100,9 @@ inline static const constexpr char* __tag_for_logging() {
 //     where:
 //       LEVEL = ERROR | WARNING | INFO | DEBUG
 #define LOG(LEVEL)                                                             \
-    ::utils::LogCatcher() & std::ostringstream()                               \
+    ::utils::LogCatcher(                                                       \
+      ::utils::LogLevel::LOG_ ## LEVEL, __tag_for_project())                   \
+      & ::utils::StringStream<char>()                                          \
       << std::string(::utils::LogTag<::utils::LogLevel::LOG_ ## LEVEL>::value) \
       << " " << std::setw(20) << std::left << __tag_for_logging()              \
       << std::setw(0) << " : "                                                 \