1616#include <uv.h>
1717
1818#include "auto/config.h"
19+ #include "nvim/eval.h"
1920#include "nvim/log.h"
2021#include "nvim/main.h"
2122#include "nvim/message.h"
2223#include "nvim/os/os.h"
2324#include "nvim/os/time.h"
25+ #include "nvim/path.h"
2426#include "nvim/types.h"
2527
26- #define LOG_FILE_ENV "NVIM_LOG_FILE"
27-
2828/// Cached location of the expanded log file path decided by log_path_init().
2929static char log_file_path [MAXPATHL + 1 ] = { 0 };
3030
@@ -52,17 +52,16 @@ static bool log_try_create(char *fname)
5252 return true;
5353}
5454
55- /// Initializes path to log file. Sets $NVIM_LOG_FILE if empty.
55+ /// Initializes the log file path and sets $NVIM_LOG_FILE if empty.
5656///
5757/// Tries $NVIM_LOG_FILE, or falls back to $XDG_STATE_HOME/nvim/log. Failed
5858/// initialization indicates either a bug in expand_env() or both $NVIM_LOG_FILE
5959/// and $HOME environment variables are undefined.
6060static void log_path_init (void )
6161{
6262 size_t size = sizeof (log_file_path );
63- expand_env ((char_u * )"$" LOG_FILE_ENV , (char_u * )log_file_path ,
64- (int )size - 1 );
65- if (strequal ("$" LOG_FILE_ENV , log_file_path )
63+ expand_env ((char_u * )"$" ENV_LOGFILE , (char_u * )log_file_path , (int )size - 1 );
64+ if (strequal ("$" ENV_LOGFILE , log_file_path )
6665 || log_file_path [0 ] == '\0'
6766 || os_isdir ((char_u * )log_file_path )
6867 || !log_try_create (log_file_path )) {
@@ -87,7 +86,7 @@ static void log_path_init(void)
8786 log_file_path [0 ] = '\0' ;
8887 return ;
8988 }
90- os_setenv (LOG_FILE_ENV , log_file_path , true);
89+ os_setenv (ENV_LOGFILE , log_file_path , true);
9190 if (log_dir_failure ) {
9291 WLOG ("Failed to create directory %s for writing logs: %s" ,
9392 failed_dir , os_strerror (log_dir_failure ));
@@ -209,7 +208,7 @@ FILE *open_log_file(void)
209208 // - Directory does not exist
210209 // - File is not writable
211210 do_log_to_file (stderr , LOGLVL_ERR , NULL , __func__ , __LINE__ , true,
212- "failed to open $" LOG_FILE_ENV " (%s): %s" ,
211+ "failed to open $" ENV_LOGFILE " (%s): %s" ,
213212 strerror (errno ), log_file_path );
214213 return stderr ;
215214}
@@ -277,6 +276,9 @@ static bool v_do_log_to_file(FILE *log_file, int log_level, const char *context,
277276 va_list args )
278277 FUNC_ATTR_PRINTF (7 , 0 )
279278{
279+ // Name of the Nvim instance that produced the log.
280+ static char name [16 ] = { 0 };
281+
280282 static const char * log_levels [] = {
281283 [LOGLVL_DBG ] = "DBG" ,
282284 [LOGLVL_INF ] = "INF" ,
@@ -291,8 +293,7 @@ static bool v_do_log_to_file(FILE *log_file, int log_level, const char *context,
291293 return false;
292294 }
293295 char date_time [20 ];
294- if (strftime (date_time , sizeof (date_time ), "%Y-%m-%dT%H:%M:%S" ,
295- & local_time ) == 0 ) {
296+ if (strftime (date_time , sizeof (date_time ), "%Y-%m-%dT%H:%M:%S" , & local_time ) == 0 ) {
296297 return false;
297298 }
298299
@@ -302,14 +303,30 @@ static bool v_do_log_to_file(FILE *log_file, int log_level, const char *context,
302303 millis = (int )curtime .tv_usec / 1000 ;
303304 }
304305
306+ // Get a name for this Nvim instance.
307+ // TODO(justinmk): expose this as v:name ?
308+ if (starting || name [0 ] == '\0' ) {
309+ // Parent servername.
310+ const char * parent = path_tail (os_getenv (ENV_NVIM ));
311+ // Servername. Empty until starting=false.
312+ const char * serv = path_tail (get_vim_var_str (VV_SEND_SERVER ));
313+ if (parent && parent [0 ] != NUL ) {
314+ snprintf (name , sizeof (name ), "%s/c" , parent ); // "/c" indicates child.
315+ } else if (serv && serv [0 ] != NUL ) {
316+ snprintf (name , sizeof (name ), "%s" , serv ? serv : "" );
317+ } else {
318+ int64_t pid = os_get_pid ();
319+ snprintf (name , sizeof (name ), "?.%-5" PRId64 , pid );
320+ }
321+ }
322+
305323 // Print the log message.
306- int64_t pid = os_get_pid ();
307324 int rv = (line_num == -1 || func_name == NULL )
308- ? fprintf (log_file , "%s %s.%03d %-5" PRId64 " %s" ,
309- log_levels [log_level ], date_time , millis , pid ,
325+ ? fprintf (log_file , "%s %s.%03d %-10s %s" ,
326+ log_levels [log_level ], date_time , millis , name ,
310327 (context == NULL ? "?:" : context ))
311- : fprintf (log_file , "%s %s.%03d %-5" PRId64 " %s%s:%d: " ,
312- log_levels [log_level ], date_time , millis , pid ,
328+ : fprintf (log_file , "%s %s.%03d %-10s %s%s:%d: " ,
329+ log_levels [log_level ], date_time , millis , name ,
313330 (context == NULL ? "" : context ),
314331 func_name , line_num );
315332 if (rv < 0 ) {
0 commit comments