Skip to content

Commit

Permalink
fix: Start logger as early as possible (getsentry#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatinem authored and irov committed Jun 17, 2020
1 parent f5e5bf2 commit 19320b9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
13 changes: 10 additions & 3 deletions src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ sentry_init(sentry_options_t *options)
{
sentry_shutdown();

if (sentry__path_create_dir_all(options->database_path)) {
sentry_options_free(options);
return 1;
sentry_logger_t logger = { NULL };
if (options->debug) {
logger.logger = options->logger;
}
sentry__logger_set_global(logger);

sentry_path_t *database_path = options->database_path;
options->database_path = sentry__path_absolute(database_path);
if (options->database_path) {
Expand All @@ -76,6 +78,11 @@ sentry_init(sentry_options_t *options)
SENTRY_DEBUGF("using database path \"%" SENTRY_PATH_PRI "\"",
options->database_path->path);

if (sentry__path_create_dir_all(options->database_path)) {
sentry_options_free(options);
return 1;
}

// try to create and lock our run folder as early as possibly, since it is
// fallible. since it does locking, it will not interfere with run folder
// enumeration.
Expand Down
17 changes: 11 additions & 6 deletions src/sentry_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
#include <stdio.h>
#include <string.h>

static sentry_logger_t g_logger = { NULL };

void
sentry__logger_set_global(sentry_logger_t logger)
{
g_logger = logger;
}

#if defined(SENTRY_PLATFORM_ANDROID)

# include <android/log.h>
Expand Down Expand Up @@ -77,14 +85,11 @@ sentry__logger_describe(sentry_level_t level)
void
sentry__logger_log(sentry_level_t level, const char *message, ...)
{
const sentry_options_t *options = sentry_get_options();
if (options && options->logger_func && sentry_options_get_debug(options)) {

sentry_logger_t logger = g_logger;
if (logger.logger) {
va_list args;
va_start(args, message);

options->logger_func(level, message, args, options->logger_data);

logger.logger(level, message, args);
va_end(args);
}
}
6 changes: 6 additions & 0 deletions src/sentry_logger.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "sentry_boot.h"

typedef struct {
void (*logger)(sentry_level_t level, const char *message, va_list args);
} sentry_logger_t;

void sentry__logger_set_global(sentry_logger_t logger);

void sentry__logger_defaultlogger(
sentry_level_t level, const char *message, va_list args, void *data);

Expand Down

0 comments on commit 19320b9

Please sign in to comment.