@@ -8,7 +8,7 @@ local M = {
8
8
--- @param fmt string for string.format
9
9
--- @vararg any arguments for string.format
10
10
function 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
12
12
return
13
13
end
14
14
@@ -21,32 +21,48 @@ function M.raw(typ, fmt, ...)
21
21
end
22
22
end
23
23
24
- --- Write to log file via M.line
24
+ --- Write profile start to log file
25
25
--- START is prefixed
26
+ --- @param fmt string for string.format
27
+ --- @vararg any arguments for string.format
26
28
--- @return number nanos to pass to profile_end
27
29
function 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
29
34
return 0
30
35
end
31
- M .line (" profile" , " START " .. (fmt or " ???" ), ... )
32
- return vim .loop .hrtime ()
33
36
end
34
37
35
- --- Write to log file via M.line
38
+ --- Write profile end to log file
36
39
--- END is prefixed and duration in seconds is suffixed
37
40
--- @param start number nanos returned from profile_start
41
+ --- @param fmt string for string.format
42
+ --- @vararg any arguments for string.format
38
43
function 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" , ... )
41
47
end
42
- local millis = start and math.modf ((vim .loop .hrtime () - start ) / 1000000 ) or - 1
43
- M .line (" profile" , " END " .. (fmt or " ???" ) .. " " .. millis .. " ms" , ... )
44
48
end
45
49
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
48
55
function 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 )
50
66
end
51
67
52
68
function M .setup (opts )
0 commit comments