[Common] Added macros useful for reporting errors.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 23 Nov 2015 11:16:29 +0000 (12:16 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Tue, 24 Nov 2015 07:44:59 +0000 (08:44 +0100)
[Usage]
///////////////////////
    result = ArchiveManager::getInstance().open(callback);

    if (result) {
        ReportSuccess(out);
    } else {
        LogAndReportError(result, &out);
        LogAndReportError(result, &out, ("Failed to open archive."));
        LogAndReportError(result, &out, ("Failed to open archive: %d.", 11));
    }
///////////////////////
    auto pr1 = LogAndCreateResult(ErrorCode::IO_ERR);
    PlatformResult pr2 = LogAndCreateResult(ErrorCode::IO_ERR);
    return LogAndCreateResult(ErrorCode::IO_ERR);

    auto pr3 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.");
    PlatformResult pr4 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.");
    return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.");

    auto pr5 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error"));
    PlatformResult pr6 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error"));
    return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error"));

    auto pr7 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error: %d", 33));
    PlatformResult pr8 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error: %d", 33));
    return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error: %d", 33));

[Verification] Existing code was not affected.

Change-Id: I8391443ae00c784d89ad25d50de212e26a1bdc07
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/common/logger.h
src/common/platform_result.cc
src/common/tools.cc

index 52407247a941b95e02eb7debc6f1391b9d94bf42..e80dc4aacd720494ab02fd591d435ade43b08015 100644 (file)
@@ -91,4 +91,77 @@ public:
   void operator&(std::ostream &) {}
 };
 
+// internal macros
+#define LogAndReportError_2(error, object) \
+  do { \
+    LoggerE("Reporting error."); \
+    ReportError(error, object); \
+  } while(false)
+
+#define LogAndReportError_3(error, object, log) \
+  do { \
+    LoggerE log; \
+    ReportError(error, object); \
+  } while(false)
+
+#define LogAndReportError_X(_0, _1, _2, _3, FUNC, ...) FUNC
+
+// usage:
+// LogAndCreateResult(const common::PlatformResult& result, picojson::object* obj, [(const char* log, ...)])
+//  LogAndReportError(result, &out);
+//  LogAndReportError(result, &out, ("Failed to open archive."));
+//  LogAndReportError(result, &out, ("Failed to open archive: %d.", 11));
+
+#define LogAndReportError(...) \
+  LogAndReportError_X(, ##__VA_ARGS__, \
+     LogAndReportError_3(__VA_ARGS__), \
+     LogAndReportError_2(__VA_ARGS__) \
+  )
+
+// internal macros
+#define LogAndCreateResult_1(error_code) \
+  ( \
+    LoggerE("Creating PlatformResult with error"), \
+    common::PlatformResult(error_code) \
+  )
+
+#define LogAndCreateResult_2(error_code, msg) \
+  ( \
+    LoggerE("Creating PlatformResult with error"), \
+    common::PlatformResult(error_code, msg) \
+  )
+
+#define LogAndCreateResult_3(error_code, msg, log) \
+  ( \
+    LoggerE log, \
+    common::PlatformResult(error_code, msg) \
+  )
+
+#define LogAndCreateResult_X(_0, _1, _2, _3, FUNC, ...) FUNC
+
+// LogAndCreateResult(common::ErrorCode code, [const char* error_message, (const char* log, ...)])
+// usage:
+//  auto pr1 = LogAndCreateResult(ErrorCode::IO_ERR);
+//  PlatformResult pr2 = LogAndCreateResult(ErrorCode::IO_ERR);
+//  return LogAndCreateResult(ErrorCode::IO_ERR);
+//
+//  auto pr3 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.");
+//  PlatformResult pr4 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.");
+//  return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.");
+//
+//  auto pr5 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error"));
+//  PlatformResult pr6 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error"));
+//  return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error"));
+//
+//  auto pr7 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error: %d", 33));
+//  PlatformResult pr8 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error: %d", 33));
+//  return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO error: %d", 33));
+
+#define LogAndCreateResult(...) \
+  LogAndCreateResult_X(, ##__VA_ARGS__, \
+     LogAndCreateResult_3(__VA_ARGS__), \
+     LogAndCreateResult_2(__VA_ARGS__), \
+     LogAndCreateResult_1(__VA_ARGS__) \
+  )
+
 #endif // COMMON_LOGGER_H_
index cf091b346543bc2ee5ab16dcdc762cebc7dca8b9..cc85f30b75943b256ca6b671307e5d4b31de27ec 100755 (executable)
@@ -24,6 +24,10 @@ PlatformResult::PlatformResult(const ErrorCode& error_code, const std::string& m
     message_(message)
 {
   LoggerD("Enter");
+
+  if (ErrorCode::NO_ERROR != error_code) {
+    LoggerE("PlatformResult: %d, message: %s", error_code, message.c_str());
+  }
 }
 
 picojson::value PlatformResult::ToJSON() const {
index a02b6a7ae84e44010c776b35d9acd8e996c26e2b..784e9e12e2c3423a4b055c7eb84ff170bfba86b5 100644 (file)
@@ -46,18 +46,18 @@ void ReportSuccess(const picojson::value& result, picojson::object& out) {
 }
 
 void ReportError(picojson::object& out) {
-  LoggerD("Enter");
+  LoggerE("Error without error code");
   out.insert(std::make_pair("status", picojson::value("error")));
 }
 
 void ReportError(const PlatformException& ex, picojson::object& out) {
-  LoggerD("Enter");
+  LoggerE("PlatformException: %s, message: %s", ex.name().c_str(), ex.message().c_str());
   out.insert(std::make_pair("status", picojson::value("error")));
   out.insert(std::make_pair("error", ex.ToJSON()));
 }
 
 void ReportError(const PlatformResult& error, picojson::object* out) {
-  LoggerD("Enter");
+  LoggerE("PlatformResult: %d, message: %s", error.error_code(), error.message().c_str());
   out->insert(std::make_pair("status", picojson::value("error")));
   out->insert(std::make_pair("error", error.ToJSON()));
 }