Skip to content

Commit

Permalink
feat: add option to set debug log level (#1107)
Browse files Browse the repository at this point in the history
* feat: add logger output level

* remove test function

* add separate init log level

* refactor: rename of internal log macros

* update CHANGELOG.md

* applied feedback changing log macros

* missed one F

* remove overdesigned init log level

* changelog update
  • Loading branch information
JoshuaMoelans authored Dec 19, 2024
1 parent 19ba6d5 commit b24b30c
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 100 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

**Features**:

- Add option to set debug log level. ([#1107](https://github.com/getsentry/sentry-native/pull/1107))

## 0.7.17

**Features**:
Expand Down
9 changes: 8 additions & 1 deletion include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,8 @@ SENTRY_API const char *sentry_options_get_user_agent(
const sentry_options_t *opts);

/**
* Enables or disables debug printing mode.
* Enables or disables debug printing mode. To change the log level from the
* default DEBUG level, use `sentry_options_set_logger_level`.
*/
SENTRY_API void sentry_options_set_debug(sentry_options_t *opts, int debug);

Expand All @@ -1078,6 +1079,12 @@ SENTRY_API void sentry_options_set_debug(sentry_options_t *opts, int debug);
*/
SENTRY_API int sentry_options_get_debug(const sentry_options_t *opts);

/**
* Sets the level of the logger. Has no effect if `debug` is not set to true.
*/
SENTRY_API void sentry_options_set_logger_level(
sentry_options_t *opts, sentry_level_t level);

/**
* Sets the number of breadcrumbs being tracked and attached to events.
*
Expand Down
8 changes: 4 additions & 4 deletions src/backends/sentry_backend_breakpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ breakpad_backend_callback(const google_breakpad::MinidumpDescriptor &descriptor,
void *UNUSED(context), bool succeeded)
#endif
{
SENTRY_DEBUG("entering breakpad minidump callback");
SENTRY_INFO("entering breakpad minidump callback");

// this is a bit strange, according to docs, `succeeded` should be true when
// a minidump file was successfully generated. however, when running our
Expand Down Expand Up @@ -125,7 +125,7 @@ breakpad_backend_callback(const google_breakpad::MinidumpDescriptor &descriptor,
uctx = &uctx_data;
#endif

SENTRY_TRACE("invoking `on_crash` hook");
SENTRY_DEBUG("invoking `on_crash` hook");
sentry_value_t result
= options->on_crash_func(uctx, event, options->on_crash_data);
should_handle = !sentry_value_is_null(result);
Expand Down Expand Up @@ -167,7 +167,7 @@ breakpad_backend_callback(const google_breakpad::MinidumpDescriptor &descriptor,
sentry__path_remove(dump_path);
sentry__path_free(dump_path);
} else {
SENTRY_TRACE("event was discarded by the `on_crash` hook");
SENTRY_DEBUG("event was discarded by the `on_crash` hook");
sentry_value_decref(event);
}

Expand All @@ -176,7 +176,7 @@ breakpad_backend_callback(const google_breakpad::MinidumpDescriptor &descriptor,
sentry__transport_dump_queue(options->transport, options->run);
// and restore the old transport
}
SENTRY_DEBUG("crash has been captured");
SENTRY_INFO("crash has been captured");

#ifndef SENTRY_PLATFORM_WINDOWS
sentry__leave_signal_handler();
Expand Down
22 changes: 11 additions & 11 deletions src/backends/sentry_backend_crashpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ crashpad_register_wer_module(
}

if (wer_path && sentry__path_is_file(wer_path)) {
SENTRY_TRACEF("registering crashpad WER handler "
SENTRY_DEBUGF("registering crashpad WER handler "
"\"%" SENTRY_PATH_PRI "\"",
wer_path->path);

Expand Down Expand Up @@ -186,7 +186,7 @@ crashpad_backend_flush_scope_to_event(const sentry_path_t *event_path,
sentry_free(mpack);

if (rv != 0) {
SENTRY_DEBUG("flushing scope to msgpack failed");
SENTRY_WARN("flushing scope to msgpack failed");
}
}

Expand Down Expand Up @@ -257,7 +257,7 @@ sentry__crashpad_handler(int signum, siginfo_t *info, ucontext_t *user_context)
{
sentry__page_allocator_enable();
# endif
SENTRY_DEBUG("flushing session and queue before crashpad handler");
SENTRY_INFO("flushing session and queue before crashpad handler");

bool should_dump = true;

Expand All @@ -276,11 +276,11 @@ sentry__crashpad_handler(int signum, siginfo_t *info, ucontext_t *user_context)
uctx.user_context = user_context;
# endif

SENTRY_TRACE("invoking `on_crash` hook");
SENTRY_DEBUG("invoking `on_crash` hook");
crash_event = options->on_crash_func(
&uctx, crash_event, options->on_crash_data);
} else if (options->before_send_func) {
SENTRY_TRACE("invoking `before_send` hook");
SENTRY_DEBUG("invoking `before_send` hook");
crash_event = options->before_send_func(
crash_event, nullptr, options->before_send_data);
}
Expand All @@ -305,12 +305,12 @@ sentry__crashpad_handler(int signum, siginfo_t *info, ucontext_t *user_context)
sentry_transport_free(disk_transport);
}
} else {
SENTRY_TRACE("event was discarded");
SENTRY_DEBUG("event was discarded");
}
sentry__transport_dump_queue(options->transport, options->run);
}

SENTRY_DEBUG("handing control over to crashpad");
SENTRY_INFO("handing control over to crashpad");
// If we __don't__ want a minidump produced by crashpad we need to either
// exit or longjmp at this point. The crashpad client handler which calls
// back here (SetFirstChanceExceptionHandler) does the same if the
Expand Down Expand Up @@ -392,7 +392,7 @@ crashpad_backend_startup(
return 1;
}

SENTRY_TRACEF("starting crashpad backend with handler "
SENTRY_DEBUGF("starting crashpad backend with handler "
"\"%" SENTRY_PATH_PRI "\"",
absolute_handler_path->path);
sentry_path_t *current_run_folder = options->run->run_path;
Expand Down Expand Up @@ -437,7 +437,7 @@ crashpad_backend_startup(
char *minidump_url
= sentry__dsn_get_minidump_url(options->dsn, options->user_agent);
if (minidump_url) {
SENTRY_TRACEF("using minidump URL \"%s\"", minidump_url);
SENTRY_DEBUGF("using minidump URL \"%s\"", minidump_url);
}
bool success = data->client->StartHandler(handler, database, database,
minidump_url ? minidump_url : "", options->proxy ? options->proxy : "",
Expand All @@ -453,7 +453,7 @@ crashpad_backend_startup(
sentry__path_free(absolute_handler_path);

if (success) {
SENTRY_DEBUG("started crashpad client handler");
SENTRY_INFO("started crashpad client handler");
} else {
SENTRY_WARN("failed to start crashpad client handler");
// not calling `shutdown`
Expand Down Expand Up @@ -545,7 +545,7 @@ crashpad_backend_add_breadcrumb(sentry_backend_t *backend,
sentry_free(mpack);

if (rv != 0) {
SENTRY_DEBUG("flushing breadcrumb to msgpack failed");
SENTRY_WARN("flushing breadcrumb to msgpack failed");
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/backends/sentry_backend_inproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ startup_inproc_backend(
stack_t old_sig_stack;
int ret = sigaltstack(NULL, &old_sig_stack);
if (ret == 0 && old_sig_stack.ss_flags == SS_DISABLE) {
SENTRY_TRACEF("installing signal stack (size: %d)", SIGNAL_STACK_SIZE);
SENTRY_DEBUGF("installing signal stack (size: %d)", SIGNAL_STACK_SIZE);
g_signal_stack.ss_sp = sentry_malloc(SIGNAL_STACK_SIZE);
if (!g_signal_stack.ss_sp) {
return 1;
Expand All @@ -98,7 +98,7 @@ startup_inproc_backend(
g_signal_stack.ss_flags = 0;
sigaltstack(&g_signal_stack, 0);
} else if (ret == 0) {
SENTRY_TRACEF("using existing signal stack (size: %d, flags: %d)",
SENTRY_DEBUGF("using existing signal stack (size: %d, flags: %d)",
old_sig_stack.ss_size, old_sig_stack.ss_flags);
} else if (ret == -1) {
SENTRY_WARNF("Failed to query signal stack size: %s", strerror(errno));
Expand Down Expand Up @@ -485,15 +485,15 @@ make_signal_event(
void *backtrace[MAX_FRAMES];
size_t frame_count
= sentry_unwind_stack_from_ucontext(uctx, &backtrace[0], MAX_FRAMES);
SENTRY_TRACEF(
SENTRY_DEBUGF(
"captured backtrace from ucontext with %lu frames", frame_count);
// if unwinding from a ucontext didn't yield any results, try again with a
// direct unwind. this is most likely the case when using `libbacktrace`,
// since that does not allow to unwind from a ucontext at all.
if (!frame_count) {
frame_count = sentry_unwind_stack(NULL, &backtrace[0], MAX_FRAMES);
}
SENTRY_TRACEF("captured backtrace with %lu frames", frame_count);
SENTRY_DEBUGF("captured backtrace with %lu frames", frame_count);

sentry_value_t stacktrace
= sentry_value_new_stacktrace(&backtrace[0], frame_count);
Expand All @@ -518,7 +518,7 @@ make_signal_event(
static void
handle_ucontext(const sentry_ucontext_t *uctx)
{
SENTRY_DEBUG("entering signal handler");
SENTRY_INFO("entering signal handler");

const struct signal_slot *sig_slot = NULL;
for (int i = 0; i < SIGNAL_COUNT; ++i) {
Expand Down Expand Up @@ -550,7 +550,7 @@ handle_ucontext(const sentry_ucontext_t *uctx)
// we process the signal.
if (sentry_options_get_handler_strategy(options)
== SENTRY_HANDLER_STRATEGY_CHAIN_AT_START) {
SENTRY_TRACE("defer to runtime signal handler at start");
SENTRY_DEBUG("defer to runtime signal handler at start");
// there is a good chance that we won't return from the previous
// handler and that would mean we couldn't enter this handler with
// the next signal coming in if we didn't "leave" here.
Expand All @@ -563,7 +563,7 @@ handle_ucontext(const sentry_ucontext_t *uctx)

// let's re-enter because it means this was an actual native crash
sentry__enter_signal_handler();
SENTRY_TRACE(
SENTRY_DEBUG(
"return from runtime signal handler, we handle the signal");
}
#endif
Expand All @@ -578,7 +578,7 @@ handle_ucontext(const sentry_ucontext_t *uctx)
sentry__write_crash_marker(options);

if (options->on_crash_func) {
SENTRY_TRACE("invoking `on_crash` hook");
SENTRY_DEBUG("invoking `on_crash` hook");
event = options->on_crash_func(uctx, event, options->on_crash_data);
should_handle = !sentry_value_is_null(event);
}
Expand All @@ -600,15 +600,15 @@ handle_ucontext(const sentry_ucontext_t *uctx)
sentry__transport_dump_queue(disk_transport, options->run);
sentry_transport_free(disk_transport);
} else {
SENTRY_TRACE("event was discarded by the `on_crash` hook");
SENTRY_DEBUG("event was discarded by the `on_crash` hook");
sentry_value_decref(event);
}

// after capturing the crash event, dump all the envelopes to disk
sentry__transport_dump_queue(options->transport, options->run);
}

SENTRY_DEBUG("crash has been captured");
SENTRY_INFO("crash has been captured");

#ifdef SENTRY_PLATFORM_UNIX
// reset signal handlers and invoke the original ones. This will then tear
Expand Down
4 changes: 2 additions & 2 deletions src/modulefinder/sentry_modulefinder_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,9 @@ sentry_get_modules_list(void)
sentry__mutex_lock(&g_mutex);
if (!g_initialized) {
g_modules = sentry_value_new_list();
SENTRY_TRACE("trying to read modules from /proc/self/maps");
SENTRY_DEBUG("trying to read modules from /proc/self/maps");
load_modules(g_modules);
SENTRY_TRACEF("read %zu modules from /proc/self/maps",
SENTRY_DEBUGF("read %zu modules from /proc/self/maps",
sentry_value_get_length(g_modules));
sentry_value_freeze(g_modules);
g_initialized = true;
Expand Down
2 changes: 1 addition & 1 deletion src/path/sentry_path_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ write_buffer_with_flags(
int fd = open(
path->path, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
if (fd < 0) {
SENTRY_TRACEF(
SENTRY_WARNF(
"failed to open file \"%s\" for writing (errno %d, flags %x)",
path->path, errno, flags);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/path/sentry_path_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ sentry__path_current_exe(void)
sentry_path_t *path = path_with_len(MAX_PATH);
size_t len = GetModuleFileNameW(NULL, path->path, MAX_PATH);
if (!len) {
SENTRY_DEBUG("unable to get current exe path");
SENTRY_WARN("unable to get current exe path");
sentry__path_free(path);
return NULL;
}
Expand Down
Loading

0 comments on commit b24b30c

Please sign in to comment.