mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
log: Make logging thread-safe
This commit is contained in:
parent
9ec240e03f
commit
2a0ff9f5cf
@ -16,29 +16,49 @@
|
|||||||
|
|
||||||
#define USR_LOG_FILE "$HOME/.nvimlog"
|
#define USR_LOG_FILE "$HOME/.nvimlog"
|
||||||
|
|
||||||
|
static uv_mutex_t mutex;
|
||||||
|
|
||||||
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
||||||
# include "log.c.generated.h"
|
# include "log.c.generated.h"
|
||||||
#endif
|
#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,
|
bool do_log(int log_level, const char *func_name, int line_num, bool eol,
|
||||||
const char* fmt, ...) FUNC_ATTR_UNUSED
|
const char* fmt, ...) FUNC_ATTR_UNUSED
|
||||||
{
|
{
|
||||||
|
log_lock();
|
||||||
|
bool ret = false;
|
||||||
FILE *log_file = open_log_file();
|
FILE *log_file = open_log_file();
|
||||||
|
|
||||||
if (log_file == NULL) {
|
if (log_file == NULL) {
|
||||||
return false;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
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);
|
fmt, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
if (log_file != stderr && log_file != stdout) {
|
if (log_file != stderr && log_file != stdout) {
|
||||||
fclose(log_file);
|
fclose(log_file);
|
||||||
}
|
}
|
||||||
|
end:
|
||||||
|
log_unlock();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,6 +177,7 @@ void event_teardown(void)
|
|||||||
/// Needed for unit tests. Must be called after `time_init()`.
|
/// Needed for unit tests. Must be called after `time_init()`.
|
||||||
void early_init(void)
|
void early_init(void)
|
||||||
{
|
{
|
||||||
|
log_init();
|
||||||
fs_init();
|
fs_init();
|
||||||
handle_init();
|
handle_init();
|
||||||
|
|
||||||
|
@ -825,6 +825,7 @@ static void log_server_msg(uint64_t channel_id,
|
|||||||
msgpack_unpack_next(&unpacked, packed->data, packed->size, NULL);
|
msgpack_unpack_next(&unpacked, packed->data, packed->size, NULL);
|
||||||
uint64_t type = unpacked.data.via.array.ptr[0].via.u64;
|
uint64_t type = unpacked.data.via.array.ptr[0].via.u64;
|
||||||
DLOGN("[msgpack-rpc] nvim -> client(%" PRIu64 ") ", channel_id);
|
DLOGN("[msgpack-rpc] nvim -> client(%" PRIu64 ") ", channel_id);
|
||||||
|
log_lock();
|
||||||
FILE *f = open_log_file();
|
FILE *f = open_log_file();
|
||||||
fprintf(f, type ? (type == 1 ? RES : NOT) : REQ);
|
fprintf(f, type ? (type == 1 ? RES : NOT) : REQ);
|
||||||
log_msg_close(f, unpacked.data);
|
log_msg_close(f, unpacked.data);
|
||||||
@ -836,6 +837,7 @@ static void log_client_msg(uint64_t channel_id,
|
|||||||
msgpack_object msg)
|
msgpack_object msg)
|
||||||
{
|
{
|
||||||
DLOGN("[msgpack-rpc] client(%" PRIu64 ") -> nvim ", channel_id);
|
DLOGN("[msgpack-rpc] client(%" PRIu64 ") -> nvim ", channel_id);
|
||||||
|
log_lock();
|
||||||
FILE *f = open_log_file();
|
FILE *f = open_log_file();
|
||||||
fprintf(f, is_request ? REQ : RES);
|
fprintf(f, is_request ? REQ : RES);
|
||||||
log_msg_close(f, msg);
|
log_msg_close(f, msg);
|
||||||
@ -847,6 +849,7 @@ static void log_msg_close(FILE *f, msgpack_object msg)
|
|||||||
fputc('\n', f);
|
fputc('\n', f);
|
||||||
fflush(f);
|
fflush(f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
log_unlock();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user