Annotate

util/debug.lua @ 7947:24170d74b00b

core: Split some very long lines [luacheck]
author Kim Alvefur <zash@zash.se>
date Sat, 04 Mar 2017 17:49:48 +0100
parent 7182:858ccafbd823
child 8322:cd15fc505b62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Variables ending with these names will not
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- have their values printed ('password' includes
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- 'new_password', etc.)
7182
858ccafbd823 util.debug: Silence luacheck warning about modifying 'debug' lib
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
4 --
858ccafbd823 util.debug: Silence luacheck warning about modifying 'debug' lib
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
5 -- luacheck: ignore 122/debug
858ccafbd823 util.debug: Silence luacheck warning about modifying 'debug' lib
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
6
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local censored_names = {
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 password = true;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 passwd = true;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 pass = true;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 pwd = true;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 };
4522
29f75c2af90e util.debug: Move optimal line length (default 65) into a variable
Matthew Wild <mwild1@gmail.com>
parents: 4521
diff changeset
13 local optimal_line_length = 65;
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
15 local termcolours = require "util.termcolours";
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
16 local getstring = termcolours.getstring;
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
17 local styles;
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
18 do
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
19 local _ = termcolours.getstyle;
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
20 styles = {
4699
c66179261551 util.debug: Remove 'white' from boundary style (leave at default colour)
Matthew Wild <mwild1@gmail.com>
parents: 4693
diff changeset
21 boundary_padding = _("bright");
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
22 filename = _("bright", "blue");
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
23 level_num = _("green");
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
24 funcname = _("yellow");
4524
816c319a09d8 util.debug: Add a touch of colour to source locations
Matthew Wild <mwild1@gmail.com>
parents: 4523
diff changeset
25 location = _("yellow");
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
26 };
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
27 end
4680
8834f220ab91 util.debug: Turn into a real-ish module ('debugx'), and require you call use() to override debug.traceback()
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
28
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
29 local function get_locals_table(thread, level)
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local locals = {};
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 for local_num = 1, math.huge do
5787
9a22586f67eb util.debug: Fix level of locals when inspecting a coroutine
Matthew Wild <mwild1@gmail.com>
parents: 5786
diff changeset
32 local name, value;
9a22586f67eb util.debug: Fix level of locals when inspecting a coroutine
Matthew Wild <mwild1@gmail.com>
parents: 5786
diff changeset
33 if thread then
9a22586f67eb util.debug: Fix level of locals when inspecting a coroutine
Matthew Wild <mwild1@gmail.com>
parents: 5786
diff changeset
34 name, value = debug.getlocal(thread, level, local_num);
9a22586f67eb util.debug: Fix level of locals when inspecting a coroutine
Matthew Wild <mwild1@gmail.com>
parents: 5786
diff changeset
35 else
9a22586f67eb util.debug: Fix level of locals when inspecting a coroutine
Matthew Wild <mwild1@gmail.com>
parents: 5786
diff changeset
36 name, value = debug.getlocal(level+1, local_num);
9a22586f67eb util.debug: Fix level of locals when inspecting a coroutine
Matthew Wild <mwild1@gmail.com>
parents: 5786
diff changeset
37 end
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if not name then break; end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 table.insert(locals, { name = name, value = value });
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return locals;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
44 local function get_upvalues_table(func)
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local upvalues = {};
4418
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
46 if func then
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
47 for upvalue_num = 1, math.huge do
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
48 local name, value = debug.getupvalue(func, upvalue_num);
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
49 if not name then break; end
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
50 table.insert(upvalues, { name = name, value = value });
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
51 end
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return upvalues;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
56 local function string_from_var_table(var_table, max_line_len, indent_str)
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local var_string = {};
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local col_pos = 0;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 max_line_len = max_line_len or math.huge;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 indent_str = "\n"..(indent_str or "");
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 for _, var in ipairs(var_table) do
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local name, value = var.name, var.value;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 if name:sub(1,1) ~= "(" then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 if type(value) == "string" then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if censored_names[name:match("%a+$")] then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 value = "<hidden>";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 else
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 value = ("%q"):format(value);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 else
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 value = tostring(value);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if #value > max_line_len then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 value = value:sub(1, max_line_len-3).."…";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local str = ("%s = %s"):format(name, tostring(value));
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 col_pos = col_pos + #str;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if col_pos > max_line_len then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 table.insert(var_string, indent_str);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 col_pos = 0;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 table.insert(var_string, str);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 if #var_string == 0 then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 return nil;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 else
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 return "{ "..table.concat(var_string, ", "):gsub(indent_str..", ", indent_str).." }";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
92 local function get_traceback_table(thread, start_level)
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 local levels = {};
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 for level = start_level, math.huge do
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 local info;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 if thread then
5785
b2d6c5f94aa5 util.debug: Fixes to make coroutine tracebacks work properly
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
97 info = debug.getinfo(thread, level);
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 else
4693
7ef4faa056fb util.debug: Some more magic constant fiddling. Don't ask me.
Matthew Wild <mwild1@gmail.com>
parents: 4684
diff changeset
99 info = debug.getinfo(level+1);
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 if not info then break; end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4778
diff changeset
102
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 levels[(level-start_level)+1] = {
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 level = level;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 info = info;
5786
d50005796a26 util.debug: Further fix to display locals in extended tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 5785
diff changeset
106 locals = get_locals_table(thread, level+(thread and 0 or 1));
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 upvalues = get_upvalues_table(info.func);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 };
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4778
diff changeset
109 end
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 return levels;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
113 local function build_source_boundary_marker(last_source_desc)
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
114 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
4525
e00b4ec5fca4 util.debug: Move boundary markers to top of relevant sections of the stack trace (easier to follow)
Matthew Wild <mwild1@gmail.com>
parents: 4524
diff changeset
115 return getstring(styles.boundary_padding, "v"..padding).." "..getstring(styles.filename, last_source_desc).." "..getstring(styles.boundary_padding, padding..(#last_source_desc%2==0 and "-v" or "v "));
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
116 end
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
117
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
118 local function _traceback(thread, message, level)
4684
dc70c4ffb66d Merge timber->trunk - thanks everyone!
Matthew Wild <mwild1@gmail.com>
parents: 4525 4680
diff changeset
119
4777
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
120 -- Lua manual says: debug.traceback ([thread,] [message [, level]])
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
121 -- I fathom this to mean one of:
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
122 -- ()
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
123 -- (thread)
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
124 -- (message, level)
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
125 -- (thread, message, level)
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
126
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
127 if thread == nil then -- Defaults
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
128 thread, message, level = coroutine.running(), message, level;
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
129 elseif type(thread) == "string" then
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 thread, message, level = coroutine.running(), thread, message;
4777
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
131 elseif type(thread) ~= "thread" then
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
132 return nil; -- debug.traceback() does this
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
4777
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
134
5785
b2d6c5f94aa5 util.debug: Fixes to make coroutine tracebacks work properly
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
135 level = level or 0;
4777
74ae0433f8dd util.debug: Re-fix parameter handling (I think it matches debug.traceback() more accurately now) and document level fudge
Matthew Wild <mwild1@gmail.com>
parents: 4699
diff changeset
136
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 message = message and (message.."\n") or "";
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4778
diff changeset
138
5785
b2d6c5f94aa5 util.debug: Fixes to make coroutine tracebacks work properly
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
139 -- +3 counts for this function, and the pcall() and wrapper above us, the +1... I don't know.
b2d6c5f94aa5 util.debug: Fixes to make coroutine tracebacks work properly
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
140 local levels = get_traceback_table(thread, level+(thread == nil and 4 or 0));
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4778
diff changeset
141
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
142 local last_source_desc;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4778
diff changeset
143
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 local lines = {};
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 for nlevel, level in ipairs(levels) do
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 local info = level.info;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 local line = "...";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 local func_type = info.namewhat.." ";
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
149 local source_desc = (info.short_src == "[C]" and "C code") or info.short_src or "Unknown";
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 if func_type == " " then func_type = ""; end;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 if info.short_src == "[C]" then
4524
816c319a09d8 util.debug: Add a touch of colour to source locations
Matthew Wild <mwild1@gmail.com>
parents: 4523
diff changeset
152 line = "[ C ] "..func_type.."C function "..getstring(styles.location, (info.name and ("%q"):format(info.name) or "(unknown name)"));
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 elseif info.what == "main" then
4524
816c319a09d8 util.debug: Add a touch of colour to source locations
Matthew Wild <mwild1@gmail.com>
parents: 4523
diff changeset
154 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline);
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 else
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 local name = info.name or " ";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 if name ~= " " then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 name = ("%q"):format(name);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 if func_type == "global " or func_type == "local " then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 func_type = func_type.."function ";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
4524
816c319a09d8 util.debug: Add a touch of colour to source locations
Matthew Wild <mwild1@gmail.com>
parents: 4523
diff changeset
163 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline).." in "..func_type..getstring(styles.funcname, name).." (defined on line "..info.linedefined..")";
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 end
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
165 if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
166 last_source_desc = source_desc;
4525
e00b4ec5fca4 util.debug: Move boundary markers to top of relevant sections of the stack trace (easier to follow)
Matthew Wild <mwild1@gmail.com>
parents: 4524
diff changeset
167 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 nlevel = nlevel-1;
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
170 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 local npadding = (" "):rep(#tostring(nlevel));
5785
b2d6c5f94aa5 util.debug: Fixes to make coroutine tracebacks work properly
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
172 if level.locals then
b2d6c5f94aa5 util.debug: Fixes to make coroutine tracebacks work properly
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
173 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
b2d6c5f94aa5 util.debug: Fixes to make coroutine tracebacks work properly
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
174 if locals_str then
b2d6c5f94aa5 util.debug: Fixes to make coroutine tracebacks work properly
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
175 table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
b2d6c5f94aa5 util.debug: Fixes to make coroutine tracebacks work properly
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
176 end
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 end
4522
29f75c2af90e util.debug: Move optimal line length (default 65) into a variable
Matthew Wild <mwild1@gmail.com>
parents: 4521
diff changeset
178 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding);
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 if upvalues_str then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 end
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
183
4525
e00b4ec5fca4 util.debug: Move boundary markers to top of relevant sections of the stack trace (easier to follow)
Matthew Wild <mwild1@gmail.com>
parents: 4524
diff changeset
184 -- table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
185
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 return message.."stack traceback:\n"..table.concat(lines, "\n");
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 end
4680
8834f220ab91 util.debug: Turn into a real-ish module ('debugx'), and require you call use() to override debug.traceback()
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
188
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
189 local function traceback(...)
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
190 local ok, ret = pcall(_traceback, ...);
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
191 if not ok then
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
192 return "Error in error handling: "..ret;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
193 end
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
194 return ret;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
195 end
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
196
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
197 local function use()
4680
8834f220ab91 util.debug: Turn into a real-ish module ('debugx'), and require you call use() to override debug.traceback()
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
198 debug.traceback = traceback;
8834f220ab91 util.debug: Turn into a real-ish module ('debugx'), and require you call use() to override debug.traceback()
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
199 end
8834f220ab91 util.debug: Turn into a real-ish module ('debugx'), and require you call use() to override debug.traceback()
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
200
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
201 return {
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
202 get_locals_table = get_locals_table;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
203 get_upvalues_table = get_upvalues_table;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
204 string_from_var_table = string_from_var_table;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
205 get_traceback_table = get_traceback_table;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
206 traceback = traceback;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
207 use = use;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5787
diff changeset
208 };