From dac15796ad903deb88afa9d8e054d5dba0e0d964 Mon Sep 17 00:00:00 2001 From: Nick Jensen Date: Mon, 22 Jun 2020 11:36:41 +1200 Subject: [PATCH] Improve log formatting and respect loglevel --- autoload/OmniSharp/log.vim | 43 ++++++++++++++++++++++-------------- autoload/OmniSharp/proc.vim | 12 ++++++++++ autoload/OmniSharp/stdio.vim | 10 ++++----- autoload/OmniSharp/util.vim | 8 +++++-- ftdetect/omnisharplog.vim | 2 +- syntax/omnisharplog.vim | 6 ++++- 6 files changed, 56 insertions(+), 25 deletions(-) diff --git a/autoload/OmniSharp/log.vim b/autoload/OmniSharp/log.vim index 34cdc1e65..7032f545d 100644 --- a/autoload/OmniSharp/log.vim +++ b/autoload/OmniSharp/log.vim @@ -3,28 +3,27 @@ set cpoptions&vim let s:stdiologfile = expand(':p:h:h:h') . '/log/stdio.log' -function! OmniSharp#log#Log(job, message, loglevel) abort +" Log from OmniSharp-vim +function! OmniSharp#log#Log(job, message, ...) abort + if g:OmniSharp_loglevel ==? 'none' | return | endif call s:Init(a:job) - let logit = 0 - if g:OmniSharp_loglevel ==? 'debug' - " Log everything - let logit = 1 - elseif g:OmniSharp_loglevel ==? 'info' - let logit = a:loglevel ==# 'info' - else - " g:OmniSharp_loglevel ==? 'none' - endif - if logit + let debug = a:0 && a:1 + if g:OmniSharp_loglevel !=? 'info' || !debug call writefile([a:message], a:job.logfile, 'a') endif endfunction " Log a decoded server message function! OmniSharp#log#LogServer(job, raw, msg) abort + if g:OmniSharp_loglevel ==? 'none' | return | endif call s:Init(a:job) - if !has_key(a:msg, 'Body') || type(a:msg.Body) != type({}) - call writefile(['RAW: ' . a:raw], a:job.logfile, 'a') - elseif get(a:msg, 'Event', '') ==? 'log' + if get(g:, 'OmniSharp_proc_debug') + call writefile([a:raw], a:job.logfile, 'a') + elseif !has_key(a:msg, 'Body') || type(a:msg.Body) != type({}) + return + elseif !has_key(a:msg, 'Event') + return + elseif get(a:msg, 'Event', '') ==# 'log' " Attempt to normalise newlines, which can be \%uD\%u0 in Windows and \%u0 " in linux let message = substitute(a:msg.Body.Message, '\%uD\ze\%u0', '', 'g') @@ -33,8 +32,20 @@ function! OmniSharp#log#LogServer(job, raw, msg) abort let prefix = s:LogLevelPrefix(a:msg.Body.LogLevel) call insert(lines, printf('[%s]: %s', prefix, a:msg.Body.Name)) call writefile(lines, a:job.logfile, 'a') - else - call writefile(['ELSE: ' . a:raw], a:job.logfile, 'a') + elseif get(a:msg, 'Event', '') ==# 'MsBuildProjectDiagnostics' + if len(a:msg.Body.Errors) == 0 && len(a:msg.Body.Warnings) == 0 + return + endif + let lines = [a:msg.Body.FileName] + for error in a:msg.Body.Errors + call add(lines, printf('%s(%d,%d): Error: %s', + \ error.FileName, error.StartLine, error.StartColumn, error.Text)) + endfor + for warn in a:msg.Body.Warnings + call add(lines, printf('%s(%d,%d): Warning: %s', + \ warn.FileName, warn.StartLine, warn.StartColumn, warn.Text)) + endfor + call writefile(lines, a:job.logfile, 'a') endif endfunction diff --git a/autoload/OmniSharp/proc.vim b/autoload/OmniSharp/proc.vim index cbc36ba0d..47ebbb502 100644 --- a/autoload/OmniSharp/proc.vim +++ b/autoload/OmniSharp/proc.vim @@ -78,6 +78,7 @@ function! OmniSharp#proc#neovimJobStart(command) abort \} let job.pid = jobpid(job.job_id) let s:channels[job.job_id] = job + call OmniSharp#log#Log(job, split(execute('version'), "\n")[0]) return job endfunction @@ -114,6 +115,9 @@ function! OmniSharp#proc#vimJobStart(command) abort call OmniSharp#util#EchoErr('Not using Vim 8.0+') return -1 endif + + + call s:debug('Using vim job_start to start the following command:') call s:debug(a:command) let opts = {'err_cb': 'OmniSharp#proc#vimErrHandler'} @@ -127,6 +131,7 @@ function! OmniSharp#proc#vimJobStart(command) abort let job.pid = job_info(job.job_id).process let channel_id = ch_info(job_getchannel(job.job_id)).id let s:channels[channel_id] = job + call OmniSharp#log#Log(job, join(split(execute('version'), "\n")[0:1], ', ')) return job endfunction @@ -202,6 +207,13 @@ function! OmniSharp#proc#Start(command, jobkey) abort if type(job) == type({}) let job.sln_or_dir = a:jobkey let job.loaded = 0 + call OmniSharp#log#Log(job, '') + call OmniSharp#log#Log(job, 'OmniSharp server started.') + call OmniSharp#log#Log(job, ' Path: ' . OmniSharp#util#GetServerPath()) + call OmniSharp#log#Log(job, ' Target: ' . a:jobkey) + call OmniSharp#log#Log(job, ' PID: ' . job.pid) + call OmniSharp#log#Log(job, ' Command: ' . join(a:command), 1) + call OmniSharp#log#Log(job, '') silent doautocmd User OmniSharpStarted endif return job diff --git a/autoload/OmniSharp/stdio.vim b/autoload/OmniSharp/stdio.vim index 518a9f1d3..da1399345 100644 --- a/autoload/OmniSharp/stdio.vim +++ b/autoload/OmniSharp/stdio.vim @@ -14,7 +14,7 @@ function! OmniSharp#stdio#HandleResponse(job, message) abort return endif if a:job.json_errors >= 3 && !a:job.loaded - call OmniSharp#log#Log(a:job, '3 errors caught while loading: stopping', 'info') + call OmniSharp#log#Log(a:job, '3 errors caught while loading: stopping') call OmniSharp#proc#StopJob(a:job.sln_or_dir) echohl WarningMsg echomsg 'You appear to be running an HTML server in stdio mode - ' . @@ -24,8 +24,8 @@ function! OmniSharp#stdio#HandleResponse(job, message) abort echohl None return endif - call OmniSharp#log#Log(a:job, a:message, 'info') - call OmniSharp#log#Log(a:job, 'JSON error: ' . v:exception, 'info') + call OmniSharp#log#Log(a:job, a:message) + call OmniSharp#log#Log(a:job, 'JSON error: ' . v:exception) return endtry call OmniSharp#log#LogServer(a:job, a:message, res) @@ -155,7 +155,7 @@ function! OmniSharp#stdio#RequestSend(job, body, command, opts, ...) abort endif return 0 endif - call OmniSharp#log#Log(a:job, ' Request: ' . a:command, 'debug') + call OmniSharp#log#Log(a:job, 'Request: ' . a:command, 1) let a:body['Command'] = a:command let a:body['Seq'] = s:nextseq @@ -176,7 +176,7 @@ function! OmniSharp#stdio#RequestSend(job, body, command, opts, ...) abort let s:nextseq += 1 if get(g:, 'OmniSharp_proc_debug') " The raw request is already logged by the server in debug mode. - call OmniSharp#log#Log(a:job, encodedBody, 'debug') + call OmniSharp#log#Log(a:job, encodedBody, 1) endif if has('nvim') call chansend(a:job.job_id, encodedBody . "\n") diff --git a/autoload/OmniSharp/util.vim b/autoload/OmniSharp/util.vim index e91688bd7..4674e153a 100644 --- a/autoload/OmniSharp/util.vim +++ b/autoload/OmniSharp/util.vim @@ -136,6 +136,10 @@ function! OmniSharp#util#EchoErr(msg) echohl ErrorMsg | echomsg a:msg | echohl None endfunction +function! OmniSharp#util#GetServerPath() abort + return get(s:, 'server_path', '') +endfunction + function! OmniSharp#util#GetStartCmd(solution_file) abort let solution_path = a:solution_file if fnamemodify(solution_path, ':t') ==? s:roslyn_server_files @@ -170,8 +174,8 @@ function! OmniSharp#util#GetStartCmd(solution_file) abort endif let command += ['-s', solution_path] - if g:OmniSharp_loglevel ==? 'debug' - let command += ['-v'] + if g:OmniSharp_loglevel !=? 'info' + let command += ['-l', g:OmniSharp_loglevel] endif if !has('win32') && !s:is_cygwin() && g:OmniSharp_server_use_mono diff --git a/ftdetect/omnisharplog.vim b/ftdetect/omnisharplog.vim index 6b602b7ec..7da569a65 100644 --- a/ftdetect/omnisharplog.vim +++ b/ftdetect/omnisharplog.vim @@ -1 +1 @@ -autocmd BufRead,BufNewFile *_omnisharp.log set filetype=omnisharplog +autocmd BufRead,BufNewFile *omnisharp.log set filetype=omnisharplog diff --git a/syntax/omnisharplog.vim b/syntax/omnisharplog.vim index 852723cb9..f077ae9d7 100644 --- a/syntax/omnisharplog.vim +++ b/syntax/omnisharplog.vim @@ -11,10 +11,12 @@ syn match oslTrace "^\[trce\]"ms=s+1,me=e-1 contained syn match oslDebug "^\[dbug\]"ms=s+1,me=e-1 contained syn match oslInformation "^\[info\]"ms=s+1,me=e-1 contained syn match oslWarning "^\[warn\]"ms=s+1,me=e-1 contained -syn match oslError "^\[trce\]"ms=s+1,me=e-1 contained +syn match oslError "^\[fail\]"ms=s+1,me=e-1 contained syn match oslError "^\[ERROR\]"ms=s+1,me=e-1 contained syn match oslCritical "^\[crit\]"ms=s+1,me=e-1 contained +syn match oslEndpoint "^Request: .*$"hs=s+9 + syn region oslRequestResponse start="\*\{12\}\s\+\%(Request\|Response\)\s\+\*\{12\}" end="^}" transparent fold hi def link oslName Comment @@ -26,6 +28,8 @@ hi def link oslWarning WarningMsg hi def link oslError ErrorMsg hi def link oslCritical ErrorMsg +hi def link oslEndpoint Identifier + let b:current_syntax = 'omnisharplog' let &cpoptions = s:save_cpo