summaryrefslogtreecommitdiff
path: root/tex/context/base/trac-log.lua
blob: ae1130e65502ab6efbe8227f5b9648db2f2ba17b (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
if not modules then modules = { } end modules ['trac-log'] = {
    version   = 1.001,
    comment   = "companion to trac-log.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- xml logging is only usefull in normal runs, not in ini mode
-- it looks like some tex logging (like filenames) is broken (no longer
-- interceoted at the tex end so the xml variant is not that useable now)

--~ io.stdout:setvbuf("no")
--~ io.stderr:setvbuf("no")

local write_nl, write = texio and texio.write_nl or print, texio and texio.write or io.write
local format, gmatch = string.format, string.gmatch
local texcount = tex and tex.count

--[[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]]--

logs = logs or { }

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

local moreinfo = [[
More information about ConTeXt and the tools that come with it can be found at:

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
wiki     : http://contextgarden.net
]]

local functions = {
    'report', 'status', 'start', 'stop', 'push', 'pop', 'line', 'direct',
    'start_run', 'stop_run',
    'start_page_number', 'stop_page_number',
    'report_output_pages', 'report_output_log',
    'report_tex_stat', 'report_job_stat',
    'show_open', 'show_close', 'show_load',
    'dummy',
}

local method = "nop"

function logs.set_method(newmethod)
    method = newmethod
    -- a direct copy might be faster but let's try this for a while
    setmetatable(logs, { __index = logs[method] })
end

function logs.get_method()
    return method
end

-- installer

local data = { }

function logs.new(category)
    local logger = data[category]
    if not logger then
        logger = function(...)
            logs.report(category,...)
        end
        data[category] = logger
    end
    return logger
end

--~ local report = logs.new("fonts")


-- nop logging (maybe use __call instead)

local noplog = { } logs.nop = noplog  setmetatable(logs, { __index = noplog })

for i=1,#functions do
    noplog[functions[i]] = function() end
end

-- tex logging

local texlog = { }  logs.tex = texlog  setmetatable(texlog, { __index = noplog })

function texlog.report(a,b,c,...)
    if c then
        write_nl(format("%-16s> %s\n",a,format(b,c,...)))
    elseif b then
        write_nl(format("%-16s> %s\n",a,b))
    else
        write_nl(format("%-16s>\n",a))
    end
end

function texlog.status(a,b,c,...)
    if c then
        write_nl(format("%-16s: %s\n",a,format(b,c,...)))
    elseif b then
        write_nl(format("%-16s: %s\n",a,b)) -- b can have %'s
    else
        write_nl(format("%-16s:>\n",a))
    end
end

function texlog.line(fmt,...) -- new
    if fmt then
        write_nl(format(fmt,...))
    else
        write_nl("")
    end
end

local real, user, sub

function texlog.start_page_number()
    real, user, sub = texcount.realpageno, texcount.userpageno, texcount.subpageno
end

local report_pages = logs.new("pages") -- not needed but saves checking when we grep for it

function texlog.stop_page_number()
    if real > 0 then
        if user > 0 then
            if sub > 0 then
                report_pages("flushing realpage %s, userpage %s, subpage %s",real,user,sub)
            else
                report_pages("flushing realpage %s, userpage %s",real,user)
            end
        else
            report_pages("flushing realpage %s",real)
        end
    else
        report_pages("flushing page")
    end
    io.flush()
end

texlog.report_job_stat = statistics and statistics.show_job_stat

-- xml logging

local xmllog = { }  logs.xml = xmllog  setmetatable(xmllog, { __index = noplog })

function xmllog.report(category,fmt,s,...) -- new
    if s then
        write_nl(format("<r category='%s'>%s</r>",category,format(fmt,s,...)))
    elseif fmt then
        write_nl(format("<r category='%s'>%s</r>",category,fmt))
    else
        write_nl(format("<r category='%s'/>",category))
    end
end

function xmllog.status(category,fmt,s,...)
    if s then
        write_nl(format("<s category='%s'>%s</r>",category,format(fmt,s,...)))
    elseif fmt then
        write_nl(format("<s category='%s'>%s</r>",category,fmt))
    else
        write_nl(format("<s category='%s'/>",category))
    end
end

function xmllog.line(fmt,...) -- new
    if fmt then
        write_nl(format("<r>%s</r>",format(fmt,...)))
    else
        write_nl("<r/>")
    end
end

function xmllog.start() write_nl("<%s>" ) end
function xmllog.stop () write_nl("</%s>") end
function xmllog.push () write_nl("<!-- ") end
function xmllog.pop  () write_nl(" -->" ) end

function xmllog.start_run()
    write_nl("<?xml version='1.0' standalone='yes'?>")
    write_nl("<job>") --  xmlns='www.pragma-ade.com/luatex/schemas/context-job.rng'
    write_nl("")
end

function xmllog.stop_run()
    write_nl("</job>")
end

function xmllog.start_page_number()
    write_nl(format("<p real='%s' page='%s' sub='%s'", texcount.realpageno, texcount.userpageno, texcount.subpageno))
end

function xmllog.stop_page_number()
    write("/>")
    write_nl("")
end

function xmllog.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 xmllog.report_output_log()
    -- nothing
end

function xmllog.report_tex_stat(k,v)
    write_nl("log","<v k='"..k.."'>"..tostring(v).."</v>")
end

local nesting = 0

function xmllog.show_open(name)
    nesting = nesting + 1
    write_nl(format("<f l='%s' n='%s'>",nesting,name))
end

function xmllog.show_close(name)
    write("</f> ")
    nesting = nesting - 1
end

function xmllog.show_load(name)
    write_nl(format("<f l='%s' n='%s'/>",nesting+1,name))
end

-- initialization

if tex and (tex.jobname or tex.formatname) then
    -- todo: this can be set in mtxrun ... or maybe we should just forget about this alternative format
    if (os.getenv("mtx.directives.logmethod") or os.getenv("mtx_directives_logmethod")) == "xml" then
        logs.set_method('xml')
    else
        logs.set_method('tex')
    end
else
    logs.set_method('nop')
end

-- logging in runners -> these are actually the nop loggers

local name, banner = 'report', 'context'

function noplog.report(category,fmt,...) -- todo: fmt,s
    if fmt then
        write_nl(format("%s | %s: %s",name,category,format(fmt,...)))
    elseif category then
        write_nl(format("%s | %s",name,category))
    else
        write_nl(format("%s |",name))
    end
end

noplog.status = noplog.report -- just to be sure, never used

function noplog.simple(fmt,...) -- todo: fmt,s
    if fmt then
        write_nl(format("%s | %s",name,format(fmt,...)))
    else
        write_nl(format("%s |",name))
    end
end

if utils then
    utils.report = function(...) logs.simple(...) end
end

function logs.setprogram(newname,newbanner)
    name, banner = newname, newbanner
end

function logs.extendbanner(newbanner)
    banner = banner .. " | ".. newbanner
end

function logs.reportlines(str) -- todo: <lines></lines>
    for line in gmatch(str,"(.-)[\n\r]") do
        logs.report(line)
    end
end

function logs.reportline() -- for scripts too
    logs.report()
end

function logs.simpleline()
    logs.report()
end

function logs.simplelines(str) -- todo: <lines></lines>
    for line in gmatch(str,"(.-)[\n\r]") do
        logs.simple(line)
    end
end

function logs.reportbanner() -- for scripts too
    logs.report(banner)
end

function logs.help(message,option)
    logs.reportbanner()
    logs.reportline()
    logs.reportlines(message)
    if option ~= "nomoreinfo" then
        logs.reportline()
        logs.reportlines(moreinfo)
    end
end

-- logging to a file

--~ local syslogname = "oeps.xxx"
--~
--~ for i=1,10 do
--~     logs.system(syslogname,"context","test","fonts","font %s recached due to newer version (%s)","blabla","123")
--~ end

function logs.system(whereto,process,jobname,category,...)
    local message = format("%s %s => %s => %s => %s\r",os.date("%d/%m/%y %H:%m:%S"),process,jobname,category,format(...))
    for i=1,10 do
        local f = io.open(whereto,"a")
        if f then
            f:write(message)
            f:close()
            break
        else
            sleep(0.1)
        end
    end
end

-- bonus

function logs.fatal(where,...)
    logs.report(where,"fatal error: %s, aborting now",format(...))
    os.exit()
end

--~ the traditional tex page number logging
--~
--~ function logs.tex.start_page_number()
--~     local real, user, sub = texcount.realpageno, texcount.userpageno, texcount.subpageno
--~     if real > 0 then
--~         if user > 0 then
--~             if sub > 0 then
--~                 write(format("[%s.%s.%s",real,user,sub))
--~             else
--~                 write(format("[%s.%s",real,user))
--~             end
--~         else
--~             write(format("[%s",real))
--~         end
--~     else
--~         write("[-")
--~     end
--~ end
--~
--~ function logs.tex.stop_page_number()
--~     write("]")
--~ end