mirror of
https://github.com/neovim/neovim.git
synced 2025-09-28 14:08:32 +00:00
log: introduce context
This commit is contained in:
@@ -95,8 +95,12 @@ 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
|
||||
/// @param context description of a shared context or subsystem
|
||||
/// @param func_name function name, or NULL
|
||||
/// @param line_num source line number, or -1
|
||||
bool do_log(int log_level, const char *context, const char *func_name,
|
||||
int line_num, bool eol, const char* fmt, ...)
|
||||
FUNC_ATTR_UNUSED
|
||||
{
|
||||
if (log_level < MIN_LOG_LEVEL) {
|
||||
return false;
|
||||
@@ -112,8 +116,8 @@ bool do_log(int log_level, const char *func_name, int line_num, bool eol,
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
ret = v_do_log_to_file(log_file, log_level, func_name, line_num, eol,
|
||||
fmt, args);
|
||||
ret = v_do_log_to_file(log_file, log_level, context, func_name, line_num,
|
||||
eol, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
if (log_file != stderr && log_file != stdout) {
|
||||
@@ -151,7 +155,7 @@ FILE *open_log_file(void)
|
||||
static bool opening_log_file = false;
|
||||
// check if it's a recursive call
|
||||
if (opening_log_file) {
|
||||
do_log_to_file(stderr, ERROR_LOG_LEVEL, __func__, __LINE__, true,
|
||||
do_log_to_file(stderr, ERROR_LOG_LEVEL, NULL, __func__, __LINE__, true,
|
||||
"Cannot LOG() recursively.");
|
||||
return stderr;
|
||||
}
|
||||
@@ -171,7 +175,7 @@ FILE *open_log_file(void)
|
||||
// - LOG() is called before early_init()
|
||||
// - Directory does not exist
|
||||
// - File is not writable
|
||||
do_log_to_file(stderr, ERROR_LOG_LEVEL, __func__, __LINE__, true,
|
||||
do_log_to_file(stderr, ERROR_LOG_LEVEL, NULL, __func__, __LINE__, true,
|
||||
"Logging to stderr, failed to open $" LOG_FILE_ENV ": %s",
|
||||
log_file_path);
|
||||
return stderr;
|
||||
@@ -201,7 +205,7 @@ void log_callstack_to_file(FILE *log_file, const char *const func_name,
|
||||
// Now we have a command string like:
|
||||
// addr2line -e /path/to/exe -f -p 0x123 0x456 ...
|
||||
|
||||
do_log_to_file(log_file, DEBUG_LOG_LEVEL, func_name, line_num, true,
|
||||
do_log_to_file(log_file, DEBUG_LOG_LEVEL, NULL, func_name, line_num, true,
|
||||
"trace:");
|
||||
FILE *fp = popen(cmdbuf, "r");
|
||||
char linebuf[IOSIZE];
|
||||
@@ -230,22 +234,23 @@ end:
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool do_log_to_file(FILE *log_file, int log_level,
|
||||
static bool do_log_to_file(FILE *log_file, int log_level, const char *context,
|
||||
const char *func_name, int line_num, bool eol,
|
||||
const char* fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
bool ret = v_do_log_to_file(log_file, log_level, func_name, line_num, eol,
|
||||
fmt, args);
|
||||
bool ret = v_do_log_to_file(log_file, log_level, context, func_name,
|
||||
line_num, eol, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool v_do_log_to_file(FILE *log_file, int log_level,
|
||||
const char *func_name, int line_num, bool eol,
|
||||
const char* fmt, va_list args)
|
||||
const char *context, const char *func_name,
|
||||
int line_num, bool eol, const char* fmt,
|
||||
va_list args)
|
||||
{
|
||||
static const char *log_levels[] = {
|
||||
[DEBUG_LOG_LEVEL] = "DEBUG",
|
||||
@@ -268,8 +273,15 @@ static bool v_do_log_to_file(FILE *log_file, int log_level,
|
||||
|
||||
// print the log message prefixed by the current timestamp and pid
|
||||
int64_t pid = os_get_pid();
|
||||
if (fprintf(log_file, "%s %s %" PRId64 "/%s:%d: ", date_time,
|
||||
log_levels[log_level], pid, func_name, line_num) < 0) {
|
||||
int rv = (line_num == -1 || func_name == NULL)
|
||||
? fprintf(log_file, "%s %s %" PRId64 " %s", date_time,
|
||||
log_levels[log_level], pid,
|
||||
(context == NULL ? "?:" : context))
|
||||
: fprintf(log_file, "%s %s %" PRId64 " %s%s:%d: ", date_time,
|
||||
log_levels[log_level], pid,
|
||||
(context == NULL ? "" : context),
|
||||
func_name, line_num);
|
||||
if (rv < 0) {
|
||||
return false;
|
||||
}
|
||||
if (vfprintf(log_file, fmt, args) < 0) {
|
||||
|
Reference in New Issue
Block a user