Skip to content

Commit

Permalink
Make grn_default_{,query_}logger_set_path() thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jul 6, 2017
1 parent e738599 commit b0dedc8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ rotate_log_file(grn_ctx *ctx, const char *current_path)
rename(current_path, rotated_path);
}

static grn_bool logger_inited = GRN_FALSE;
static char *default_logger_path = NULL;
static FILE *default_logger_file = NULL;
static grn_critical_section default_logger_lock;
Expand Down Expand Up @@ -262,6 +263,10 @@ grn_default_logger_get_flags(void)
void
grn_default_logger_set_path(const char *path)
{
if (logger_inited) {
CRITICAL_SECTION_ENTER(default_logger_lock);
}

if (default_logger_path) {
free(default_logger_path);
}
Expand All @@ -271,6 +276,10 @@ grn_default_logger_set_path(const char *path)
} else {
default_logger_path = NULL;
}

if (logger_inited) {
CRITICAL_SECTION_LEAVE(default_logger_lock);
}
}

const char *
Expand Down Expand Up @@ -439,6 +448,8 @@ grn_logger_init(void)
if (!current_logger.log) {
current_logger = default_logger;
}

logger_inited = GRN_TRUE;
}

void
Expand All @@ -450,9 +461,12 @@ grn_logger_fin(grn_ctx *ctx)
default_logger_path = NULL;
}
CRITICAL_SECTION_FIN(default_logger_lock);

logger_inited = GRN_FALSE;
}


static grn_bool query_logger_inited = GRN_FALSE;
static char *default_query_logger_path = NULL;
static FILE *default_query_logger_file = NULL;
static grn_critical_section default_query_logger_lock;
Expand Down Expand Up @@ -617,6 +631,10 @@ grn_default_query_logger_get_flags(void)
void
grn_default_query_logger_set_path(const char *path)
{
if (query_logger_inited) {
CRITICAL_SECTION_ENTER(default_query_logger_lock);
}

if (default_query_logger_path) {
free(default_query_logger_path);
}
Expand All @@ -626,6 +644,10 @@ grn_default_query_logger_set_path(const char *path)
} else {
default_query_logger_path = NULL;
}

if (query_logger_inited) {
CRITICAL_SECTION_LEAVE(default_query_logger_lock);
}
}

const char *
Expand Down Expand Up @@ -767,6 +789,8 @@ grn_query_logger_init(void)
{
current_query_logger = default_query_logger;
CRITICAL_SECTION_INIT(default_query_logger_lock);

query_logger_inited = GRN_TRUE;
}

void
Expand All @@ -778,6 +802,8 @@ grn_query_logger_fin(grn_ctx *ctx)
default_query_logger_path = NULL;
}
CRITICAL_SECTION_FIN(default_query_logger_lock);

query_logger_inited = GRN_FALSE;
}

void
Expand Down

0 comments on commit b0dedc8

Please sign in to comment.