Skip to content

Commit

Permalink
Improve log formatting and respect loglevel
Browse files Browse the repository at this point in the history
  • Loading branch information
nickspoons committed Jun 21, 2020
1 parent 4ec6ce7 commit dac1579
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
43 changes: 27 additions & 16 deletions autoload/OmniSharp/log.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@ set cpoptions&vim

let s:stdiologfile = expand('<sfile>: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')
Expand All @@ -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

Expand Down
12 changes: 12 additions & 0 deletions autoload/OmniSharp/proc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'}
Expand All @@ -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

Expand Down Expand Up @@ -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 <nomodeline> User OmniSharpStarted
endif
return job
Expand Down
10 changes: 5 additions & 5 deletions autoload/OmniSharp/stdio.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 - ' .
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
8 changes: 6 additions & 2 deletions autoload/OmniSharp/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ftdetect/omnisharplog.vim
Original file line number Diff line number Diff line change
@@ -1 +1 @@
autocmd BufRead,BufNewFile *_omnisharp.log set filetype=omnisharplog
autocmd BufRead,BufNewFile *omnisharp.log set filetype=omnisharplog
6 changes: 5 additions & 1 deletion syntax/omnisharplog.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit dac1579

Please sign in to comment.