| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| local log = {} |
|
|
| local log_levels = vim.log.levels |
|
|
| |
| |
| |
| |
| |
| |
| |
| log.levels = vim.deepcopy(log_levels) |
|
|
| |
| local current_log_level = log_levels.WARN |
|
|
| local log_date_format = '%F %H:%M:%S' |
|
|
| local function format_func(arg) |
| return vim.inspect(arg, { newline = ' ', indent = '' }) |
| end |
|
|
| local function notify(msg, level) |
| if vim.in_fast_event() then |
| vim.schedule(function() |
| vim.notify(msg, level) |
| end) |
| else |
| vim.notify(msg, level) |
| end |
| end |
|
|
| local logfilename = vim.fs.joinpath(vim.fn.stdpath('log') , 'lsp.log') |
|
|
| |
| |
| |
| vim.fn.mkdir(vim.fn.stdpath('log') , 'p') |
|
|
| |
| |
| function log.get_filename() |
| return logfilename |
| end |
|
|
| |
| function log._set_filename(s) |
| logfilename = s |
| end |
|
|
| |
| local logfile, openerr |
|
|
| |
| local function open_logfile() |
| |
| if logfile then |
| return true |
| end |
| if openerr then |
| return false |
| end |
|
|
| logfile, openerr = io.open(logfilename, 'a+') |
| if not logfile then |
| local err_msg = string.format('Failed to open LSP client log file: %s', openerr) |
| notify(err_msg, log_levels.ERROR) |
| return false |
| end |
|
|
| local log_info = vim.uv.fs_stat(logfilename) |
| if log_info and log_info.size > 1e9 then |
| local warn_msg = string.format( |
| 'LSP client log is large (%d MB): %s', |
| log_info.size / (1000 * 1000), |
| logfilename |
| ) |
| notify(warn_msg) |
| end |
|
|
| |
| logfile:write(string.format('[START][%s] LSP logging initiated\n', os.date(log_date_format))) |
| return true |
| end |
|
|
| for level, levelnr in pairs(log_levels) do |
| |
| |
| log[level] = levelnr |
|
|
| |
| log.levels[levelnr] = level |
| end |
|
|
| |
| |
| |
| local function create_logger(level, levelnr) |
| return function(...) |
| if not log.should_log(levelnr) then |
| return false |
| end |
| local argc = select('#', ...) |
| if argc == 0 then |
| return true |
| end |
| if not open_logfile() then |
| return false |
| end |
| local info = debug.getinfo(2, 'Sl') |
| local header = string.format( |
| '[%s][%s] ...%s:%s', |
| level, |
| os.date(log_date_format), |
| info.short_src:sub(-16), |
| info.currentline |
| ) |
| local parts = { header } |
| for i = 1, argc do |
| local arg = select(i, ...) |
| table.insert(parts, arg == nil and 'nil' or format_func(arg)) |
| end |
| assert(logfile) |
| logfile:write(table.concat(parts, '\t'), '\n') |
| logfile:flush() |
| end |
| end |
|
|
| |
| |
| |
|
|
| |
| log.debug = create_logger('DEBUG', log_levels.DEBUG) |
|
|
| |
| log.error = create_logger('ERROR', log_levels.ERROR) |
|
|
| |
| log.info = create_logger('INFO', log_levels.INFO) |
|
|
| |
| log.trace = create_logger('TRACE', log_levels.TRACE) |
|
|
| |
| log.warn = create_logger('WARN', log_levels.WARN) |
|
|
| |
| |
| function log.set_level(level) |
| if type(level) == 'string' then |
| current_log_level = |
| assert(log.levels[level:upper()], string.format('Invalid log level: %q', level)) |
| else |
| assert(type(level) == 'number', 'level must be a number or string') |
| assert(log.levels[level], string.format('Invalid log level: %d', level)) |
| current_log_level = level |
| end |
| end |
|
|
| |
| |
| function log.get_level() |
| return current_log_level |
| end |
|
|
| |
| |
| function log.set_format_func(handle) |
| assert(handle == vim.inspect or type(handle) == 'function', 'handle must be a function') |
| format_func = handle |
| end |
|
|
| |
| |
| |
| function log.should_log(level) |
| return level >= current_log_level |
| end |
|
|
| return log |
|
|