mirror of
				https://github.com/neovim/neovim.git
				synced 2025-10-26 12:27:24 +00:00 
			
		
		
		
	logging: move to XDG_CACHE_HOME (#13739)
while there is some controversy, stdpath('cache') looks like  a better fit for logs than stdpath('data'): you can remove logs without preventing nvim to work which fits the XDG_CACHE_HOME definition of `user specific non-essential data files`.
			
			
This commit is contained in:
		 Michael Lingelbach
					Michael Lingelbach
				
			
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			 GitHub
						GitHub
					
				
			
						parent
						
							77a6049e07
						
					
				
				
					commit
					ea8756f85d
				
			| @@ -327,7 +327,7 @@ Print version information and exit. | |||||||
| .Sh ENVIRONMENT | .Sh ENVIRONMENT | ||||||
| .Bl -tag -width Fl | .Bl -tag -width Fl | ||||||
| .It Ev NVIM_LOG_FILE | .It Ev NVIM_LOG_FILE | ||||||
| Low-level log file, usually found at ~/.local/share/nvim/log. | Low-level log file, usually found at ~/.cache/nvim/log. | ||||||
| :help $NVIM_LOG_FILE | :help $NVIM_LOG_FILE | ||||||
| .It Ev VIM | .It Ev VIM | ||||||
| Used to locate user files, such as init.vim. | Used to locate user files, such as init.vim. | ||||||
|   | |||||||
| @@ -1342,7 +1342,7 @@ LOG FILE					*$NVIM_LOG_FILE* | |||||||
| Besides 'debug' and 'verbose', Nvim keeps a general log file for internal | Besides 'debug' and 'verbose', Nvim keeps a general log file for internal | ||||||
| debugging, plugins and RPC clients. > | debugging, plugins and RPC clients. > | ||||||
| 	:echo $NVIM_LOG_FILE | 	:echo $NVIM_LOG_FILE | ||||||
| Usually the file is ~/.local/share/nvim/log unless that path is inaccessible | Usually the file is ~/.cache/nvim/log unless that path is inaccessible | ||||||
| or if $NVIM_LOG_FILE was set before |startup|. | or if $NVIM_LOG_FILE was set before |startup|. | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ do | |||||||
|   local function path_join(...) |   local function path_join(...) | ||||||
|     return table.concat(vim.tbl_flatten{...}, path_sep) |     return table.concat(vim.tbl_flatten{...}, path_sep) | ||||||
|   end |   end | ||||||
|   local logfilename = path_join(vim.fn.stdpath('data'), 'lsp.log') |   local logfilename = path_join(vim.fn.stdpath('cache'), 'lsp.log') | ||||||
|  |  | ||||||
|   --- Returns the log filename. |   --- Returns the log filename. | ||||||
|   --@returns (string) log filename |   --@returns (string) log filename | ||||||
| @@ -36,7 +36,7 @@ do | |||||||
|     return logfilename |     return logfilename | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   vim.fn.mkdir(vim.fn.stdpath('data'), "p") |   vim.fn.mkdir(vim.fn.stdpath('cache'), "p") | ||||||
|   local logfile = assert(io.open(logfilename, "a+")) |   local logfile = assert(io.open(logfilename, "a+")) | ||||||
|   for level, levelnr in pairs(log.levels) do |   for level, levelnr in pairs(log.levels) do | ||||||
|     -- Also export the log level on the root object. |     -- Also export the log level on the root object. | ||||||
|   | |||||||
| @@ -38,7 +38,7 @@ alternate file (e.g. stderr) use `LOG_CALLSTACK_TO_FILE(FILE*)`. Requires | |||||||
| Many log messages have a shared prefix, such as "UI" or "RPC". Use the shell to | Many log messages have a shared prefix, such as "UI" or "RPC". Use the shell to | ||||||
| filter the log, e.g. at DEBUG level you might want to exclude UI messages: | filter the log, e.g. at DEBUG level you might want to exclude UI messages: | ||||||
|  |  | ||||||
|     tail -F ~/.local/share/nvim/log | cat -v | stdbuf -o0 grep -v UI | stdbuf -o0 tee -a log |     tail -F ~/.cache/nvim/log | cat -v | stdbuf -o0 grep -v UI | stdbuf -o0 tee -a log | ||||||
|  |  | ||||||
| Build with ASAN | Build with ASAN | ||||||
| --------------- | --------------- | ||||||
|   | |||||||
| @@ -51,7 +51,7 @@ static bool log_try_create(char *fname) | |||||||
|  |  | ||||||
| /// Initializes path to log file. Sets $NVIM_LOG_FILE if empty. | /// Initializes path to log file. Sets $NVIM_LOG_FILE if empty. | ||||||
| /// | /// | ||||||
| /// Tries $NVIM_LOG_FILE, or falls back to $XDG_DATA_HOME/nvim/log. Path to log | /// Tries $NVIM_LOG_FILE, or falls back to $XDG_CACHE_HOME/nvim/log. Path to log | ||||||
| /// file is cached, so only the first call has effect, unless first call was not | /// file is cached, so only the first call has effect, unless first call was not | ||||||
| /// successful. Failed initialization indicates either a bug in expand_env() | /// successful. Failed initialization indicates either a bug in expand_env() | ||||||
| /// or both $NVIM_LOG_FILE and $HOME environment variables are undefined. | /// or both $NVIM_LOG_FILE and $HOME environment variables are undefined. | ||||||
| @@ -70,7 +70,7 @@ static bool log_path_init(void) | |||||||
|       || os_isdir((char_u *)log_file_path) |       || os_isdir((char_u *)log_file_path) | ||||||
|       || !log_try_create(log_file_path)) { |       || !log_try_create(log_file_path)) { | ||||||
|     // Invalid $NVIM_LOG_FILE or failed to expand; fall back to default. |     // Invalid $NVIM_LOG_FILE or failed to expand; fall back to default. | ||||||
|     char *defaultpath = stdpaths_user_data_subpath("log", 0, true); |     char *defaultpath = stdpaths_user_cache_subpath("log"); | ||||||
|     size_t len = xstrlcpy(log_file_path, defaultpath, size); |     size_t len = xstrlcpy(log_file_path, defaultpath, size); | ||||||
|     xfree(defaultpath); |     xfree(defaultpath); | ||||||
|     // Fall back to .nvimlog |     // Fall back to .nvimlog | ||||||
|   | |||||||
| @@ -108,6 +108,17 @@ char *get_xdg_home(const XDGVarType idx) | |||||||
|   return dir; |   return dir; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /// Return subpath of $XDG_CACHE_HOME | ||||||
|  | /// | ||||||
|  | /// @param[in]  fname  New component of the path. | ||||||
|  | /// | ||||||
|  | /// @return [allocated] `$XDG_CACHE_HOME/nvim/{fname}` | ||||||
|  | char *stdpaths_user_cache_subpath(const char *fname) | ||||||
|  |   FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET | ||||||
|  | { | ||||||
|  |   return concat_fnames_realloc(get_xdg_home(kXDGCacheHome), fname, true); | ||||||
|  | } | ||||||
|  |  | ||||||
| /// Return subpath of $XDG_CONFIG_HOME | /// Return subpath of $XDG_CONFIG_HOME | ||||||
| /// | /// | ||||||
| /// @param[in]  fname  New component of the path. | /// @param[in]  fname  New component of the path. | ||||||
|   | |||||||
| @@ -204,9 +204,8 @@ describe('startup defaults', function() | |||||||
|   end) |   end) | ||||||
|  |  | ||||||
|   describe('$NVIM_LOG_FILE', function() |   describe('$NVIM_LOG_FILE', function() | ||||||
|     local datasubdir = iswin() and 'nvim-data' or 'nvim' |  | ||||||
|     local xdgdir = 'Xtest-startup-xdg-logpath' |     local xdgdir = 'Xtest-startup-xdg-logpath' | ||||||
|     local xdgdatadir = xdgdir..'/'..datasubdir |     local xdgcachedir = xdgdir..'/nvim' | ||||||
|     after_each(function() |     after_each(function() | ||||||
|       os.remove('Xtest-logpath') |       os.remove('Xtest-logpath') | ||||||
|       rmdir(xdgdir) |       rmdir(xdgdir) | ||||||
| @@ -218,25 +217,25 @@ describe('startup defaults', function() | |||||||
|       }}) |       }}) | ||||||
|       eq('Xtest-logpath', eval('$NVIM_LOG_FILE')) |       eq('Xtest-logpath', eval('$NVIM_LOG_FILE')) | ||||||
|     end) |     end) | ||||||
|     it('defaults to stdpath("data")/log if empty', function() |     it('defaults to stdpath("cache")/log if empty', function() | ||||||
|       eq(true, mkdir(xdgdir) and mkdir(xdgdatadir)) |       eq(true, mkdir(xdgdir) and mkdir(xdgcachedir)) | ||||||
|       clear({env={ |       clear({env={ | ||||||
|         XDG_DATA_HOME=xdgdir, |         XDG_CACHE_HOME=xdgdir, | ||||||
|         NVIM_LOG_FILE='',  -- Empty is invalid. |         NVIM_LOG_FILE='',  -- Empty is invalid. | ||||||
|       }}) |       }}) | ||||||
|       eq(xdgdir..'/'..datasubdir..'/log', string.gsub(eval('$NVIM_LOG_FILE'), '\\', '/')) |       eq(xdgcachedir..'/log', string.gsub(eval('$NVIM_LOG_FILE'), '\\', '/')) | ||||||
|     end) |     end) | ||||||
|     it('defaults to stdpath("data")/log if invalid', function() |     it('defaults to stdpath("cache")/log if invalid', function() | ||||||
|       eq(true, mkdir(xdgdir) and mkdir(xdgdatadir)) |       eq(true, mkdir(xdgdir) and mkdir(xdgcachedir)) | ||||||
|       clear({env={ |       clear({env={ | ||||||
|         XDG_DATA_HOME=xdgdir, |         XDG_CACHE_HOME=xdgdir, | ||||||
|         NVIM_LOG_FILE='.',  -- Any directory is invalid. |         NVIM_LOG_FILE='.',  -- Any directory is invalid. | ||||||
|       }}) |       }}) | ||||||
|       eq(xdgdir..'/'..datasubdir..'/log', string.gsub(eval('$NVIM_LOG_FILE'), '\\', '/')) |       eq(xdgcachedir..'/log', string.gsub(eval('$NVIM_LOG_FILE'), '\\', '/')) | ||||||
|     end) |     end) | ||||||
|     it('defaults to .nvimlog if stdpath("data") is invalid', function() |     it('defaults to .nvimlog if stdpath("cache") is invalid', function() | ||||||
|       clear({env={ |       clear({env={ | ||||||
|         XDG_DATA_HOME='Xtest-missing-xdg-dir', |         XDG_CACHE_HOME='Xtest-missing-xdg-dir', | ||||||
|         NVIM_LOG_FILE='.',  -- Any directory is invalid. |         NVIM_LOG_FILE='.',  -- Any directory is invalid. | ||||||
|       }}) |       }}) | ||||||
|       eq('.nvimlog', eval('$NVIM_LOG_FILE')) |       eq('.nvimlog', eval('$NVIM_LOG_FILE')) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user