From ec05613493d37fa848ed8a076cdc1833843a102a Mon Sep 17 00:00:00 2001 From: dmaclach Date: Thu, 6 Jan 2022 15:53:32 -0800 Subject: [PATCH] Improve C++ logging for JNI code. Gets rid of custom logging code that JNI was using and standardizes on `src/main/cpp/util/logging.h`. JNI sets up a logger when the library is loaded. PiperOrigin-RevId: 420161386 --- src/main/cpp/util/BUILD | 3 ++ src/main/native/BUILD | 3 +- src/main/native/darwin/file_jni.cc | 5 -- .../native/darwin/sleep_prevention_jni.cc | 17 +++---- .../darwin/system_disk_space_monitor_jni.cc | 15 +++--- .../system_load_advisory_monitor_jni.cc | 22 ++++----- .../darwin/system_memory_pressure_jni.cc | 23 ++++----- .../darwin/system_suspension_monitor_jni.cc | 21 ++++----- .../darwin/system_thermal_monitor_jni.cc | 28 +++++------ src/main/native/darwin/util.cc | 19 +------- src/main/native/darwin/util.h | 18 ------- src/main/native/macros.h | 16 ------- src/main/native/unix_jni.cc | 47 ++++++++++--------- src/main/native/unix_jni_bsd.cc | 5 -- src/main/native/unix_jni_linux.cc | 6 --- 15 files changed, 83 insertions(+), 165 deletions(-) diff --git a/src/main/cpp/util/BUILD b/src/main/cpp/util/BUILD index 3c416be445cbe1..f63b4249e3b770 100644 --- a/src/main/cpp/util/BUILD +++ b/src/main/cpp/util/BUILD @@ -96,6 +96,9 @@ cc_library( name = "port", srcs = ["port.cc"], hdrs = ["port.h"], + visibility = [ + "//src/main/native:__pkg__", + ], ) cc_library( diff --git a/src/main/native/BUILD b/src/main/native/BUILD index 07d1c2dc01ea52..1e02c9cffcb88a 100644 --- a/src/main/native/BUILD +++ b/src/main/native/BUILD @@ -76,8 +76,9 @@ cc_binary( visibility = ["//src/main/java/com/google/devtools/build/lib/jni:__pkg__"], deps = [ ":latin1_jni_path", - "//src/main/cpp/util", + "//src/main/cpp/util:logging", "//src/main/cpp/util:md5", + "//src/main/cpp/util:port", ], ) diff --git a/src/main/native/darwin/file_jni.cc b/src/main/native/darwin/file_jni.cc index 0ef53161a37b14..b076cd960d6dd5 100644 --- a/src/main/native/darwin/file_jni.cc +++ b/src/main/native/darwin/file_jni.cc @@ -24,7 +24,6 @@ #include -#include "src/main/native/macros.h" #include "src/main/native/unix_jni.h" namespace blaze_jni { @@ -86,8 +85,6 @@ int StatSeconds(const portable_stat_struct &statbuf, StatTimes t) { return statbuf.st_ctime; case STAT_MTIME: return statbuf.st_mtime; - default: - CHECK(false); } } @@ -99,8 +96,6 @@ int StatNanoSeconds(const portable_stat_struct &statbuf, StatTimes t) { return statbuf.st_ctimespec.tv_nsec; case STAT_MTIME: return statbuf.st_mtimespec.tv_nsec; - default: - CHECK(false); } } diff --git a/src/main/native/darwin/sleep_prevention_jni.cc b/src/main/native/darwin/sleep_prevention_jni.cc index 2abb022e437e22..67c35b201eebdd 100644 --- a/src/main/native/darwin/sleep_prevention_jni.cc +++ b/src/main/native/darwin/sleep_prevention_jni.cc @@ -18,9 +18,8 @@ // absl::Mutex but we cannot yet because Bazel doesn't depend on absl. #include // NOLINT -#include "src/main/native/darwin/util.h" +#include "src/main/cpp/util/logging.h" #include "src/main/native/unix_jni.h" -#include "src/main/native/macros.h" namespace blaze_jni { @@ -35,15 +34,14 @@ static IOPMAssertionID g_sleep_state_assertion = kIOPMNullAssertionID; int portable_push_disable_sleep() { std::lock_guard lock(g_sleep_state_mutex); - CHECK_GE(g_sleep_state_stack, 0); + BAZEL_CHECK_GE(g_sleep_state_stack, 0); if (g_sleep_state_stack == 0) { - CHECK_EQ(g_sleep_state_assertion, kIOPMNullAssertionID); + BAZEL_CHECK_EQ(g_sleep_state_assertion, kIOPMNullAssertionID); CFStringRef reasonForActivity = CFSTR("build.bazel"); IOReturn success = IOPMAssertionCreateWithName( kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, reasonForActivity, &g_sleep_state_assertion); - CHECK_EQ(success, kIOReturnSuccess); - log_if_possible("sleep assertion created"); + BAZEL_CHECK_EQ(success, kIOReturnSuccess); } g_sleep_state_stack += 1; return 0; @@ -51,14 +49,13 @@ int portable_push_disable_sleep() { int portable_pop_disable_sleep() { std::lock_guard lock(g_sleep_state_mutex); - CHECK_GT(g_sleep_state_stack, 0); + BAZEL_CHECK_GT(g_sleep_state_stack, 0); g_sleep_state_stack -= 1; if (g_sleep_state_stack == 0) { - CHECK_NEQ(g_sleep_state_assertion, kIOPMNullAssertionID); + BAZEL_CHECK_NE(g_sleep_state_assertion, kIOPMNullAssertionID); IOReturn success = IOPMAssertionRelease(g_sleep_state_assertion); - CHECK_EQ(success, kIOReturnSuccess); + BAZEL_CHECK_EQ(success, kIOReturnSuccess); g_sleep_state_assertion = kIOPMNullAssertionID; - log_if_possible("sleep assertion released"); } return 0; } diff --git a/src/main/native/darwin/system_disk_space_monitor_jni.cc b/src/main/native/darwin/system_disk_space_monitor_jni.cc index aacc2ce16bbd05..eb56bb9ec8e6c6 100644 --- a/src/main/native/darwin/system_disk_space_monitor_jni.cc +++ b/src/main/native/darwin/system_disk_space_monitor_jni.cc @@ -15,8 +15,8 @@ #include #include +#include "src/main/cpp/util/logging.h" #include "src/main/native/darwin/util.h" -#include "src/main/native/macros.h" #include "src/main/native/unix_jni.h" // Not defined by Apple headers, but definitely sent out with macOS 12. @@ -32,30 +32,29 @@ void portable_start_disk_space_monitoring() { dispatch_once(&once_token, ^{ dispatch_queue_t queue = bazel::darwin::JniDispatchQueue(); notify_handler_t lowHandler = (^(int token) { - log_if_possible("disk space low anomaly"); + BAZEL_LOG(USER) << "disk space low anomaly"; disk_space_callback(DiskSpaceLevelLow); }); notify_handler_t veryLowHandler = (^(int token) { - log_if_possible("disk space very low anomaly"); + BAZEL_LOG(USER) << "disk space very low anomaly"; disk_space_callback(DiskSpaceLevelVeryLow); }); int notifyToken; int status = notify_register_dispatch(kNotifyVFSLowDiskSpace, ¬ifyToken, queue, lowHandler); - CHECK(status == NOTIFY_STATUS_OK); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); status = notify_register_dispatch(kNotifyVFSVeryLowDiskSpace, ¬ifyToken, queue, veryLowHandler); - CHECK(status == NOTIFY_STATUS_OK); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); // These are registered solely so we can test the system from end-to-end. status = notify_register_dispatch( "com.google.bazel.test.diskspace.low", ¬ifyToken, queue, lowHandler); - CHECK(status == NOTIFY_STATUS_OK); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); status = notify_register_dispatch("com.google.bazel.test.diskspace.verylow", ¬ifyToken, queue, veryLowHandler); - CHECK(status == NOTIFY_STATUS_OK); - log_if_possible("Disk space monitoring registered"); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); }); } diff --git a/src/main/native/darwin/system_load_advisory_monitor_jni.cc b/src/main/native/darwin/system_load_advisory_monitor_jni.cc index 3a597ec0c48a34..53a33d5e5657a0 100644 --- a/src/main/native/darwin/system_load_advisory_monitor_jni.cc +++ b/src/main/native/darwin/system_load_advisory_monitor_jni.cc @@ -15,8 +15,8 @@ #include #include +#include "src/main/cpp/util/logging.h" #include "src/main/native/darwin/util.h" -#include "src/main/native/macros.h" #include "src/main/native/unix_jni.h" namespace blaze_jni { @@ -24,9 +24,6 @@ namespace blaze_jni { static int gSystemLoadAdvisoryNotifyToken = 0; void portable_start_system_load_advisory_monitoring() { - // To test use: - // /usr/bin/log stream -level debug \ - // --predicate '(subsystem == "build.bazel")' // We install a test notification as well that can be used for testing. static dispatch_once_t once_token; dispatch_once(&once_token, ^{ @@ -38,15 +35,14 @@ void portable_start_system_load_advisory_monitoring() { int status = notify_register_dispatch(kIOSystemLoadAdvisoryNotifyName, &gSystemLoadAdvisoryNotifyToken, queue, handler); - CHECK(status == NOTIFY_STATUS_OK); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); // This is registered solely so we can test the system from end-to-end. // Using the Apple notification requires admin access. int testToken; status = notify_register_dispatch( "com.google.bazel.test.SystemLoadAdvisory", &testToken, queue, handler); - CHECK(status == NOTIFY_STATUS_OK); - log_if_possible("system load advisory monitoring registered"); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); }); } @@ -54,30 +50,28 @@ int portable_system_load_advisory() { uint64_t state; uint32_t status = notify_get_state(gSystemLoadAdvisoryNotifyToken, &state); if (status != NOTIFY_STATUS_OK) { - log_if_possible("error: notify_get_state failed (%d)", status); - return -1; + BAZEL_LOG(FATAL) << "notify_get_state failed:" << status; } IOSystemLoadAdvisoryLevel advisoryLevel = (IOSystemLoadAdvisoryLevel)state; int load = -1; switch (advisoryLevel) { case kIOSystemLoadAdvisoryLevelGreat: - log_if_possible("system load advisory great (0) anomaly"); + BAZEL_LOG(USER) << "system load advisory great (0) anomaly"; load = 0; break; case kIOSystemLoadAdvisoryLevelOK: - log_if_possible("system load advisory ok (25) anomaly"); + BAZEL_LOG(USER) << "system load advisory ok (25) anomaly"; load = 25; break; case kIOSystemLoadAdvisoryLevelBad: - log_if_possible("system load advisory bad (75) anomaly"); + BAZEL_LOG(USER) << "system load advisory bad (75) anomaly"; load = 75; break; } if (load == -1) { - log_if_possible("error: unknown system load advisory level: %d", - (int)advisoryLevel); + BAZEL_LOG(FATAL) << "unknown system load advisory level: " << advisoryLevel; } return load; diff --git a/src/main/native/darwin/system_memory_pressure_jni.cc b/src/main/native/darwin/system_memory_pressure_jni.cc index 612c7d23dcde10..6c6ce7e344be23 100644 --- a/src/main/native/darwin/system_memory_pressure_jni.cc +++ b/src/main/native/darwin/system_memory_pressure_jni.cc @@ -14,16 +14,14 @@ #include +#include "src/main/cpp/util/logging.h" #include "src/main/native/darwin/util.h" -#include "src/main/native/macros.h" #include "src/main/native/unix_jni.h" namespace blaze_jni { void portable_start_memory_pressure_monitoring() { // To test use: - // /usr/bin/log stream -level debug \ - // --predicate '(subsystem == "build.bazel")' // sudo memory_pressure -S -l warn // sudo memory_pressure -S -l critical // or use the test notifications that we register. @@ -33,19 +31,19 @@ void portable_start_memory_pressure_monitoring() { dispatch_source_t source = dispatch_source_create( DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL, queue); - CHECK(source != nullptr); + BAZEL_CHECK_NE(source, nullptr); dispatch_source_set_event_handler(source, ^{ dispatch_source_memorypressure_flags_t pressureLevel = dispatch_source_get_data(source); if (pressureLevel == DISPATCH_MEMORYPRESSURE_WARN) { - log_if_possible("memory pressure warning anomaly"); + BAZEL_LOG(USER) << "memory pressure warning anomaly"; memory_pressure_callback(MemoryPressureLevelWarning); } else if (pressureLevel == DISPATCH_MEMORYPRESSURE_CRITICAL) { - log_if_possible("memory pressure critical anomaly"); + BAZEL_LOG(USER) << "memory pressure critical anomaly"; memory_pressure_callback(MemoryPressureLevelCritical); } else { - log_if_possible("error: unknown memory pressure critical level: %d", - (int)pressureLevel); + BAZEL_LOG(FATAL) << "unknown memory pressure critical level: " + << pressureLevel; } }); dispatch_resume(source); @@ -55,18 +53,17 @@ void portable_start_memory_pressure_monitoring() { int32_t status = notify_register_dispatch( "com.google.bazel.test.memorypressurelevel.warning", &testToken, queue, ^(int state) { - log_if_possible("memory pressure test warning anomaly"); + BAZEL_LOG(USER) << "memory pressure test warning anomaly"; memory_pressure_callback(MemoryPressureLevelWarning); }); - CHECK(status == NOTIFY_STATUS_OK); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); status = notify_register_dispatch( "com.google.bazel.test.memorypressurelevel.critical", &testToken, queue, ^(int state) { - log_if_possible("memory pressure test critical anomaly"); + BAZEL_LOG(USER) << "memory pressure test critical anomaly"; memory_pressure_callback(MemoryPressureLevelCritical); }); - CHECK(status == NOTIFY_STATUS_OK); - log_if_possible("memory pressure monitoring registered"); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); }); } diff --git a/src/main/native/darwin/system_suspension_monitor_jni.cc b/src/main/native/darwin/system_suspension_monitor_jni.cc index 4b0450a10efc03..3483aa79356b93 100644 --- a/src/main/native/darwin/system_suspension_monitor_jni.cc +++ b/src/main/native/darwin/system_suspension_monitor_jni.cc @@ -16,8 +16,8 @@ #include #include +#include "src/main/cpp/util/logging.h" #include "src/main/native/darwin/util.h" -#include "src/main/native/macros.h" #include "src/main/native/unix_jni.h" namespace blaze_jni { @@ -37,7 +37,7 @@ static void SleepCallBack(void *refcon, io_service_t service, break; case kIOMessageSystemWillSleep: - log_if_possible("suspend anomaly due to kIOMessageSystemWillSleep"); + BAZEL_LOG(USER) << "suspend anomaly due to kIOMessageSystemWillSleep"; suspend_callback(SuspensionReasonSleep); // This needs to be acknowledged to allow sleep. IOAllowPowerChange(state->connect_port, (intptr_t)message_argument); @@ -57,7 +57,7 @@ static void SleepCallBack(void *refcon, io_service_t service, // wasn't. I haven't come up with an smart way of avoiding this issue, but // I don't think we really care. Over reporting "suspensions" is better // than under reporting them. - log_if_possible("suspend anomaly due to kIOMessageSystemHasPoweredOn"); + BAZEL_LOG(USER) << "suspend anomaly due to kIOMessageSystemHasPoweredOn"; suspend_callback(SuspensionReasonWake); break; @@ -83,11 +83,9 @@ void portable_start_suspend_monitoring() { // Register to receive system sleep notifications. // Testing needs to be done manually. Use the logging to verify // that sleeps are being caught here. - // `/usr/bin/log \ - // stream -level debug --predicate '(subsystem == "build.bazel")'` suspend_state.connect_port = IORegisterForSystemPower( &suspend_state, ¬ifyPortRef, SleepCallBack, ¬ifierObject); - CHECK(suspend_state.connect_port != MACH_PORT_NULL); + BAZEL_CHECK_NE(suspend_state.connect_port, MACH_PORT_NULL); IONotificationPortSetDispatchQueue(notifyPortRef, queue); // Register to deal with SIGCONT. @@ -98,24 +96,23 @@ void portable_start_suspend_monitoring() { // having this functionality gives us some ability to unit test suspension // counts. sig_t signal_val = signal(SIGCONT, SIG_IGN); - CHECK(signal_val != SIG_ERR); + BAZEL_CHECK_NE(signal_val, SIG_ERR); dispatch_source_t signal_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGCONT, 0, queue); - CHECK(signal_source != nullptr); + BAZEL_CHECK_NE(signal_source, nullptr); dispatch_source_set_event_handler(signal_source, ^{ - log_if_possible("suspend anomaly due to SIGCONT"); + BAZEL_LOG(USER) << "suspend anomaly due to SIGCONT"; suspend_callback(SuspensionReasonSIGCONT); }); dispatch_resume(signal_source); signal_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTSTP, 0, queue); - CHECK(signal_source != nullptr); + BAZEL_CHECK_NE(signal_source, nullptr); dispatch_source_set_event_handler(signal_source, ^{ - log_if_possible("suspend anomaly due to SIGTSTP"); + BAZEL_LOG(USER) << "suspend anomaly due to SIGTSTP"; suspend_callback(SuspensionReasonSIGTSTP); }); dispatch_resume(signal_source); - log_if_possible("suspend monitoring registered"); }); } diff --git a/src/main/native/darwin/system_thermal_monitor_jni.cc b/src/main/native/darwin/system_thermal_monitor_jni.cc index 6b3631597a67b6..baa6e212468807 100644 --- a/src/main/native/darwin/system_thermal_monitor_jni.cc +++ b/src/main/native/darwin/system_thermal_monitor_jni.cc @@ -16,8 +16,8 @@ #include #include +#include "src/main/cpp/util/logging.h" #include "src/main/native/darwin/util.h" -#include "src/main/native/macros.h" #include "src/main/native/unix_jni.h" namespace blaze_jni { @@ -27,41 +27,37 @@ static int gThermalNotifyToken = 0; static int thermal_load_from_token(int token) { uint64_t state; uint32_t status = notify_get_state(token, &state); - if (status != NOTIFY_STATUS_OK) { - log_if_possible("error: notify_get_state failed (%d)", status); - return -1; - } + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); OSThermalPressureLevel thermalLevel = (OSThermalPressureLevel)state; int load = -1; switch (thermalLevel) { case kOSThermalPressureLevelNominal: - log_if_possible("thermal pressure nominal (0) anomaly"); + BAZEL_LOG(USER) << "thermal pressure nominal (0) anomaly"; load = 0; break; case kOSThermalPressureLevelModerate: - log_if_possible("thermal pressure moderate (33) anomaly "); + BAZEL_LOG(USER) << "thermal pressure moderate (33) anomaly "; load = 33; break; case kOSThermalPressureLevelHeavy: - log_if_possible("thermal pressure heavy (50) anomaly"); + BAZEL_LOG(USER) << "thermal pressure heavy (50) anomaly"; load = 50; break; case kOSThermalPressureLevelTrapping: - log_if_possible("thermal pressure trapping (90) anomaly"); + BAZEL_LOG(USER) << "thermal pressure trapping (90) anomaly"; load = 90; break; case kOSThermalPressureLevelSleeping: - log_if_possible("thermal pressure sleeping (100) anomaly"); + BAZEL_LOG(USER) << "thermal pressure sleeping (100) anomaly"; load = 100; break; } if (load == -1) { - log_if_possible("error: unknown thermal pressure level: %d", - (int)thermalLevel); + BAZEL_LOG(FATAL) << "unknown thermal pressure level: " << thermalLevel; } return load; @@ -69,8 +65,6 @@ static int thermal_load_from_token(int token) { void portable_start_thermal_monitoring() { // To test use: - // /usr/bin/log stream -level debug \ - // --predicate '(subsystem == "build.bazel")' // sudo thermal simulate cpu {nominal|moderate|heavy|trapping|sleeping} // Note that we install the test notification as well that can be used for // testing. @@ -84,7 +78,7 @@ void portable_start_thermal_monitoring() { int status = notify_register_dispatch(kOSThermalNotificationPressureLevelName, &gThermalNotifyToken, queue, handler); - CHECK(status == NOTIFY_STATUS_OK); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); // This is registered solely so we can test the system from end-to-end. // Using the Apple notification requires admin access. @@ -92,8 +86,8 @@ void portable_start_thermal_monitoring() { status = notify_register_dispatch("com.google.bazel.test.thermalpressurelevel", &testToken, queue, handler); - CHECK(status == NOTIFY_STATUS_OK); - log_if_possible("thermal monitoring registered"); + BAZEL_CHECK_EQ(status, NOTIFY_STATUS_OK); + BAZEL_LOG(INFO) << "thermal monitoring registered"; }); } diff --git a/src/main/native/darwin/util.cc b/src/main/native/darwin/util.cc index 5a4aad70ecce58..97090938d89307 100644 --- a/src/main/native/darwin/util.cc +++ b/src/main/native/darwin/util.cc @@ -14,7 +14,7 @@ #include "src/main/native/darwin/util.h" -#include "src/main/native/macros.h" +#include "src/main/cpp/util/logging.h" namespace bazel { namespace darwin { @@ -24,25 +24,10 @@ dispatch_queue_t JniDispatchQueue() { static dispatch_queue_t queue; dispatch_once(&once_token, ^{ queue = dispatch_queue_create("build.bazel.jni", DISPATCH_QUEUE_SERIAL); - CHECK(queue); + BAZEL_CHECK_NE(queue, nullptr); }); return queue; } -os_log_t JniOSLog() { - static dispatch_once_t once_token; - static os_log_t log = nullptr; - // On macOS < 10.12, os_log_create is not available. Since we target 10.10, - // this will be weakly linked and can be checked for availability at run - // time. - if (&os_log_create != nullptr) { - dispatch_once(&once_token, ^{ - log = os_log_create("build.bazel", "jni"); - CHECK(log); - }); - } - return log; -} - } // namespace darwin } // namespace bazel diff --git a/src/main/native/darwin/util.h b/src/main/native/darwin/util.h index 871f2d50831bd5..c9bda1d062b4e4 100644 --- a/src/main/native/darwin/util.h +++ b/src/main/native/darwin/util.h @@ -24,26 +24,8 @@ namespace darwin { // Queue used for all of our anomaly tracking. dispatch_queue_t JniDispatchQueue(); -// Log used for all of our anomaly logging. -// Logging can be traced using: -// `log stream -level debug --predicate '(subsystem == "build.bazel")'` -// -// This may return NULL if `os_log_create` is not supported on this version of -// macOS. Use `log_if_possible` to log when supported. -os_log_t JniOSLog(); - } // namespace darwin } // namespace bazel -// The macOS implementation asserts that `msg` be a string literal (not just a -// const char*), so we cannot use a function. -#define log_if_possible(msg...) \ - do { \ - os_log_t log = bazel::darwin::JniOSLog(); \ - if (log != nullptr) { \ - os_log_debug(log, msg); \ - } \ - } while (0); - #endif // BAZEL_SRC_MAIN_NATIVE_DARWIN_JNI_UTIL_H_ diff --git a/src/main/native/macros.h b/src/main/native/macros.h index e635339b848d32..c310d8e0202c50 100644 --- a/src/main/native/macros.h +++ b/src/main/native/macros.h @@ -34,20 +34,4 @@ #define FALLTHROUGH_INTENDED do { } while (0) #endif -#define CHECK(condition) \ - do { \ - if (!(condition)) { \ - fprintf(stderr, "%s:%d: check failed: %s\n", \ - __FILE__, __LINE__, #condition); \ - abort(); \ - } \ - } while (0) - -#define CHECK_EQ(a, b) CHECK((a) == (b)) -#define CHECK_NEQ(a, b) CHECK((a) != (b)) -#define CHECK_GT(a, b) CHECK((a) > (b)) -#define CHECK_GE(a, b) CHECK((a) >= (b)) -#define CHECK_LT(a, b) CHECK((a) < (b)) -#define CHECK_LE(a, b) CHECK((a) <= (b)) - #endif // MACROS_H__ diff --git a/src/main/native/unix_jni.cc b/src/main/native/unix_jni.cc index 24be06ca8d24b7..14a3cfbfe12fc4 100644 --- a/src/main/native/unix_jni.cc +++ b/src/main/native/unix_jni.cc @@ -35,6 +35,7 @@ #include #include +#include "src/main/cpp/util/logging.h" #include "src/main/cpp/util/port.h" #include "src/main/native/latin1_jni_path.h" #include "src/main/native/macros.h" @@ -55,9 +56,7 @@ static void PostException(JNIEnv *env, const char *exception_classname, success = env->ThrowNew(exception_class, message.c_str()) == 0; } if (!success) { - fprintf(stderr, "error: Failure to throw java error: %s\n", - message.c_str()); - abort(); // panic! + BAZEL_LOG(FATAL) << "Failure to throw java error: " << message.c_str(); } } @@ -174,8 +173,9 @@ static bool PostRuntimeException(JNIEnv *env, int error_number, env->ThrowNew(exception_class, message.c_str()); return true; } else { - abort(); // panic! - return false; // Not reachable. + BAZEL_LOG(FATAL) << "Unable to find exception_class: " + << exception_classname; + return false; } } @@ -207,19 +207,20 @@ static void PerformIntegerValueCallback(jobject object, const char *callback, if (status == JNI_EDETACHED) { attach_current_thread = true; } else { - CHECK_EQ(status, JNI_OK); + BAZEL_CHECK_EQ(status, JNI_OK); } if (attach_current_thread) { - CHECK_EQ(java_vm->AttachCurrentThread((void **)&java_env, nullptr), 0); + BAZEL_CHECK_EQ(java_vm->AttachCurrentThread((void **)&java_env, nullptr), + 0); } jclass clazz = java_env->GetObjectClass(object); - CHECK_NEQ(clazz, nullptr); + BAZEL_CHECK_NE(clazz, nullptr); jmethodID method_id = java_env->GetMethodID(clazz, callback, "(I)V"); - CHECK_NEQ(method_id, nullptr); + BAZEL_CHECK_NE(method_id, nullptr); java_env->CallVoidMethod(object, method_id, value); if (attach_current_thread) { - CHECK_EQ(java_vm->DetachCurrentThread(), JNI_OK); + BAZEL_CHECK_EQ(java_vm->DetachCurrentThread(), JNI_OK); } } @@ -294,14 +295,14 @@ static jmethodID dirents_ctor = nullptr; static jclass makeStaticClass(JNIEnv *env, const char *name) { jclass lookup_result = env->FindClass(name); - CHECK(lookup_result != nullptr); + BAZEL_CHECK_NE(lookup_result, nullptr); return static_cast(env->NewGlobalRef(lookup_result)); } static jmethodID getConstructorID(JNIEnv *env, jclass clazz, const char *parameters) { jmethodID method = env->GetMethodID(clazz, "", parameters); - CHECK(method != nullptr); + BAZEL_CHECK_NE(method, nullptr); return method; } @@ -339,7 +340,7 @@ static void SetIntField(JNIEnv *env, const char *name, int val) { jfieldID fid = env->GetFieldID(clazz, name, "I"); - CHECK(fid != nullptr); + BAZEL_CHECK_NE(fid, nullptr); env->SetIntField(object, fid, val); } @@ -780,9 +781,9 @@ Java_com_google_devtools_build_lib_unix_NativePosixFiles_readdir(JNIEnv *env, jbyteArray types_obj = nullptr; if (read_types != 'n') { - CHECK(len == types.size()); + BAZEL_CHECK_EQ(len, types.size()); types_obj = env->NewByteArray(len); - CHECK(types_obj); + BAZEL_CHECK_NE(types_obj, nullptr); if (len > 0) { env->SetByteArrayRegion(types_obj, 0, len, &types[0]); } @@ -891,7 +892,7 @@ static void PostDeleteTreesBelowException( // dir_path buffer is still empty but we have the full path in entry. path = entry; } - CHECK(!env->ExceptionOccurred()); + BAZEL_CHECK(!env->ExceptionOccurred()); PostException(env, errno, std::string(function) + " (" + path + ")"); } @@ -1024,7 +1025,7 @@ static int DeleteTreesBelow(JNIEnv* env, std::vector* dir_path, const int dir_fd, const char* entry) { DIR *dir = ForceOpendir(env, *dir_path, dir_fd, entry); if (dir == nullptr) { - CHECK(env->ExceptionOccurred() != nullptr); + BAZEL_CHECK_NE(env->ExceptionOccurred(), nullptr); return -1; } @@ -1053,7 +1054,7 @@ static int DeleteTreesBelow(JNIEnv* env, std::vector* dir_path, bool is_dir; if (IsSubdir(env, *dir_path, dirfd(dir), de, &is_dir) == -1) { - CHECK(env->ExceptionOccurred() != nullptr); + BAZEL_CHECK_NE(env->ExceptionOccurred(), nullptr); break; } if (is_dir) { @@ -1065,7 +1066,7 @@ static int DeleteTreesBelow(JNIEnv* env, std::vector* dir_path, if (env->ExceptionOccurred() == nullptr) { for (const auto &file : dir_files) { if (ForceDelete(env, *dir_path, dirfd(dir), file.c_str(), false) == -1) { - CHECK(env->ExceptionOccurred() != nullptr); + BAZEL_CHECK_NE(env->ExceptionOccurred(), nullptr); break; } } @@ -1075,11 +1076,11 @@ static int DeleteTreesBelow(JNIEnv* env, std::vector* dir_path, if (env->ExceptionOccurred() == nullptr) { for (const auto &subdir : dir_subdirs) { if (DeleteTreesBelow(env, dir_path, dirfd(dir), subdir.c_str()) == -1) { - CHECK(env->ExceptionOccurred() != nullptr); + BAZEL_CHECK_NE(env->ExceptionOccurred(), nullptr); break; } if (ForceDelete(env, *dir_path, dirfd(dir), subdir.c_str(), true) == -1) { - CHECK(env->ExceptionOccurred() != nullptr); + BAZEL_CHECK_NE(env->ExceptionOccurred(), nullptr); break; } } @@ -1108,9 +1109,9 @@ Java_com_google_devtools_build_lib_unix_NativePosixFiles_deleteTreesBelow( const char *path_chars = GetStringLatin1Chars(env, path); std::vector dir_path; if (DeleteTreesBelow(env, &dir_path, AT_FDCWD, path_chars) == -1) { - CHECK(env->ExceptionOccurred() != nullptr); + BAZEL_CHECK_NE(env->ExceptionOccurred(), nullptr); } - CHECK(dir_path.empty()); + BAZEL_CHECK(dir_path.empty()); ReleaseStringLatin1Chars(path_chars); } diff --git a/src/main/native/unix_jni_bsd.cc b/src/main/native/unix_jni_bsd.cc index cb9093f62b17de..932867ff8bb452 100644 --- a/src/main/native/unix_jni_bsd.cc +++ b/src/main/native/unix_jni_bsd.cc @@ -36,7 +36,6 @@ #include -#include "src/main/native/macros.h" #include "src/main/native/unix_jni.h" namespace blaze_jni { @@ -66,8 +65,6 @@ int StatSeconds(const portable_stat_struct &statbuf, StatTimes t) { return statbuf.st_ctime; case STAT_MTIME: return statbuf.st_mtime; - default: - CHECK(false); } } @@ -79,8 +76,6 @@ int StatNanoSeconds(const portable_stat_struct &statbuf, StatTimes t) { return statbuf.st_ctimespec.tv_nsec; case STAT_MTIME: return statbuf.st_mtimespec.tv_nsec; - default: - CHECK(false); } } diff --git a/src/main/native/unix_jni_linux.cc b/src/main/native/unix_jni_linux.cc index 9caae5662a2cfc..4855ab0b1bbb26 100644 --- a/src/main/native/unix_jni_linux.cc +++ b/src/main/native/unix_jni_linux.cc @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - #include #include #include @@ -21,7 +20,6 @@ #include -#include "src/main/native/macros.h" #include "src/main/native/unix_jni.h" namespace blaze_jni { @@ -57,8 +55,6 @@ int StatSeconds(const portable_stat_struct &statbuf, StatTimes t) { return statbuf.st_ctim.tv_sec; case STAT_MTIME: return statbuf.st_mtim.tv_sec; - default: - CHECK(false); } return 0; } @@ -71,8 +67,6 @@ int StatNanoSeconds(const portable_stat_struct &statbuf, StatTimes t) { return statbuf.st_ctim.tv_nsec; case STAT_MTIME: return statbuf.st_mtim.tv_nsec; - default: - CHECK(false); } return 0; }