diff --git a/CHANGELOG.md b/CHANGELOG.md index 796c54fd4..fa9dff983 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ **Features**: +- Add `sentry_is_enabled` for checking whether the SDK has been initialized. ([#2045](https://github.com/getsentry/sentry-native/pull/2045)) - Add `sentry_event_set_level` for setting the level of an individual event. ([#2038](https://github.com/getsentry/sentry-native/pull/2038)) **Fixes**: diff --git a/include/sentry.h b/include/sentry.h index f4e607566..449e4a455 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -2382,6 +2382,16 @@ SENTRY_API void sentry_options_set_backend( */ SENTRY_API int sentry_init(sentry_options_t *options); +/** + * Returns whether the Sentry SDK has been initialized successfully and has not + * yet been closed. + * + * Returns 1 after `sentry_init()` completes successfully and until + * `sentry_close()` completes. Returns 0 before initialization, after failed + * initialization, and after close. + */ +SENTRY_API int sentry_is_enabled(void); + /** * Instructs the transport to flush its send-queue. * diff --git a/src/sentry_core.c b/src/sentry_core.c index 31e453ea0..da00f19ba 100644 --- a/src/sentry_core.c +++ b/src/sentry_core.c @@ -343,6 +343,14 @@ sentry_init(sentry_options_t *options) return 1; } +int +sentry_is_enabled(void) +{ + int enabled = sentry__options_lock() != NULL; + sentry__options_unlock(); + return enabled; +} + int sentry_flush(uint64_t timeout) { diff --git a/tests/unit/test_failures.c b/tests/unit/test_failures.c index 169d0fb24..f84785fe3 100644 --- a/tests/unit/test_failures.c +++ b/tests/unit/test_failures.c @@ -34,5 +34,6 @@ SENTRY_TEST(init_failure) } #else TEST_CHECK(rv != 0); + TEST_CHECK(!sentry_is_enabled()); #endif } diff --git a/tests/unit/test_uninit.c b/tests/unit/test_uninit.c index 3c510da0a..11dfefa2d 100644 --- a/tests/unit/test_uninit.c +++ b/tests/unit/test_uninit.c @@ -42,6 +42,25 @@ SENTRY_TEST(uninitialized) sentry_close(); } +SENTRY_TEST(is_enabled) +{ + TEST_CHECK(!sentry_is_enabled()); + + SENTRY_TEST_OPTIONS_NEW(options); + TEST_CHECK(sentry_init(options) == 0); + TEST_CHECK(sentry_is_enabled()); + + sentry_close(); + TEST_CHECK(!sentry_is_enabled()); + + SENTRY_TEST_OPTIONS_NEW(reinit_options); + TEST_CHECK(sentry_init(reinit_options) == 0); + TEST_CHECK(sentry_is_enabled()); + + sentry_close(); + TEST_CHECK(!sentry_is_enabled()); +} + SENTRY_TEST(empty_transport) { SENTRY_TEST_OPTIONS_NEW(options); diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index bf5f7af10..ca01f5aaf 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -221,6 +221,7 @@ XX(installation_id) XX(internal_uuid_api) XX(invalid_dsn) XX(invalid_proxy) +XX(is_enabled) XX(iso_time) XX(lazy_attachments) XX(logger_enable_disable_functionality)