mirror of
https://github.com/neovim/neovim.git
synced 2025-09-28 22:18:33 +00:00
log: Make logging thread-safe
This commit is contained in:
@@ -16,29 +16,49 @@
|
||||
|
||||
#define USR_LOG_FILE "$HOME/.nvimlog"
|
||||
|
||||
static uv_mutex_t mutex;
|
||||
|
||||
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
||||
# include "log.c.generated.h"
|
||||
#endif
|
||||
|
||||
void log_init(void)
|
||||
{
|
||||
uv_mutex_init(&mutex);
|
||||
}
|
||||
|
||||
void log_lock(void)
|
||||
{
|
||||
uv_mutex_lock(&mutex);
|
||||
}
|
||||
|
||||
void log_unlock(void)
|
||||
{
|
||||
uv_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
bool do_log(int log_level, const char *func_name, int line_num, bool eol,
|
||||
const char* fmt, ...) FUNC_ATTR_UNUSED
|
||||
{
|
||||
log_lock();
|
||||
bool ret = false;
|
||||
FILE *log_file = open_log_file();
|
||||
|
||||
if (log_file == NULL) {
|
||||
return false;
|
||||
goto end;
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
bool ret = v_do_log_to_file(log_file, log_level, func_name, line_num, eol,
|
||||
ret = v_do_log_to_file(log_file, log_level, func_name, line_num, eol,
|
||||
fmt, args);
|
||||
va_end(args);
|
||||
|
||||
if (log_file != stderr && log_file != stdout) {
|
||||
fclose(log_file);
|
||||
}
|
||||
end:
|
||||
log_unlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user