@@ -8,7 +8,7 @@ local M = {
88--- @param fmt string for string.format
99--- @vararg any arguments for string.format
1010function M .raw (typ , fmt , ...)
11- if not M .path or not M . config . types [ typ ] and not M . config . types . all then
11+ if not M .enabled ( typ ) then
1212 return
1313 end
1414
@@ -21,32 +21,48 @@ function M.raw(typ, fmt, ...)
2121 end
2222end
2323
24- --- Write to log file via M.line
24+ --- Write profile start to log file
2525--- START is prefixed
26+ --- @param fmt string for string.format
27+ --- @vararg any arguments for string.format
2628--- @return number nanos to pass to profile_end
2729function M .profile_start (fmt , ...)
28- if not M .path or not M .config .types .profile and not M .config .types .all then
30+ if M .enabled " profile" then
31+ M .line (" profile" , " START " .. (fmt or " ???" ), ... )
32+ return vim .loop .hrtime ()
33+ else
2934 return 0
3035 end
31- M .line (" profile" , " START " .. (fmt or " ???" ), ... )
32- return vim .loop .hrtime ()
3336end
3437
35- --- Write to log file via M.line
38+ --- Write profile end to log file
3639--- END is prefixed and duration in seconds is suffixed
3740--- @param start number nanos returned from profile_start
41+ --- @param fmt string for string.format
42+ --- @vararg any arguments for string.format
3843function M .profile_end (start , fmt , ...)
39- if not M .path or not M .config .types .profile and not M .config .types .all then
40- return
44+ if M .enabled " profile" then
45+ local millis = start and math.modf ((vim .loop .hrtime () - start ) / 1000000 ) or - 1
46+ M .line (" profile" , " END " .. (fmt or " ???" ) .. " " .. millis .. " ms" , ... )
4147 end
42- local millis = start and math.modf ((vim .loop .hrtime () - start ) / 1000000 ) or - 1
43- M .line (" profile" , " END " .. (fmt or " ???" ) .. " " .. millis .. " ms" , ... )
4448end
4549
46- -- Write to log file via M.raw
47- -- time and typ are prefixed and a trailing newline is added
50+ --- Write to log file
51+ --- time and typ are prefixed and a trailing newline is added
52+ --- @param typ string as per log.types config
53+ --- @param fmt string for string.format
54+ --- @vararg any arguments for string.format
4855function M .line (typ , fmt , ...)
49- M .raw (typ , string.format (" [%s] [%s] %s\n " , os.date " %Y-%m-%d %H:%M:%S" , typ , fmt ), ... )
56+ if M .enabled (typ ) then
57+ M .raw (typ , string.format (" [%s] [%s] %s\n " , os.date " %Y-%m-%d %H:%M:%S" , typ , fmt ), ... )
58+ end
59+ end
60+
61+ --- Logging is enabled for typ or all
62+ --- @param typ string as per log.types config
63+ --- @return boolean
64+ function M .enabled (typ )
65+ return M .path ~= nil and (M .config .types [typ ] or M .config .types .all )
5066end
5167
5268function M .setup (opts )
0 commit comments