4 luatexbase.provides_module({
5 name = 'luatexja.debug',
10 module('luatexja.debug', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
15 -------------------- pretty-print
17 local function get_serialize_param()
18 return table.serialize_functions,
19 table.serialize_compact,
20 table.serialize_inline
22 local function set_serialize_param(s_f, s_c, s_i)
23 table.serialize_functions = s_f
24 table.serialize_compact = s_c
25 table.serialize_inline = s_i
28 local function normal_serialize(t)
29 local s_f, s_c, s_i = get_serialize_param()
30 set_serialize_param(true, true, true)
31 local ret = table.serialize(t, false, false, true)
32 set_serialize_param(s_f, s_c, s_i)
36 function table_tosource(t)
37 if not next(t) then return "{}" end
38 local res_n = "\127"..normal_serialize({t}).."\127"
39 local s, e, cap = res_n:find("\127{\n ({ .* }),\n}\127")
40 if s == 1 and e == res_n:len() then return cap
41 else return normal_serialize(t)
45 function function_tosource(f)
46 local res = normal_serialize({f})
47 return res:sub(4, res:len() - 3)
50 --! 値 v をそれを表すソース文字列に変換する.
51 --! lualibs の table.serialize() の処理を利用している.
54 if tv == "function" then return function_tosource(v)
55 elseif tv == "table" then return table_tosource(v)
56 elseif tv == "string" then return string.format('%q', v)
57 else return tostring(v)
61 local function coerce(f, v)
62 if f == "q" then return "s", tosource(v)
63 elseif f == "s" then return f, tostring(v)
64 else return f, tonumber(v) or 0
68 local function do_pformat(fmt, ...)
69 fmt = fmt:gsub("``", "\127"):gsub("`", "%%"):gsub("\127", "`")
70 local i, na, a = 0, {}, {...}
71 local function proc(p, f)
72 i = i + 1; f, na[i] = coerce(f, a[i])
75 fmt = fmt:gsub("(%%[-+#]?[%d%.]*)([a-zA-Z])", proc)
76 return fmt:format(unpack(na))
79 --! string.format() の拡張版. 以下の点が異なる.
80 --! - %q は全ての型について tosource() に変換
81 --! - <%> の代わりに <`> も使える (TeX での使用のため)
82 --! - %d, %s 等でキャストを行う
83 function pformat(fmt, ...)
84 if type(fmt) == "string" then
85 return do_pformat(fmt, ...)
91 -------------------- debug logging
93 local debug_show_term = true
94 local debug_show_log = true
99 --! デバッグログをログファイルに出力するか
104 local function write_debug_log(s)
106 if debug_show_term and debug_show_log then
107 texio.write_nl("term and log", s)
108 elseif debug_show_term and not debug_show_log then
109 texio.write_nl("term", s)
110 elseif not debug_show_term and debug_show_log then
111 texio.write_nl("log", s)
115 --! デバッグログ出力. 引数は pformat() と同じ.
117 if debug_show_term or debug_show_log then
118 write_debug_log("%DEBUG:"..pformat(...))
122 --! デバッグログ出力, パッケージ名付き.
123 function package_debug(pkg, ...)
124 if debug_show_term or debug_show_log then
125 write_debug_log("%DEBUG("..pkg.."):"..pformat(...))
129 --! パッケージ名付きデバッグログ出力器を得る.
130 function debug_logger(pkg)
131 return function(...) package_debug(pkg, ...) end
134 if luatexja.base then
135 luatexja.base.debug = debug
136 luatexja.base.package_debug = package_debug
137 luatexja.base.debug_logger = debug_logger
138 luatexja.base.show_term = show_term
139 luatexja.base.show_log = show_log
142 -------------------- all done