From ba0f821b9fbe4ea151a739fdb3111750f897dd7c Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 2 Sep 2026 11:51:58 +0200 Subject: [PATCH] feat: add `sentry_is_enabled()` --- CHANGELOG.md | 1 + include/sentry.h | 10 ++++++++++ src/sentry_core.c | 8 ++++++++ tests/unit/test_failures.c | 1 + tests/unit/test_uninit.c | 19 +++++++++++++++++++ tests/unit/tests.inc | 1 + 6 files changed, 40 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 796c54fd40..fa9dff9831 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 f4e6075665..449e4a455f 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 31e453ea09..da00f19bad 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 169d0fb247..f84785fe30 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 3c510da0ac..11dfefa2de 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 bf5f7af109..ca01f5aaf4 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)