1 // This is an open source non-commercial project. Dear PVS-Studio, please check
2 // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3
4 //
5 // Log module
6 //
7 // How Linux printk() handles recursion, buffering, etc:
8 // https://lwn.net/Articles/780556/
9 //
10
11 #include <assert.h>
12 #include <inttypes.h>
13 #include <stdarg.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <uv.h>
17
18 #include "auto/config.h"
19 #include "nvim/log.h"
20 #include "nvim/os/os.h"
21 #include "nvim/os/time.h"
22 #include "nvim/types.h"
23
24 #define LOG_FILE_ENV "NVIM_LOG_FILE"
25
26 /// Cached location of the expanded log file path decided by log_path_init().
27 static char log_file_path[MAXPATHL + 1] = { 0 };
28
29 static uv_mutex_t mutex;
30
31 #ifdef INCLUDE_GENERATED_DECLARATIONS
32 # include "log.c.generated.h"
33 #endif
34
35 #ifdef HAVE_EXECINFO_BACKTRACE
36 # include <execinfo.h>
37 #endif
38
log_try_create(char * fname)39 static bool log_try_create(char *fname)
40 {
41 if (fname == NULL || fname[0] == '\0') {
42 return false;
43 }
44 FILE *log_file = fopen(fname, "a");
45 if (log_file == NULL) {
46 return false;
47 }
48 fclose(log_file);
49 return true;
50 }
51
52 /// Initializes path to log file. Sets $NVIM_LOG_FILE if empty.
53 ///
54 /// Tries $NVIM_LOG_FILE, or falls back to $XDG_CACHE_HOME/nvim/log. Path to log
55 /// file is cached, so only the first call has effect, unless first call was not
56 /// successful. Failed initialization indicates either a bug in expand_env()
57 /// or both $NVIM_LOG_FILE and $HOME environment variables are undefined.
58 ///
59 /// @return true if path was initialized, false otherwise.
log_path_init(void)60 static bool log_path_init(void)
61 {
62 if (log_file_path[0]) {
63 return true;
64 }
65 size_t size = sizeof(log_file_path);
66 expand_env((char_u *)"$" LOG_FILE_ENV, (char_u *)log_file_path,
67 (int)size - 1);
68 if (strequal("$" LOG_FILE_ENV, log_file_path)
69 || log_file_path[0] == '\0'
70 || os_isdir((char_u *)log_file_path)
71 || !log_try_create(log_file_path)) {
72 // Make kXDGCacheHome if it does not exist.
73 char *cachehome = get_xdg_home(kXDGCacheHome);
74 char *failed_dir = NULL;
75 bool log_dir_failure = false;
76 if (!os_isdir((char_u *)cachehome)) {
77 log_dir_failure = (os_mkdir_recurse(cachehome, 0700, &failed_dir) != 0);
78 }
79 XFREE_CLEAR(cachehome);
80 // Invalid $NVIM_LOG_FILE or failed to expand; fall back to default.
81 char *defaultpath = stdpaths_user_cache_subpath("log");
82 size_t len = xstrlcpy(log_file_path, defaultpath, size);
83 xfree(defaultpath);
84 // Fall back to .nvimlog
85 if (len >= size || !log_try_create(log_file_path)) {
86 len = xstrlcpy(log_file_path, ".nvimlog", size);
87 }
88 // Fall back to stderr
89 if (len >= size || !log_try_create(log_file_path)) {
90 log_file_path[0] = '\0';
91 return false;
92 }
93 os_setenv(LOG_FILE_ENV, log_file_path, true);
94 if (log_dir_failure) {
95 WLOG("Failed to create directory %s for writing logs: %s",
96 failed_dir, os_strerror(log_dir_failure));
97 }
98 XFREE_CLEAR(failed_dir);
99 }
100 return true;
101 }
102
log_init(void)103 void log_init(void)
104 {
105 uv_mutex_init(&mutex);
106 log_path_init();
107 }
108
log_lock(void)109 void log_lock(void)
110 {
111 uv_mutex_lock(&mutex);
112 }
113
log_unlock(void)114 void log_unlock(void)
115 {
116 uv_mutex_unlock(&mutex);
117 }
118
119 /// Logs a message to $NVIM_LOG_FILE.
120 ///
121 /// @param log_level Log level (see log.h)
122 /// @param context Description of a shared context or subsystem
123 /// @param func_name Function name, or NULL
124 /// @param line_num Source line number, or -1
125 /// @param eol Append linefeed "\n"
126 /// @param fmt printf-style format string
logmsg(int log_level,const char * context,const char * func_name,int line_num,bool eol,const char * fmt,...)127 bool logmsg(int log_level, const char *context, const char *func_name, int line_num, bool eol,
128 const char *fmt, ...)
129 FUNC_ATTR_UNUSED FUNC_ATTR_PRINTF(6, 7)
130 {
131 if (log_level < MIN_LOG_LEVEL) {
132 return false;
133 }
134
135 #ifdef EXITFREE
136 // Logging after we've already started freeing all our memory will only cause
137 // pain. We need access to VV_PROGPATH, homedir, etc.
138 assert(!entered_free_all_mem);
139 #endif
140
141 log_lock();
142 bool ret = false;
143 FILE *log_file = open_log_file();
144
145 if (log_file == NULL) {
146 goto end;
147 }
148
149 va_list args;
150 va_start(args, fmt);
151 ret = v_do_log_to_file(log_file, log_level, context, func_name, line_num,
152 eol, fmt, args);
153 va_end(args);
154
155 if (log_file != stderr && log_file != stdout) {
156 fclose(log_file);
157 }
158 end:
159 log_unlock();
160 return ret;
161 }
162
log_uv_handles(void * loop)163 void log_uv_handles(void *loop)
164 {
165 uv_loop_t *l = loop;
166 log_lock();
167 FILE *log_file = open_log_file();
168
169 if (log_file == NULL) {
170 goto end;
171 }
172
173 uv_print_all_handles(l, log_file);
174
175 if (log_file != stderr && log_file != stdout) {
176 fclose(log_file);
177 }
178 end:
179 log_unlock();
180 }
181
182 /// Open the log file for appending.
183 ///
184 /// @return FILE* decided by log_path_init() or stderr in case of error
open_log_file(void)185 FILE *open_log_file(void)
186 {
187 static bool opening_log_file = false;
188 // Disallow recursion. (This only matters for log_path_init; for logmsg and
189 // friends we use a mutex: log_lock).
190 if (opening_log_file) {
191 do_log_to_file(stderr, ERROR_LOG_LEVEL, NULL, __func__, __LINE__, true,
192 "Cannot LOG() recursively.");
193 return stderr;
194 }
195
196 FILE *log_file = NULL;
197 opening_log_file = true;
198 if (log_path_init()) {
199 log_file = fopen(log_file_path, "a");
200 }
201 opening_log_file = false;
202
203 if (log_file != NULL) {
204 return log_file;
205 }
206
207 // May happen if:
208 // - LOG() is called before early_init()
209 // - Directory does not exist
210 // - File is not writable
211 do_log_to_file(stderr, ERROR_LOG_LEVEL, NULL, __func__, __LINE__, true,
212 "Logging to stderr, failed to open $" LOG_FILE_ENV ": %s",
213 log_file_path);
214 return stderr;
215 }
216
217 #ifdef HAVE_EXECINFO_BACKTRACE
log_callstack_to_file(FILE * log_file,const char * const func_name,const int line_num)218 void log_callstack_to_file(FILE *log_file, const char *const func_name, const int line_num)
219 {
220 void *trace[100];
221 int trace_size = backtrace(trace, ARRAY_SIZE(trace));
222
223 char exepath[MAXPATHL] = { 0 };
224 size_t exepathlen = MAXPATHL;
225 if (os_exepath(exepath, &exepathlen) != 0) {
226 abort();
227 }
228 assert(24 + exepathlen < IOSIZE); // Must fit in `cmdbuf` below.
229
230 char cmdbuf[IOSIZE + (20 * ARRAY_SIZE(trace)) + MAXPATHL];
231 snprintf(cmdbuf, sizeof(cmdbuf), "addr2line -e %s -f -p", exepath);
232 for (int i = 1; i < trace_size; i++) {
233 char buf[20]; // 64-bit pointer 0xNNNNNNNNNNNNNNNN with leading space.
234 snprintf(buf, sizeof(buf), " %p", trace[i]);
235 xstrlcat(cmdbuf, buf, sizeof(cmdbuf));
236 }
237 // Now we have a command string like:
238 // addr2line -e /path/to/exe -f -p 0x123 0x456 ...
239
240 do_log_to_file(log_file, DEBUG_LOG_LEVEL, NULL, func_name, line_num, true,
241 "trace:");
242 FILE *fp = popen(cmdbuf, "r");
243 char linebuf[IOSIZE];
244 while (fgets(linebuf, sizeof(linebuf) - 1, fp) != NULL) {
245 fprintf(log_file, " %s", linebuf);
246 }
247 pclose(fp);
248
249 if (log_file != stderr && log_file != stdout) {
250 fclose(log_file);
251 }
252 }
253
log_callstack(const char * const func_name,const int line_num)254 void log_callstack(const char *const func_name, const int line_num)
255 {
256 log_lock();
257 FILE *log_file = open_log_file();
258 if (log_file == NULL) {
259 goto end;
260 }
261
262 log_callstack_to_file(log_file, func_name, line_num);
263
264 end:
265 log_unlock();
266 }
267 #endif
268
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,...)269 static bool do_log_to_file(FILE *log_file, int log_level, const char *context,
270 const char *func_name, int line_num, bool eol, const char *fmt, ...)
271 FUNC_ATTR_PRINTF(7, 8)
272 {
273 va_list args;
274 va_start(args, fmt);
275 bool ret = v_do_log_to_file(log_file, log_level, context, func_name,
276 line_num, eol, fmt, args);
277 va_end(args);
278
279 return ret;
280 }
281
v_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)282 static bool v_do_log_to_file(FILE *log_file, int log_level, const char *context,
283 const char *func_name, int line_num, bool eol, const char *fmt,
284 va_list args)
285 {
286 static const char *log_levels[] = {
287 [DEBUG_LOG_LEVEL] = "DEBUG",
288 [INFO_LOG_LEVEL] = "INFO ",
289 [WARN_LOG_LEVEL] = "WARN ",
290 [ERROR_LOG_LEVEL] = "ERROR",
291 };
292 assert(log_level >= DEBUG_LOG_LEVEL && log_level <= ERROR_LOG_LEVEL);
293
294 // Format the timestamp.
295 struct tm local_time;
296 if (os_localtime(&local_time) == NULL) {
297 return false;
298 }
299 char date_time[20];
300 if (strftime(date_time, sizeof(date_time), "%Y-%m-%dT%H:%M:%S",
301 &local_time) == 0) {
302 return false;
303 }
304
305 int millis = 0;
306 uv_timeval64_t curtime;
307 if (uv_gettimeofday(&curtime) == 0) {
308 millis = (int)curtime.tv_usec / 1000;
309 }
310
311 // Print the log message.
312 int64_t pid = os_get_pid();
313 int rv = (line_num == -1 || func_name == NULL)
314 ? fprintf(log_file, "%s %s.%03d %-5" PRId64 " %s",
315 log_levels[log_level], date_time, millis, pid,
316 (context == NULL ? "?:" : context))
317 : fprintf(log_file, "%s %s.%03d %-5" PRId64 " %s%s:%d: ",
318 log_levels[log_level], date_time, millis, pid,
319 (context == NULL ? "" : context),
320 func_name, line_num);
321 if (rv < 0) {
322 return false;
323 }
324 if (vfprintf(log_file, fmt, args) < 0) {
325 return false;
326 }
327 if (eol) {
328 fputc('\n', log_file);
329 }
330 if (fflush(log_file) == EOF) {
331 return false;
332 }
333
334 return true;
335 }
336