Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/luaotfload-log.lua

raw
1 -------------------------------------------------------------------------------
2 -- FILE: luaotfload-log.lua
3 -- DESCRIPTION: part of luaotfload / logging
4 -------------------------------------------------------------------------------
5
6
7 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") {
8 name = "luaotfload-log",
9 version = "3.29", --TAGVERSION
10 date = "2024-12-03", --TAGDATE
11 description = "luaotfload submodule / logging",
12 license = "GPL v2.0",
13 author = "Khaled Hosny, Elie Roux, Philipp Gesang",
14 copyright = "Luaotfload Development Team",
15 }
16
17 --[[doc--
18 The logging system is slow in general, as we always have the function
19 call overhead even if we aren’t going to output anything. On the other
20 hand, the more efficient approach followed by Context isn’t an option
21 because we lack a user interface to toggle per-subsystem tracing.
22 --doc]]--
23
24 local module_name = "luaotfload" --- prefix for messages
25 local debug = debug
26
27 luaotfload = luaotfload or { }
28 luaotfload.log = luaotfload.log or { }
29 local log = luaotfload.log
30
31 local ioopen = io.open
32 local iowrite = io.write
33 local lfsisdir = lfs.isdir
34 local lfsisfile = lfs.isfile
35 local osdate = os.date
36 local ostime = os.time
37 local osuuid = os.uuid
38 local select = select
39 local stringformat = string.format
40 local stringsub = string.sub
41 local tableconcat = table.concat
42 local texiowrite_nl = texio.write_nl
43 local texiowrite = texio.write
44 local type = type
45
46 local dummyfunction = function () end
47
48 local texjob = false
49 if tex and (tex.jobname or tex.formatname) then
50 --- TeX
51 texjob = true
52 end
53
54 local loglevel = 0 --- default
55 local logout = "log"
56
57 --- int -> bool
58 local function set_loglevel (n)
59 if type(n) == "number" then
60 loglevel = n
61 end
62 return true
63 end
64 log.set_loglevel = set_loglevel
65
66 --- unit -> int
67 local function get_loglevel ( )
68 return loglevel
69 end
70 log.get_loglevel = get_loglevel
71
72 local writeln --- pointer to terminal/log writer
73 local statusln --- terminal writer that reuses the current line
74 local first_status = true --- indicate the begin of a status region
75
76 local log_msg = [[
77 logging output redirected to %s
78 to monitor the progress run "tail -f %s" in another terminal
79 ]]
80
81 local tmppath = os.getenv "TMPDIR" or "/tmp"
82
83 local function choose_logfile ( )
84 if lfsisdir (tmppath) then
85 local fname
86 repeat --- ensure that file of that name doesn’t exist
87 fname = tmppath .. "/luaotfload-log-" .. osuuid()
88 until not lfsisfile (fname)
89 iowrite (stringformat (log_msg, fname, fname))
90 return ioopen (fname, "w")
91 end
92 --- missing /tmp
93 return false
94 end
95
96 local function set_logout (s, finalizers)
97 if s == "stdout" then
98 logout = "redirect"
99 elseif s == "file" then --- inject custom logger
100 logout = "redirect"
101 local chan = choose_logfile ()
102 chan:write (stringformat ("logging initiated at %s",
103 osdate ("%Y-%m-%d %H:%M:%S", --- i. e. osdate "%F %T"
104 ostime ())))
105 local writefile = function (...)
106 if select ("#", ...) == 2 then
107 chan:write (select (2, ...))
108 else
109 chan:write (select (1, ...))
110 end
111 end
112 local writefile_nl= function (...)
113 chan:write "\n"
114 if select ("#", ...) == 2 then
115 chan:write (select (2, ...))
116 else
117 chan:write (select (1, ...))
118 end
119 end
120
121 local writeln_orig = writeln
122
123 texiowrite = writefile
124 texiowrite_nl = writefile_nl
125 writeln = writefile_nl
126 statusln = dummyfunction
127
128 finalizers[#finalizers+1] = function ()
129 chan:write (stringformat ("\nlogging finished at %s\n",
130 osdate ("%Y-%m-%d %H:%M:%S", --- i. e. osdate "%F %T"
131 ostime ())))
132 chan:close ()
133 texiowrite = texio.write
134 texiowrite_nl = texio.write_nl
135 writeln = writeln_orig
136 end
137 --else --- remains “log”
138 end
139 return finalizers
140 end
141
142 log.set_logout = set_logout
143
144 local format_error_handler
145 if debug then
146 local debugtraceback = debug.traceback
147 function format_error_handler (err)
148 print ""
149 print (stringformat ("luaotfload error: %q", err))
150 print (stringformat ("Lua interpreter %s", debugtraceback ()))
151 print ""
152 end
153 else
154 function format_error_handler (err)
155 print ""
156 print (stringformat ("luaotfload error: %q", err))
157 print "Lua debug module not available; please enable for a backtrace"
158 print ""
159 end
160 end
161
162 local function basic_logger (category, fmt, ...)
163 local res = { module_name, "|", category or "UNKNOWN", ":" }
164 if fmt then
165 local ok, val = xpcall (stringformat, format_error_handler, fmt, ...)
166 if ok then
167 res [#res + 1] = val
168 else
169 res [#res + 1] = stringformat ("ERROR: %q", val)
170 end
171 end
172 texiowrite_nl (logout, tableconcat(res, " "))
173 end
174
175 --- with faux db update with maximum verbosity:
176 ---
177 --- --------- --------
178 --- buffering time (s)
179 --- --------- --------
180 --- full 4.12
181 --- line 4.20
182 --- none 4.39
183 --- --------- --------
184 ---
185
186 io.stdout:setvbuf "no"
187 io.stderr:setvbuf "no"
188
189 local kill_line = "\r\x1b[K"
190
191 if texjob == true then
192 --- We imitate the texio.* functions so the output is consistent.
193 function writeln (str)
194 iowrite "\n"
195 iowrite(str)
196 end
197 function statusln (str)
198 if first_status == false then
199 iowrite (kill_line)
200 else
201 iowrite "\n"
202 end
203 iowrite (str)
204 end
205 else
206 function writeln (str)
207 iowrite(str)
208 iowrite "\n"
209 end
210 function statusln (str)
211 if first_status == false then
212 iowrite (kill_line)
213 end
214 iowrite (str)
215 end
216 end
217
218 local function stdout (writer, category, ...)
219 local res = { module_name, "|", category, ":" }
220 local nargs = select("#", ...)
221 if nargs == 0 then
222 --writeln tableconcat(res, " ")
223 --return
224 elseif nargs == 1 then
225 res[5] = ... -- around 30% faster than unpack()
226 else
227 res[5] = stringformat(...)
228 end
229 writer (tableconcat(res, " "))
230 end
231
232 --- at default (zero), we aim to be quiet
233 local level_ids = { common = 1, loading = 2, search = 3 }
234
235 --[[doc--
236
237 The report() logger is used more or less all over luaotfload.
238 Its requirements are twofold:
239
240 1) Provide two logging channels, the terminal and the log file;
241 2) Allow for control over verbosity levels.
242
243 The first part is addressed by specifying the log *mode* as the
244 first argument that can be either “log”, meaning the log file, or
245 “both”: log file and stdout. Anything else is taken as referring to
246 stdout only.
247
248 Verbosity levels, though not as fine-grained as e.g. Context’s
249 system of tracers, allow keeping the logging spam caused by
250 different subsystems manageable. By default, luaotfload will not
251 emit anything if things are running smoothly on level zero. Only
252 warning messages are relayed, while the other messages are skipped
253 over. (This is a little sub-optimal performance-wise since the
254 function calls to the logger are executed regardless.) The log
255 level during a Luatex run can be adjusted by setting the “loglevel”
256 field in config.luaotfload, or by calling log.set_loglevel() as
257 defined above.
258
259 --doc]]--
260
261 local report = function (mode, lvl, ...)
262 if type(lvl) == "string" then
263 lvl = level_ids[lvl]
264 end
265 if not lvl then lvl = 0 end
266
267 if loglevel >= lvl then
268 if mode == "log" then
269 basic_logger (...)
270 elseif mode == "both" and logout ~= "redirect" then
271 basic_logger (...)
272 stdout (writeln, ...)
273 else
274 stdout (writeln, ...)
275 end
276 end
277 end
278
279 log.report = report
280
281 --[[doc--
282
283 status_logger -- Overwrites the most recently printed line of the
284 terminal. Its purpose is to provide feedback without spamming
285 stdout with irrelevant messages, i.e. when building the database.
286
287 Status logging must be initialized by calling status_start() and
288 properly reset via status_stop().
289
290 The arguments low and high indicate the loglevel threshold at which
291 linewise and full logging is triggered, respectively. E.g.
292
293 names_status (1, 4, "term", "Hello, world!")
294
295 will print nothing if the loglevel is less than one, reuse the
296 current line if the loglevel ranges from one to three inclusively,
297 and output the message on a separate line otherwise.
298
299 --doc]]--
300
301 local status_logger = function (mode, ...)
302 if mode == "log" then
303 basic_logger (...)
304 else
305 if mode == "both" and logout ~= "redirect" then
306 basic_logger (...)
307 stdout (statusln, ...)
308 else
309 stdout (statusln, ...)
310 end
311 first_status = false
312 end
313 end
314
315 --[[doc--
316
317 status_start -- Initialize status logging. This installs the status
318 logger if the loglevel is in the specified range, and the normal
319 logger otherwise. It also resets the first line state which
320 causing the next line printed using the status logger to not kill
321 the current line.
322
323 --doc]]--
324
325 local status_writer
326 local status_low = 99
327 local status_high = 99
328
329 local function status_start (low, high)
330 first_status = true
331 status_low = low
332 status_high = high
333
334 if os.type == "windows" --- Assume broken terminal.
335 or os.getenv "TERM" == "dumb"
336 then
337 status_writer = function (mode, ...)
338 report (mode, high, ...)
339 end
340 return
341 end
342
343 if low <= loglevel and loglevel < high then
344 status_writer = status_logger
345 else
346 status_writer = function (mode, ...)
347 report (mode, high, ...)
348 end
349 end
350 end
351
352 --[[doc--
353
354 status_stop -- Finalize a status region by outputting a newline and
355 printing a message.
356
357 --doc]]--
358
359 local status_stop = function (...)
360 if first_status == false then
361 status_writer(...)
362 if texjob == false then
363 writeln ""
364 end
365 end
366 end
367
368 log.names_status = function (...) status_writer (...) end
369 log.names_status_start = status_start
370 log.names_status_stop = status_stop
371
372 --[[doc--
373
374 The fontloader comes with the Context logging mechanisms
375 inaccessible. Instead, it provides dumb fallbacks based
376 on the functions in texio.write*() that can be overridden
377 by providing a function texio.reporter().
378
379 The fontloader output can be quite verbose, so we disable
380 it entirely by default.
381
382 --doc]]--
383
384 local function texioreporter (message)
385 report ("log", 2, message)
386 end
387
388 texio.reporter = texioreporter
389 --- vim:shiftwidth=4:expandtab:ft=lua