From 67bca1f889e6b439371c826fef6f413663f74f81 Mon Sep 17 00:00:00 2001 From: Pawel Andruszkiewicz Date: Mon, 23 Nov 2015 12:16:29 +0100 Subject: [PATCH] [Common] Added macros useful for reporting errors. [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 --- src/common/logger.h | 73 +++++++++++++++++++++++++++++++++++ src/common/platform_result.cc | 4 ++ src/common/tools.cc | 6 +-- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/common/logger.h b/src/common/logger.h index 52407247..e80dc4aa 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -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_ diff --git a/src/common/platform_result.cc b/src/common/platform_result.cc index cf091b34..cc85f30b 100755 --- a/src/common/platform_result.cc +++ b/src/common/platform_result.cc @@ -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 { diff --git a/src/common/tools.cc b/src/common/tools.cc index a02b6a7a..784e9e12 100644 --- a/src/common/tools.cc +++ b/src/common/tools.cc @@ -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())); } -- 2.34.1