@@ -54,25 +54,19 @@ static bool log_try_create(char *fname)
54
54
55
55
/// Initializes path to log file. Sets $NVIM_LOG_FILE if empty.
56
56
///
57
- /// Tries $NVIM_LOG_FILE, or falls back to $XDG_STATE_HOME/nvim/log. Path to log
58
- /// file is cached, so only the first call has effect, unless first call was not
59
- /// successful. Failed initialization indicates either a bug in expand_env()
60
- /// or both $NVIM_LOG_FILE and $HOME environment variables are undefined.
61
- ///
62
- /// @return true if path was initialized, false otherwise.
63
- static bool log_path_init (void )
57
+ /// Tries $NVIM_LOG_FILE, or falls back to $XDG_STATE_HOME/nvim/log. Failed
58
+ /// initialization indicates either a bug in expand_env() or both $NVIM_LOG_FILE
59
+ /// and $HOME environment variables are undefined.
60
+ static void log_path_init (void )
64
61
{
65
- if (log_file_path [0 ]) {
66
- return true;
67
- }
68
62
size_t size = sizeof (log_file_path );
69
63
expand_env ((char_u * )"$" LOG_FILE_ENV , (char_u * )log_file_path ,
70
64
(int )size - 1 );
71
65
if (strequal ("$" LOG_FILE_ENV , log_file_path )
72
66
|| log_file_path [0 ] == '\0'
73
67
|| os_isdir ((char_u * )log_file_path )
74
68
|| !log_try_create (log_file_path )) {
75
- // Make kXDGStateHome if it does not exist.
69
+ // Make $XDG_STATE_HOME if it does not exist.
76
70
char * loghome = get_xdg_home (kXDGStateHome );
77
71
char * failed_dir = NULL ;
78
72
bool log_dir_failure = false;
@@ -91,7 +85,7 @@ static bool log_path_init(void)
91
85
// Fall back to stderr
92
86
if (len >= size || !log_try_create (log_file_path )) {
93
87
log_file_path [0 ] = '\0' ;
94
- return false ;
88
+ return ;
95
89
}
96
90
os_setenv (LOG_FILE_ENV , log_file_path , true);
97
91
if (log_dir_failure ) {
@@ -100,7 +94,6 @@ static bool log_path_init(void)
100
94
}
101
95
XFREE_CLEAR (failed_dir );
102
96
}
103
- return true;
104
97
}
105
98
106
99
void log_init (void )
@@ -166,21 +159,17 @@ bool logmsg(int log_level, const char *context, const char *func_name, int line_
166
159
if (!did_msg ) {
167
160
did_msg = true;
168
161
char * arg1 = func_name ? xstrdup (func_name ) : (context ? xstrdup (context ) : NULL );
162
+ // coverity[leaked_storage]
169
163
loop_schedule_deferred (& main_loop , event_create (on_log_recursive_event , 2 , arg1 , line_num ));
170
164
}
171
165
g_stats .log_skip ++ ;
172
166
log_unlock ();
173
167
return false;
174
168
}
175
-
176
169
recursive = true;
177
170
bool ret = false;
178
171
FILE * log_file = open_log_file ();
179
172
180
- if (log_file == NULL ) {
181
- goto end ;
182
- }
183
-
184
173
va_list args ;
185
174
va_start (args , fmt );
186
175
ret = v_do_log_to_file (log_file , log_level , context , func_name , line_num ,
@@ -190,7 +179,7 @@ bool logmsg(int log_level, const char *context, const char *func_name, int line_
190
179
if (log_file != stderr && log_file != stdout ) {
191
180
fclose (log_file );
192
181
}
193
- end :
182
+
194
183
recursive = false;
195
184
log_unlock ();
196
185
return ret ;
@@ -202,46 +191,36 @@ void log_uv_handles(void *loop)
202
191
log_lock ();
203
192
FILE * log_file = open_log_file ();
204
193
205
- if (log_file == NULL ) {
206
- goto end ;
207
- }
208
-
209
194
uv_print_all_handles (l , log_file );
210
195
211
196
if (log_file != stderr && log_file != stdout ) {
212
197
fclose (log_file );
213
198
}
214
- end :
199
+
215
200
log_unlock ();
216
201
}
217
202
218
203
/// Open the log file for appending.
219
204
///
220
- /// @return FILE* decided by log_path_init() or stderr in case of error
205
+ /// @return Log file, or stderr on failure
221
206
FILE * open_log_file (void )
222
207
{
223
- static bool recursive = false;
224
- if (recursive ) {
225
- abort ();
226
- }
227
-
228
- FILE * log_file = NULL ;
229
- recursive = true;
230
- if (log_path_init ()) {
231
- log_file = fopen (log_file_path , "a" );
232
- }
233
- recursive = false;
234
-
235
- if (log_file != NULL ) {
236
- return log_file ;
208
+ errno = 0 ;
209
+ if (log_file_path [0 ]) {
210
+ FILE * f = fopen (log_file_path , "a" );
211
+ if (f != NULL ) {
212
+ return f ;
213
+ }
237
214
}
238
215
239
216
// May happen if:
240
- // - LOG() is called before early_init()
217
+ // - fopen() failed
218
+ // - LOG() is called before log_init()
241
219
// - Directory does not exist
242
220
// - File is not writable
243
221
do_log_to_file (stderr , LOGLVL_ERR , NULL , __func__ , __LINE__ , true,
244
- "failed to open $" LOG_FILE_ENV ": %s" , log_file_path );
222
+ "failed to open $" LOG_FILE_ENV " (%s): %s" ,
223
+ strerror (errno ), log_file_path );
245
224
return stderr ;
246
225
}
247
226
@@ -285,13 +264,7 @@ void log_callstack(const char *const func_name, const int line_num)
285
264
{
286
265
log_lock ();
287
266
FILE * log_file = open_log_file ();
288
- if (log_file == NULL ) {
289
- goto end ;
290
- }
291
-
292
267
log_callstack_to_file (log_file , func_name , line_num );
293
-
294
- end :
295
268
log_unlock ();
296
269
}
297
270
#endif
0 commit comments