OSDN Git Service

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