| 1 |
-- Copyright 2012 by Till Tantau |
| 2 |
-- |
| 3 |
-- This file may be distributed an/or modified |
| 4 |
-- |
| 5 |
-- 1. under the LaTeX Project Public License and/or |
| 6 |
-- 2. under the GNU Public License |
| 7 |
-- |
| 8 |
-- See the file doc/generic/pgf/licenses/LICENSE for more information |
| 9 |
|
| 10 |
-- @release $Header$ |
| 11 |
|
| 12 |
|
| 13 |
--- |
| 14 |
-- The |pgf| namespace lies in the global namespace. It is the only |
| 15 |
-- global table defined by \pgfname. The whole graph drawing system, |
| 16 |
-- in turn, lies in the table |pgf.gd|. |
| 17 |
|
| 18 |
pgf = {} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
-- Forward |
| 23 |
local tostring_table |
| 24 |
|
| 25 |
--- |
| 26 |
-- Writes debug info on the \TeX\ output, separating the parameters |
| 27 |
-- by spaces. The debug information will include a complete traceback |
| 28 |
-- of the stack, allowing you to see ``where you are'' inside the Lua |
| 29 |
-- program. |
| 30 |
-- |
| 31 |
-- Note that this function resides directly in the |pgf| table. The |
| 32 |
-- reason for this is that you can ``always use it'' since |pgf| is |
| 33 |
-- always available in the global name space. |
| 34 |
-- |
| 35 |
-- @param ... List of parameters to write to the \TeX\ output. |
| 36 |
|
| 37 |
function pgf.debug(...) |
| 38 |
local stacktrace = debug.traceback("",2) |
| 39 |
texio.write_nl(" ") |
| 40 |
texio.write_nl("Debug called for: ") |
| 41 |
-- this is to even print out nil arguments in between |
| 42 |
local args = {...} |
| 43 |
for i = 1, #args do |
| 44 |
if i ~= 1 then texio.write(", ") end |
| 45 |
texio.write(tostring_table(args[i], "", 5)) |
| 46 |
end |
| 47 |
texio.write_nl('') |
| 48 |
for w in string.gmatch(stacktrace, "/.-:.-:.-%c") do |
| 49 |
texio.write('by ', string.match(w,".*/(.*)")) |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
|
| 54 |
-- Helper function |
| 55 |
|
| 56 |
function tostring_table(t, prefix, depth) |
| 57 |
if type(t) ~= "table" or (getmetatable(t) and getmetatable(t).__tostring) or depth <= 0 then |
| 58 |
return type(t) == "string" and ('"' .. t .. '"') or tostring(t) |
| 59 |
else |
| 60 |
local r = "{\n" |
| 61 |
for k,v in pairs(t) do |
| 62 |
r = r .. prefix .. " " .. tostring(k) .. "=" .. |
| 63 |
(v==t and "self" or tostring_table(v, prefix .. " ", depth-1)) .. ",\n" |
| 64 |
end |
| 65 |
return r .. prefix .. "}" |
| 66 |
end |
| 67 |
end |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
return pgf |