Skip to content

Commit 4deefa6

Browse files
authored
Notify user for messages logged at WARN or above by default. (#186)
Also adds a maktaba#log#SetNotificationLevel hook to override that level.
1 parent 02935bd commit 4deefa6

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

autoload/maktaba/log.vim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ endif
2121

2222
let s:LEVELS = maktaba#log#LEVELS
2323

24+
if !exists('s:notification_level')
25+
let s:notification_level = s:LEVELS.WARN
26+
endif
27+
2428

2529
""
2630
" @usage {level} {context} {message} [args...]
@@ -37,6 +41,9 @@ function! s:DoMessage(level, context, message, ...) abort
3741
endif
3842

3943
call s:SendToHandlers([a:level, localtime(), a:context, l:message])
44+
if s:notification_level isnot -1 && a:level >= s:notification_level
45+
call s:NotifyMessage(printf('[%s] %s', a:context, l:message), a:level)
46+
endif
4047
endfunction
4148

4249

@@ -85,6 +92,34 @@ function! s:SendToHandlers(logitem) abort
8592
endfunction
8693

8794

95+
function! s:NotifyMessage(message, level) abort
96+
if a:level >= s:LEVELS.ERROR
97+
call maktaba#error#Shout(a:message)
98+
elseif a:level >= s:LEVELS.WARN
99+
call maktaba#error#Warn(a:message)
100+
else
101+
echomsg a:message
102+
endif
103+
endfunction
104+
105+
106+
""
107+
" Sets the minimum {level} of log messages that will trigger a user
108+
" notification, or -1 to disable notifications. By default, the user will be
109+
" notified after every message logged at WARN or higher.
110+
"
111+
" Notifications will be sent using @function(maktaba#error#Shout) for ERROR
112+
" and SEVERE messages, @function(maktaba#error#Warn) for WARN, and |:echomsg|
113+
" for INFO and DEBUG.
114+
" @throws BadValue
115+
function! maktaba#log#SetNotificationLevel(level) abort
116+
call maktaba#ensure#IsTrue(
117+
\ a:level is -1 || maktaba#value#IsIn(a:level, s:LEVELS.Values()),
118+
\ 'Expected {level} to be maktaba#log#LEVELS value or -1')
119+
let s:notification_level = a:level
120+
endfunction
121+
122+
88123
""
89124
" Gets a string representing a single log {entry}.
90125
function! s:FormatLogEntry(entry) abort

doc/maktaba.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,16 @@ maktaba#list#RemoveAll({list}, {item}) *maktaba#list#RemoveAll()*
12261226
Removes all instances of {item} from {list}. {list} is modified in place and
12271227
returned for convenience.
12281228

1229+
maktaba#log#SetNotificationLevel({level}) *maktaba#log#SetNotificationLevel()*
1230+
Sets the minimum {level} of log messages that will trigger a user
1231+
notification, or -1 to disable notifications. By default, the user will be
1232+
notified after every message logged at WARN or higher.
1233+
1234+
Notifications will be sent using |maktaba#error#Shout()| for ERROR and
1235+
SEVERE messages, |maktaba#error#Warn()| for WARN, and |:echomsg| for INFO
1236+
and DEBUG.
1237+
Throws ERROR(BadValue)
1238+
12291239
maktaba#log#Logger({context}) *maktaba#log#Logger()*
12301240
Creates a |maktaba.Logger| interface for {context}.
12311241

vroom/log.vroom

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,26 @@ the usual Debug, Info, Warn, Error, and Severe functions:
2727
:call g:vroom_logger.Debug('why')
2828
:call g:vroom_logger.Info('hello')
2929
:call g:vroom_logger.Warn('there')
30+
~ [VROOMFILE] there
3031
:call g:vroom_logger.Error('my')
32+
~ [VROOMFILE] my
3133
:call g:vroom_logger.Severe('friend')
34+
~ [VROOMFILE] friend
35+
36+
By default, messages logged at WARN or above are also shown immediately to the
37+
user. Call maktaba#log#SetNotificationLevel to configure a different level.
38+
39+
:let g:LEVELS = maktaba#log#LEVELS
40+
:call maktaba#log#SetNotificationLevel(g:LEVELS.SEVERE)
41+
:call g:vroom_logger.Error('Threepio! Where could he be?!')
42+
:call g:vroom_logger.Severe('Shut down all the garbage mashers!!')
43+
~ [VROOMFILE] Shut down all the garbage mashers!!
44+
45+
Setting the display level to -1 disables printing these log messages completely.
46+
47+
:call maktaba#log#SetNotificationLevel(-1)
48+
:call g:vroom_logger.Severe('Shut down all the garbage mashers!!')
49+
3250

3351
Each plugin gets a logger attached automatically for its context:
3452

@@ -62,7 +80,9 @@ logged earlier...
6280
~ Log message: WARN [-0-9T:]+ \[VROOMFILE\] there (regex)
6381
~ Log message: ERROR [-0-9T:]+ \[VROOMFILE\] my (regex)
6482
~ Log message: SEVERE [-0-9T:]+ \[VROOMFILE\] friend (regex)
65-
83+
~ .* (regex)
84+
~ .* (regex)
85+
~ .* (regex)
6686
~ Log message: DEBUG [-0-9T:]+ \[emptyplugin\] Hello from emptyplugin (regex)
6787

6888
New messages are logged as they arrive.

0 commit comments

Comments
 (0)