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