summaryrefslogtreecommitdiff
path: root/tex/context/base/luat-log.lua
blob: faecf7e29162ab04f880114da637f70ca519410e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
if not modules then modules = { } end modules ['luat-log'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.tex",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

--[[ldx--
<p>This is a prelude to a more extensive logging module. For the sake
of parsing log files, in addition to the standard logging we will
provide an <l n='xml'/> structured file. Actually, any logging that
is hooked into callbacks will be \XML\ by default.</p>
--ldx]]--

input = input or { }
logs  = logs  or { }

--[[ldx--
<p>This looks pretty ugly but we need to speed things up a bit.</p>
--ldx]]--

logs.levels = {
    ['error']   = 1,
    ['warning'] = 2,
    ['info']    = 3,
    ['debug']   = 4
}

logs.functions = {
    'error', 'warning', 'info', 'debug', 'report',
    'start', 'stop', 'push', 'pop'
}

logs.callbacks  = {
    'start_page_number',
    'stop_page_number',
    'report_output_pages',
    'report_output_log'
}

logs.xml = logs.xml or { }
logs.tex = logs.tex or { }

logs.level = 0

do
    local write_nl, write, format = texio.write_nl or print, texio.write or print, string.format

    function logs.xml.debug(category,str)
        if logs.level > 3 then write_nl(format("<d category='%s'>%s</d>",category,str)) end
    end
    function logs.xml.info(category,str)
        if logs.level > 2 then write_nl(format("<i category='%s'>%s</i>",category,str)) end
    end
    function logs.xml.warning(category,str)
        if logs.level > 1 then write_nl(format("<w category='%s'>%s</w>",category,str)) end
    end
    function logs.xml.error(category,str)
        if logs.level > 0 then write_nl(format("<e category='%s'>%s</e>",category,str)) end
    end
    function logs.xml.report(category,str)
        write_nl(format("<r category='%s'>%s</r>",category,str))
    end

    function logs.xml.start() if logs.level > 0 then tw("<%s>" ) end end
    function logs.xml.stop () if logs.level > 0 then tw("</%s>") end end
    function logs.xml.push () if logs.level > 0 then tw("<!-- ") end end
    function logs.xml.pop  () if logs.level > 0 then tw(" -->" ) end end

    function logs.tex.debug(category,str)
        if logs.level > 3 then write_nl(format("debug >> %s: %s"  ,category,str)) end
    end
    function logs.tex.info(category,str)
        if logs.level > 2 then write_nl(format("info >> %s: %s"   ,category,str)) end
    end
    function logs.tex.warning(category,str)
        if logs.level > 1 then write_nl(format("warning >> %s: %s",category,str)) end
    end
    function logs.tex.error(category,str)
        if logs.level > 0 then write_nl(format("error >> %s: %s"  ,category,str)) end
    end
    function logs.tex.report(category,str)
        write_nl(format("report >> %s: %s"  ,category,str))
    end

    function logs.set_level(level)
        logs.level = logs.levels[level] or level
    end

    function logs.set_method(method)
        for _, v in pairs(logs.functions) do
            logs[v] = logs[method][v] or function() end
        end
        if callback and input[method] then
            for _, cb in pairs(logs.callbacks) do
                callback.register(cb, input[method][cb])
            end
        end
    end

    function logs.xml.start_page_number()
        write_nl(format("<p real='%s' page='%s' sub='%s'", tex.count[0], tex.count[1], tex.count[2]))
    end

    function logs.xml.stop_page_number()
        write("/>")
        write_nl("")
    end

    function logs.xml.report_output_pages(p,b)
        write_nl(format("<v k='pages' v='%s'/>", p))
        write_nl(format("<v k='bytes' v='%s'/>", b))
        write_nl("")
    end

    function logs.xml.report_output_log()
    end

end

logs.set_level('error')
logs.set_method('tex')