]> git.lizzy.rs Git - luairc.git/blob - src/irc/debug.lua
414b49deefc33b1995eaddf83b756bc0d21dba9d
[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 -- internal functions {{{
23 -- _message {{{
24 --
25 -- Output a debug message.
26 -- @param msg_type Arbitrary string corresponding to the type of message
27 -- @param msg      Message text
28 -- @param color    Which terminal code to use for color output (defaults to
29 --                 dark gray)
30 function _message(msg_type, msg, color)
31     if ON then
32         local endcolor = ""
33         if COLOR and outfile == io.stdout then
34             color = color or "\027[1;30m"
35             endcolor = "\027[0m"
36         else
37             color = ""
38             endcolor = ""
39         end
40         outfile:write(color .. msg_type .. ": " .. msg .. endcolor .. "\n")
41     end
42 end
43 -- }}}
44
45 -- _err {{{
46 --
47 -- Signal an error. Writes the error message to the screen in red and calls
48 -- error().
49 -- @param msg Error message
50 -- @see error
51 function _err(msg)
52     _message("ERR", msg, "\027[0;31m")
53     base.error(msg, 2)
54 end
55 -- }}}
56
57 -- _warn {{{
58 --
59 -- Signal a warning. Writes the warning message to the screen in yellow.
60 -- @param msg Warning message
61 function _warn(msg)
62     _message("WARN", msg, "\027[0;33m")
63 end
64 -- }}}
65 -- }}}
66
67 -- public functions {{{
68 -- enable {{{
69 ---
70 -- Turns on debug output.
71 function enable()
72     ON = true
73 end
74 -- }}}
75
76 -- disable {{{
77 ---
78 -- Turns off debug output.
79 function disable()
80     ON = false
81 end
82 -- }}}
83
84 -- set_output {{{
85 ---
86 -- Redirects output to a file rather than stdout.
87 -- @param file File to write debug output to
88 function set_output(file)
89     outfile = base.assert(io.open(file))
90 end
91 -- }}}
92 -- }}}