log.vim 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. if exists('g:loaded_syntastic_log_autoload') || !exists('g:loaded_syntastic_plugin')
  2. finish
  3. endif
  4. let g:loaded_syntastic_log_autoload = 1
  5. let s:save_cpo = &cpo
  6. set cpo&vim
  7. let s:one_time_notices_issued = []
  8. " Public functions {{{1
  9. function! syntastic#log#info(msg) abort " {{{2
  10. echomsg 'syntastic: info: ' . a:msg
  11. endfunction " }}}2
  12. function! syntastic#log#warn(msg) abort " {{{2
  13. echohl WarningMsg
  14. echomsg 'syntastic: warning: ' . a:msg
  15. echohl None
  16. endfunction " }}}2
  17. function! syntastic#log#error(msg) abort " {{{2
  18. execute "normal \<Esc>"
  19. echohl ErrorMsg
  20. echomsg 'syntastic: error: ' . a:msg
  21. echohl None
  22. endfunction " }}}2
  23. function! syntastic#log#oneTimeWarn(msg) abort " {{{2
  24. if index(s:one_time_notices_issued, a:msg) >= 0
  25. return
  26. endif
  27. call add(s:one_time_notices_issued, a:msg)
  28. call syntastic#log#warn(a:msg)
  29. endfunction " }}}2
  30. " @vimlint(EVL102, 1, l:OLD_VAR)
  31. function! syntastic#log#deprecationWarn(old, new, ...) abort " {{{2
  32. if exists('g:syntastic_' . a:old) && !exists('g:syntastic_' . a:new)
  33. let msg = 'variable g:syntastic_' . a:old . ' is deprecated, please use '
  34. if a:0
  35. let OLD_VAR = g:syntastic_{a:old}
  36. try
  37. let NEW_VAR = eval(a:1)
  38. let msg .= 'in its stead: let g:syntastic_' . a:new . ' = ' . string(NEW_VAR)
  39. let g:syntastic_{a:new} = NEW_VAR
  40. catch
  41. let msg .= 'g:syntastic_' . a:new . ' instead'
  42. endtry
  43. else
  44. let msg .= 'g:syntastic_' . a:new . ' instead'
  45. let g:syntastic_{a:new} = g:syntastic_{a:old}
  46. endif
  47. call syntastic#log#oneTimeWarn(msg)
  48. endif
  49. endfunction " }}}2
  50. " @vimlint(EVL102, 0, l:OLD_VAR)
  51. function! syntastic#log#debug(level, msg, ...) abort " {{{2
  52. if !s:_isDebugEnabled(a:level)
  53. return
  54. endif
  55. let leader = s:_log_timestamp()
  56. call s:_logRedirect(1)
  57. if a:0 > 0
  58. " filter out dictionary functions
  59. echomsg leader . a:msg . ' ' .
  60. \ strtrans(string(type(a:1) == type({}) || type(a:1) == type([]) ?
  61. \ filter(copy(a:1), 'type(v:val) != type(function("tr"))') : a:1))
  62. else
  63. echomsg leader . a:msg
  64. endif
  65. call s:_logRedirect(0)
  66. endfunction " }}}2
  67. function! syntastic#log#debugShowOptions(level, names) abort " {{{2
  68. if !s:_isDebugEnabled(a:level)
  69. return
  70. endif
  71. let leader = s:_log_timestamp()
  72. call s:_logRedirect(1)
  73. let vlist = copy(type(a:names) == type('') ? [a:names] : a:names)
  74. if !empty(vlist)
  75. call map(vlist, "'&' . v:val . ' = ' . strtrans(string(eval('&' . v:val)))")
  76. echomsg leader . join(vlist, ', ')
  77. endif
  78. call s:_logRedirect(0)
  79. endfunction " }}}2
  80. function! syntastic#log#debugShowVariables(level, names) abort " {{{2
  81. if !s:_isDebugEnabled(a:level)
  82. return
  83. endif
  84. let leader = s:_log_timestamp()
  85. call s:_logRedirect(1)
  86. let vlist = type(a:names) == type('') ? [a:names] : a:names
  87. for name in vlist
  88. let msg = s:_format_variable(name)
  89. if msg !=# ''
  90. echomsg leader . msg
  91. endif
  92. endfor
  93. call s:_logRedirect(0)
  94. endfunction " }}}2
  95. function! syntastic#log#debugDump(level) abort " {{{2
  96. if !s:_isDebugEnabled(a:level)
  97. return
  98. endif
  99. call syntastic#log#debugShowVariables( a:level, sort(keys(g:_SYNTASTIC_DEFAULTS)) )
  100. endfunction " }}}2
  101. " }}}1
  102. " Private functions {{{1
  103. function! s:_isDebugEnabled_smart(level) abort " {{{2
  104. return and(g:syntastic_debug, a:level)
  105. endfunction " }}}2
  106. function! s:_isDebugEnabled_dumb(level) abort " {{{2
  107. " poor man's bit test for bit N, assuming a:level == 2**N
  108. return (g:syntastic_debug / a:level) % 2
  109. endfunction " }}}2
  110. let s:_isDebugEnabled = function(exists('*and') ? 's:_isDebugEnabled_smart' : 's:_isDebugEnabled_dumb')
  111. lockvar s:_isDebugEnabled
  112. function! s:_logRedirect(on) abort " {{{2
  113. if exists('g:syntastic_debug_file')
  114. if a:on
  115. try
  116. execute 'redir >> ' . fnameescape(expand(g:syntastic_debug_file, 1))
  117. catch /\m^Vim\%((\a\+)\)\=:/
  118. silent! redir END
  119. unlet g:syntastic_debug_file
  120. endtry
  121. else
  122. silent! redir END
  123. endif
  124. endif
  125. endfunction " }}}2
  126. " }}}1
  127. " Utilities {{{1
  128. function! s:_log_timestamp() abort " {{{2
  129. return 'syntastic: ' . split(reltimestr(reltime(g:_SYNTASTIC_START)))[0] . ': '
  130. endfunction " }}}2
  131. function! s:_format_variable(name) abort " {{{2
  132. let vals = []
  133. if exists('g:syntastic_' . a:name)
  134. call add(vals, 'g:syntastic_' . a:name . ' = ' . strtrans(string(g:syntastic_{a:name})))
  135. endif
  136. if exists('b:syntastic_' . a:name)
  137. call add(vals, 'b:syntastic_' . a:name . ' = ' . strtrans(string(b:syntastic_{a:name})))
  138. endif
  139. return join(vals, ', ')
  140. endfunction " }}}2
  141. " }}}1
  142. let &cpo = s:save_cpo
  143. unlet s:save_cpo
  144. " vim: set sw=4 sts=4 et fdm=marker: