logs: make kXDGCacheHome if it doesn't exist (#13758)

This commit is contained in:
Michael Lingelbach
2021-01-16 12:14:59 -08:00
committed by GitHub
parent 0fad952a2b
commit abbfaf286f
3 changed files with 20 additions and 12 deletions

View File

@@ -1316,8 +1316,9 @@ file when reading and include:
==============================================================================
Standard Paths *standard-path*
Nvim stores configuration and data in standard locations. Plugins are strongly
encouraged to follow this pattern also. Use |stdpath()| to get the paths.
Nvim stores configuration, data, and logs in standard locations. Plugins are
strongly encouraged to follow this pattern also. Use |stdpath()| to get the
paths.
*base-directories* *xdg*
The "base" (root) directories conform to the XDG Base Directory Specification.
@@ -1338,12 +1339,12 @@ DATA DIRECTORY (DEFAULT) ~
Note: Throughout the user manual these defaults are used as placeholders, e.g.
"~/.config" is understood to mean "$XDG_CONFIG_HOME or ~/.config".
LOG FILE *$NVIM_LOG_FILE*
LOG FILE *$NVIM_LOG_FILE* *E5010*
Besides 'debug' and 'verbose', Nvim keeps a general log file for internal
debugging, plugins and RPC clients. >
:echo $NVIM_LOG_FILE
Usually the file is ~/.cache/nvim/log unless that path is inaccessible
or if $NVIM_LOG_FILE was set before |startup|.
By default, the file is located at stdpath('cache')/log unless that path
is inaccessible or if $NVIM_LOG_FILE was set before |startup|.
vim:noet:tw=78:ts=8:ft=help:norl:

View File

@@ -22,6 +22,7 @@
#include "nvim/os/time.h"
#define LOG_FILE_ENV "NVIM_LOG_FILE"
#define LOGERR "E5010: "
/// Cached location of the expanded log file path decided by log_path_init().
static char log_file_path[MAXPATHL + 1] = { 0 };
@@ -69,6 +70,19 @@ static bool log_path_init(void)
|| log_file_path[0] == '\0'
|| os_isdir((char_u *)log_file_path)
|| !log_try_create(log_file_path)) {
// Make kXDGCacheHome if it does not exist.
char *cachehome = get_xdg_home(kXDGCacheHome);
char *failed_dir = NULL;
if (!os_isdir((char_u *)cachehome)) {
int ret;
if ((ret = os_mkdir_recurse(cachehome, 0700, &failed_dir)) != 0) {
EMSG3(_(LOGERR "Failed to create directory %s "
"for writing logs: %s"),
failed_dir, os_strerror(ret));
}
}
XFREE_CLEAR(failed_dir);
XFREE_CLEAR(cachehome);
// Invalid $NVIM_LOG_FILE or failed to expand; fall back to default.
char *defaultpath = stdpaths_user_cache_subpath("log");
size_t len = xstrlcpy(log_file_path, defaultpath, size);

View File

@@ -233,13 +233,6 @@ describe('startup defaults', function()
}})
eq(xdgcachedir..'/log', string.gsub(eval('$NVIM_LOG_FILE'), '\\', '/'))
end)
it('defaults to .nvimlog if stdpath("cache") is invalid', function()
clear({env={
XDG_CACHE_HOME='Xtest-missing-xdg-dir',
NVIM_LOG_FILE='.', -- Any directory is invalid.
}})
eq('.nvimlog', eval('$NVIM_LOG_FILE'))
end)
end)
end)