]> git.lizzy.rs Git - luairc.git/blob - src/irc/debug.lua
d60e38ff27fabdab19ca7cac951788e7f6cbace0
[luairc.git] / src / irc / debug.lua
1 ---
2 -- Basic debug output
3 -- initialization {{{
4 local base = _G
5 local io =   require 'io'
6 -- }}}
7
8 ---
9 -- This module implements a few useful debug functions for use throughout the
10 -- rest of the code.
11 module 'irc.debug'
12
13 -- defaults {{{
14 COLOR = true
15 -- }}}
16
17 -- local variables {{{
18 local ON = false
19 local outfile = io.output()
20 -- }}}
21
22 -- public functions {{{
23 -- enable {{{
24 ---
25 -- Turns on debug output.
26 function enable()
27     ON = true
28 end
29 -- }}}
30
31 -- disable {{{
32 ---
33 -- Turns off debug output.
34 function disable()
35     ON = false
36 end
37 -- }}}
38
39 -- set_output {{{
40 ---
41 -- Redirects output to a file rather than stdout.
42 -- @param file File to write debug output to
43 function set_output(file)
44     outfile = base.assert(io.open(file))
45 end
46 -- }}}
47
48 -- message {{{
49 -- TODO: disable color when we are writing to a file
50 --
51 -- Output a debug message.
52 -- @param msg_type Arbitrary string corresponding to the type of message
53 -- @param msg      Message text
54 -- @param color    Which terminal code to use for color output (defaults to
55 --                 dark gray)
56 function message(msg_type, msg, color)
57     if ON then
58         local endcolor = ""
59         if COLOR then
60             color = color or "\027[1;30m"
61             endcolor = "\027[0m"
62         else
63             color = ""
64             endcolor = ""
65         end
66         outfile:write(color .. msg_type .. ": " .. msg .. endcolor .. "\n")
67     end
68 end
69 -- }}}
70
71 -- err {{{
72 --
73 -- Signal an error. Writes the error message to the screen in red and calls
74 -- error().
75 -- @param msg Error message
76 -- @see error
77 function err(msg)
78     message("ERR", msg, "\027[0;31m")
79     base.error(msg, 2)
80 end
81 -- }}}
82
83 -- warn {{{
84 --
85 -- Signal a warning. Writes the warning message to the screen in yellow.
86 -- @param msg Warning message
87 function warn(msg)
88     message("WARN", msg, "\027[0;33m")
89 end
90 -- }}}
91 -- }}}