When I use this to get the calling function and the current function, it looks like this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
debug.getinfo(2).name.." > "..debug.getinfo(1).name
since there are so many log entires being done in several different places, it would be helpful to place this chunk of code somewhere that can be referenced later, whenever a log entry is made, instead of adding it to every single log-function call. Part of the reason for my mod is to eliminate unnecessary repetitive calls to functions or duplicate entries of the same code, but if I add this chunk of code to every log-function call, it will be a whole lot of extra crap that I don't want in my code. However, I can not find any references on the internet, especially the lua to any method of putting code inside a variable, and when I try to just slap the code onto a variable, I get errors because the interpretor tries to execute the code when the variable is set. I need the variable to simply contain the code, so that when the variable is put somewhere, the code is run there as if it were typed out.
I cannot put it into a function, because it would skew the results of the debug.getinfo() functions to be relative to the function containing it, instead of the function where the code is supposed to run.
Is it even possible to put this code in a single place, not having it executed until a reference is made to it, wherein it will run as if it were actually written-out in place of the reference?
EDIT: I figured it out! I can put it into a function. I just increment both numbers by one, to ignore the current function (the "debug info getter"), do each debug.getinfo(n).name call separately and assign each to a variable, check if the variable is nil and if it is change the value to string "MAIN", then concatenate both variables with the string " -> " between them. It looks like this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
function <modID>.stacktrace()
local calling_function = debug.getinfo(3).name
local called_function = debug.getinfo(2).name
if calling_function == nil then calling_function = "MAIN" end
if called_function == nil then called_function = "MAIN" end
return calling_function.." -> "..called_function
end
--[[
This code is licensed under the GNU GPL v3.0 or later. If you run this and your computer blows up, I am not liable. It probablly won't blow up, but just in case it does, I am not liable.
--]]
This works perfectly for what I need it, as the function call "<modID>.stacktrace()" is shorter than "debug.getinfo(2).name.." -> "..debug.getinfo(1).name", keeping the code relatively clean.
If you need to use this code for something, it is licensed under the GNU GPL v3.0 (or later), and I would love to know if this helped.
EDIT2: If there was a better way to do this, please let me know. This function isn't as optimal as possible because I had to increment the stack number, but it works, and if there is a better method to do the same thing, I will gladly replace it with the better method.
EDIT3: This code fails if debug.getinfo(3) or debug.getinfo(2) returns nil, because it is not possible to access the .name field of nil, since nil is not a table. The only way for this to work is to also check whether debug.getinfo(3) and debug.getinfo(2) are nil, and avoid trying to access the .name field of either one that is nil if it is nil. I won't waste my time posting a human-readable implementation of the code that would do this properly, because it wouldn't be appreciated - it would be longer than expected, because it's so much easier to just use as little code as possible to get the expected value, only caring for having to calculate the expected input type(s), totally ignoring the possibility of an unexpected type being accidentally passed - for example, simply using the syntax of the and/or operators alone to filter the values, will produce a subtle flaw that can crash the server in certain exotic circumstances. Since part of the goal of my mod is to prevent the server crashing from stupid errors, I won't do that; I will implement some combination of the and/or operators' last-minute-fix "syntax" with logically rigid if-then-else blocks, such that it is not at all possible for an incorrect data type to be passed, ever - this will be somewhat longer "than it has to be", but I believe it's worth it, when the alternative is having your server crash for "I don't know why" reason.