Move logging and macros to utils 25/34825/5
authorTomasz Iwanek <t.iwanek@samsung.com>
Thu, 29 Jan 2015 09:17:10 +0000 (10:17 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Tue, 10 Feb 2015 15:07:18 +0000 (07:07 -0800)
Just moves code. I will think about appling this.

Change-Id: I6e6e21a9144f548697acc0f959e7aa2338831121

src/signature/CMakeLists.txt
src/signature/logging.h [deleted file]
src/signature/signature_data.h
src/signature/signature_parser.cc
src/signature/signature_parser.h
src/signature/signature_validator.cc
src/signature/signature_validator.h
src/signature/signature_xmlsec_adaptor.cc
src/utils/logging.h [new file with mode: 0644]
src/utils/macros.h [moved from src/signature/marcos.h with 79% similarity]

index 6ef981c..a8fbdc1 100644 (file)
@@ -15,6 +15,8 @@ APPLY_PKG_CONFIG(${TARGET_LIBNAME_SIGNATURE} PUBLIC
   XMLSEC1_DEPS
   Boost
 )
+# Target in-package deps
+TARGET_LINK_LIBRARIES(${TARGET_LIBNAME_SIGNATURE} PUBLIC ${TARGET_LIBNAME_UTILS})
 
 # Extra
 SET_TARGET_PROPERTIES(${TARGET_LIBNAME_SIGNATURE} PROPERTIES VERSION ${VERSION})
diff --git a/src/signature/logging.h b/src/signature/logging.h
deleted file mode 100644 (file)
index 199a14f..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright (c) 2014 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.
-
-#ifndef SIGNATURE_LOGGING_H_
-#define SIGNATURE_LOGGING_H_
-
-#include <iostream>
-#include <sstream>
-
-// TODO(tiwanek): Logging...
-
-namespace detail {
-
-class LogCatcher {
- public:
-  LogCatcher() { }
-  void operator&(const std::ostream& str) const {
-    // TODO(tiwanek): this cast is error-prone - fix it
-    std::cerr << static_cast<const std::ostringstream*>(&str)->str()
-              << std::endl;
-  }
-};
-
-}  // namespace detail
-
-#define LOG(LEVEL)                                                             \
-    ::detail::LogCatcher() & std::ostringstream() << "[" << #LEVEL << "] "     \
-
-#endif  // SIGNATURE_LOGGING_H_
index 3046008..b6c5430 100644 (file)
@@ -16,7 +16,7 @@
 #include <set>
 #include <string>
 
-#include "signature/marcos.h"
+#include "utils/macros.h"
 
 namespace common_installer {
 namespace signature {
index 749c0c8..cf3bdf6 100644 (file)
@@ -21,8 +21,8 @@
 #include <utility>
 #include <vector>
 
-#include "signature/logging.h"
-#include "signature/marcos.h"
+#include "utils/logging.h"
+#include "utils/macros.h"
 
 namespace {
 const char kExpectedXmlns[] = "http://www.w3.org/2000/09/xmldsig#";
index 96bad1d..299e945 100644 (file)
@@ -12,7 +12,7 @@
 
 #include <memory>
 
-#include "signature/marcos.h"
+#include "utils/macros.h"
 #include "signature/signature_data.h"
 
 namespace common_installer {
index 2b30d28..882d800 100644 (file)
@@ -16,7 +16,7 @@
 #include <set>
 #include <string>
 
-#include "signature/logging.h"
+#include "utils/logging.h"
 #include "signature/signature_data.h"
 #include "signature/signature_parser.h"
 #include "signature/signature_xmlsec_adaptor.h"
index de292bd..824951c 100644 (file)
@@ -12,7 +12,7 @@
 
 #include <string>
 
-#include "signature/marcos.h"
+#include "utils/macros.h"
 
 namespace common_installer {
 namespace signature {
index e905fad..4e37960 100644 (file)
@@ -34,7 +34,7 @@
 #include <string>
 #include <utility>
 
-#include "signature/logging.h"
+#include "utils/logging.h"
 
 namespace bai = boost::archive::iterators;
 namespace bf = boost::filesystem;
diff --git a/src/utils/logging.h b/src/utils/logging.h
new file mode 100644 (file)
index 0000000..58c254d
--- /dev/null
@@ -0,0 +1,55 @@
+// Copyright (c) 2014 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.
+
+#ifndef UTILS_LOGGING_H_
+#define UTILS_LOGGING_H_
+
+#include <cassert>
+#include <iostream>
+#include <sstream>
+
+namespace utils {
+
+enum class LogLevel {
+  LOG_ERROR,
+  LOG_WARNING,
+  LOG_INFO,
+  LOG_DEBUG,
+};
+
+template<LogLevel> struct LogTag;
+template<> struct LogTag<LogLevel::LOG_ERROR> {
+  static constexpr const char* value = "\033[0;31m[ERROR]  \033[0m";
+};
+template<> struct LogTag<LogLevel::LOG_WARNING> {
+  static constexpr const char* value = "\033[0;33m[WARNING]\033[0m";
+};
+template<> struct LogTag<LogLevel::LOG_INFO>  {
+  static constexpr const char* value = "\033[0;32m[INFO]   \033[0m";
+};
+template<> struct LogTag<LogLevel::LOG_DEBUG> {
+  static constexpr const char* value = "\033[0m[DEBUG]  \033[0m";
+};
+
+class LogCatcher {
+ public:
+  LogCatcher() { }
+  void operator&(const std::ostream& str) const {
+    // TODO(tiwanek): this cast is error-prone - fix it
+    std::cerr << static_cast<const std::ostringstream*>(&str)->str()
+              << std::endl;
+  }
+};
+
+}  // namespace utils
+
+// Simple logging macro of following usage:
+//   LOG(LEVEL) << object_1 << object_2 << object_n;
+//     where:
+//       LEVEL = ERROR | WARNING | INFO | DEBUG
+#define LOG(LEVEL)                                                             \
+    ::utils::LogCatcher() & std::ostringstream()                               \
+      << ::utils::LogTag<::utils::LogLevel::LOG_##LEVEL>::value                \
+
+#endif  // UTILS_LOGGING_H_
similarity index 79%
rename from src/signature/marcos.h
rename to src/utils/macros.h
index f9a0ccf..c2e558d 100644 (file)
@@ -2,11 +2,11 @@
 // Use of this source code is governed by a apache 2.0 license that can be
 // found in the LICENSE file.
 
-#ifndef SIGNATURE_MARCOS_H_
-#define SIGNATURE_MARCOS_H_
+#ifndef UTILS_MACROS_H_
+#define UTILS_MACROS_H_
 
 #define DISALLOW_COPY_AND_ASSIGN(CLASS)            \
   CLASS(const CLASS&) = delete;                    \
   CLASS& operator=(const CLASS&) = delete          \
 
-#endif  // SIGNATURE_MARCOS_H_
+#endif  // UTILS_MACROS_H_