From ebb01d4e196afe11d74792cdc77891329d84200a Mon Sep 17 00:00:00 2001 From: Georges Berenger Date: Wed, 20 Dec 2023 10:58:33 -0800 Subject: [PATCH] Fix OSS logging primitives macros (#140) Summary: Pull Request resolved: https://github.com/facebookresearch/vrs/pull/140 The logging macros were not quite working as expected, and a little more black magic fu was required to make them compile and work as expected on all platforms. Reviewed By: SeaOtocinclus Differential Revision: D52315578 fbshipit-source-id: e6e9f6e439ca3f807220ebbed699d9e3e7e263d8 --- tools/vrs/VrsCommand.cpp | 2 -- tools/vrs/vrs.cpp | 2 -- vrs/oss/logging/Checks.cpp | 7 ++++--- vrs/oss/logging/Checks.h | 12 ++++-------- vrs/oss/logging/Verify.h | 23 ++++++++++------------- 5 files changed, 18 insertions(+), 28 deletions(-) diff --git a/tools/vrs/VrsCommand.cpp b/tools/vrs/VrsCommand.cpp index 797fa245..94cac2d6 100644 --- a/tools/vrs/VrsCommand.cpp +++ b/tools/vrs/VrsCommand.cpp @@ -18,9 +18,7 @@ #include #include -#include #include -#include #include #define DEFAULT_LOG_CHANNEL "VrsCommand" diff --git a/tools/vrs/vrs.cpp b/tools/vrs/vrs.cpp index 469fdd72..16290dec 100644 --- a/tools/vrs/vrs.cpp +++ b/tools/vrs/vrs.cpp @@ -16,8 +16,6 @@ #include "VrsCommand.h" -#include - #include using namespace std; diff --git a/vrs/oss/logging/Checks.cpp b/vrs/oss/logging/Checks.cpp index 34e231a8..b50bf6b2 100644 --- a/vrs/oss/logging/Checks.cpp +++ b/vrs/oss/logging/Checks.cpp @@ -17,11 +17,12 @@ #include "Checks.h" #include -#include #include #include +#include + // Abort Macro. #if IS_ANDROID_PLATFORM() #include @@ -41,8 +42,8 @@ namespace vrs { namespace logging { -void logAndAbort(const std::string& condition, const std::string& message) { - fmt::print(stderr, fg(fmt::color::red), "{} {}", condition, message); +void logAndAbort(const char* condition, const std::string& message) { + fmt::print(stderr, fg(fmt::color::red), "Check '{}' failed. {}\n", condition, message); XR_ABORT_IMPL(message); } diff --git a/vrs/oss/logging/Checks.h b/vrs/oss/logging/Checks.h index cf475acf..c82cf0e4 100644 --- a/vrs/oss/logging/Checks.h +++ b/vrs/oss/logging/Checks.h @@ -20,14 +20,10 @@ #include -#include - -#include "LogLevel.h" - namespace vrs { namespace logging { -void logAndAbort(const std::string& condition, const std::string& message = {}); +void logAndAbort(const char* condition, const std::string& message = {}); } // namespace logging } // namespace vrs @@ -36,10 +32,10 @@ void logAndAbort(const std::string& condition, const std::string& message = {}); // Check Macros. // -#define XR_CHECK_FORMAT(condition, ...) \ - (condition ? 0 : ((vrs::logging::logAndAbort(#condition, fmt::format(__VA_ARGS__))), 0)) +#define XR_CHECK_FORMAT(condition, fmtstr, ...) \ + (condition ? 0 : ((vrs::logging::logAndAbort(#condition, fmt::format(fmtstr, ##__VA_ARGS__))), 0)) -#define XR_CHECK(condition, ...) XR_CHECK_FORMAT(condition, ##__VA_ARGS__, "") +#define XR_CHECK(condition, ...) XR_CHECK_FORMAT(condition, "" __VA_ARGS__) #define XR_CHECK_EQ(val1, val2, ...) XR_CHECK((val1) == (val2), ##__VA_ARGS__) diff --git a/vrs/oss/logging/Verify.h b/vrs/oss/logging/Verify.h index c6f1276c..48e2b8d8 100644 --- a/vrs/oss/logging/Verify.h +++ b/vrs/oss/logging/Verify.h @@ -19,10 +19,6 @@ #include #include -#include - -#include "LogLevel.h" - #ifndef DEFAULT_LOG_CHANNEL #error "DEFAULT_LOG_CHANNEL must be defined before including " #endif // DEFAULT_LOG_CHANNEL @@ -33,14 +29,15 @@ // condition. // -#define XR_VERIFY_C(channel_, condition_, ...) \ - (condition_ ? 1 \ - : (fmt::print( \ - stderr, \ - fg(fmt::color::red), \ - "Verify {} failed: {}", \ - #condition_, \ - fmt::format(__VA_ARGS__)), \ +#define XR_VERIFY_C(channel_, condition_, fmtstr, ...) \ + (condition_ ? 1 \ + : (fmt::print( \ + stderr, \ + fg(fmt::color::red), \ + "{}: Verify '{}' failed. {}\x1b[0m\n", \ + channel_, \ + #condition_, \ + fmt::format(fmtstr, ##__VA_ARGS__)), \ 0)) -#define XR_VERIFY(cond, ...) XR_VERIFY_C(DEFAULT_LOG_CHANNEL, cond, ##__VA_ARGS__, "") +#define XR_VERIFY(cond, ...) XR_VERIFY_C(DEFAULT_LOG_CHANNEL, cond, "" __VA_ARGS__)