I am writing a basic log function so that I can print debug output to a file (and perhaps more-so, to practice writing a function in vim). Here is where I am at right now:
let g:log_level_map = {'DEBUG': 10, 'INFO': 20, 'WARNING': 30, 'ERROR': 40}
let g:log_level = "DEBUG"
func LogOutput(msg, level='DEBUG')
redir >> log.vim
" Make sure its above the log_level
if g:log_level_map[a:level] < g:log_level_map[g:log_level]
return
endif
" _LogFormat: [TIME] LEVEL - FILE - FUNC - LINE : MSG
let time = strftime('%c')
let level = a:level
let file = expand('%:p:t')
let func = '' " how to get caller function?
let line = '' " how to get caller line no?
let msg = a:msg
" Print the output
echom(printf("[%s] %s - %s - %s- L%s : %s",
\ time, level, file, func, line, msg))
redir END
endfunc
It works great, but I'm having difficult figuring out how to get the line and function (if it exists) of the line of code that called the LogOutput
function. Is it possible to access something like a call stack here, or how can this be done?