OSDN Git Service

Moved Lua codes of test17-priority.tex into ltj-adjust.lua.
[luatex-ja/luatexja.git] / src / ltj-debug.lua
1 --
2 -- luatexja/debug.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.debug',
6   date = '2011/04/01',
7   version = '0.1',
8   description = '',
9 })
10 module('luatexja.debug', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
13 local table = table
14
15 -------------------- pretty-print
16
17 local function get_serialize_param()
18   return table.serialize_functions,
19          table.serialize_compact,
20          table.serialize_inline
21 end
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
26 end
27
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)
33   return ret
34 end
35
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)
42   end
43 end
44
45 function function_tosource(f)
46   local res = normal_serialize({f})
47   return res:sub(4, res:len() - 3)
48 end
49
50 --! 値 v をそれを表すソース文字列に変換する.
51 --! lualibs の table.serialize() の処理を利用している.
52 function tosource(v)
53   local tv = type(v)
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)
58   end
59 end
60
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
65   end
66 end
67
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])
73     return p..f
74   end
75   fmt = fmt:gsub("(%%[-+#]?[%d%.]*)([a-zA-Z])", proc)
76   return fmt:format(unpack(na))
77 end
78
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, ...)
86   else 
87     return tosource(fmt)
88   end
89 end
90
91 -------------------- debug logging
92
93 local debug_show_term = true
94 local debug_show_log = true
95 --! デバッグログを端末に出力するか
96 function show_term(v)
97   debug_show_term = v
98 end
99 --! デバッグログをログファイルに出力するか
100 function show_log(v)
101   debug_show_log = v
102 end
103
104 local function write_debug_log(s)
105   local target
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)
112   end
113 end
114
115 --! デバッグログ出力. 引数は pformat() と同じ.
116 function debug(...)
117   if debug_show_term or debug_show_log then
118     write_debug_log("%DEBUG:"..pformat(...))
119   end
120 end
121
122 --! デバッグログ出力, パッケージ名付き.
123 function package_debug(pkg, ...)
124   if debug_show_term or debug_show_log then
125     write_debug_log("%DEBUG("..pkg.."):"..pformat(...))
126   end
127 end
128
129 --! パッケージ名付きデバッグログ出力器を得る.
130 function debug_logger(pkg)
131   return function(...) package_debug(pkg, ...) end
132 end
133
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
140 end
141
142 -------------------- all done
143 -- EOF