I am making a small script to print log info ect. I am using an indexed table to store variables and their states at each time in the code. When printing this info(see output log) the info is not displayed in the correct order of the time it was added.
I have tried to use table.sort but cannot seem to get it to work.
Is there a way of sorting this table without needing to do loops to find the smallest time as this would be very slow.
Post a comment if you need more info.
Module script in ReplicatedFirst
local _func = {} -- requiored for module script
local debugList = {} -- stores debug info
local debugEnb = true -- enables log
local prntLocal = true -- print local messages
local prntServer = true -- print server messages
-- Starts the debug process
function _func.Start(scriptName, scriptType) -- use these vars for the corect setup
-- local repStorage = game:GetService("ReplicatedFirst")
-- local debg = require(repStorage:WaitForChild("debugScript"))
-- local scriptName = debg.Start(script.Name, script.ClassName)
-- use debg.LogMsg(scriptName, "log msg")
if debugEnb then
local logNameExt = ""
if debugList[scriptName] == nil then
while debugList[scriptName .. logNameExt] ~= nil do
logNameExt = logNameExt .. tostring(math.random(0,9))
end
end
-- add to this format if you need to store more infomation
debugList[scriptName .. logNameExt] = {
["Script Type"] = scriptType,
["Msg"] = {},
["LogStart"] = math.floor(tick()*10000),
["Vars"] = {}
}
return scriptName .. logNameExt
end
end
-- Adds a new mesage log
function _func.LogMsg(scriptName, msg)
if debugEnb then
if debugList[scriptName] ~= nil then -- check if the loging exists
table.insert(debugList[scriptName]["Msg"], getTimeTxt(scriptName) .." \t" .. tostring(msg))
end
end
end
-- Adds a variable log
function _func.LogVar(scriptName, name, var)
if debugEnb then
if debugList[scriptName]["Vars"][name] == nil then
-- add new data entry
debugList[scriptName]["Vars"][name] = {["Time"] = getTimeTxt(scriptName)}
end
-- add data
table.insert(debugList[scriptName]["Vars"][name],{["Time"] = getTimeTxt(scriptName), ["Val"] = tostring(var), ["Type"] = type(var)})
end
end
-- prints out formatted log when the script has ended
function _func.End(scriptName)
local logEnd = math.floor(tick() * 10000)
if debugList[scriptName] == nil then return false end
if debugList[scriptName]["Script Type"] == "LocalScript" and prntLocal or debugList[scriptName]["Script Type"] == "Script" and prntServer then
print("------------------------------------------------------- Script Print ------------------------------------------------------- ")
print("Script Name:-\t" .. scriptName .. "\tType:-\t" .. debugList[scriptName]["Script Type"])
print("Log Started:-\t " .. debugList[scriptName]["LogStart"] .." sec(Unix)\tLog End:-\t" .. tostring(logEnd) .. " sec(Unix)\tRun Duration:-\t" .. string.format("%.4f", (logEnd - debugList[scriptName]["LogStart"])/10000) .. " sec")
print("------------------------------------------------------- Log Start ------------------------------------------------------- ")
-- print the message logs
if #debugList[scriptName]["Msg"] ~= 0 then
print("Time\tMessage\n")
-- print out all messages for the script
for index, value in ipairs(debugList[scriptName]["Msg"]) do
print(value)
end
end
-- print the variable log
if countIndex(debugList[scriptName]["Vars"]) ~= 0 then
if #debugList[scriptName]["Msg"] ~= 0 then print() end
print("Variable Add Time\tVariable Name\tTime\tValue(String)\tType\n")
for index, value in pairs(debugList[scriptName]["Vars"]) do
print(value["Time"] .. "\t\t" .. index)
for index2, value2 in ipairs(value) do
print("\t\t\t" .. value2["Time"] .. "\t" .. value2["Val"] .. "\t" .. value2["Type"])
end
end
end
print("------------------------------------------------------- Log End ------------------------------------------------------- ")
end
table.remove(debugList[scriptName]) -- clean up table
end
-- samll function for formatting time txt
function getTimeTxt(scriptName)
return "[" .. string.format("%.4f",((math.floor(tick() * 10000) - debugList[scriptName]["LogStart"]) / 10000)) .. "]"
end
-- small function to count index cannot use #
function countIndex(tableName)
local count = 0
for _ in pairs(tableName) do
if count >= 1 then break end
count = count + 1
end
return count
end
return _func
Test localscript in starter character scripts
-- get logging scripts
local repStorage = game:GetService("ReplicatedFirst")
local debg = require(repStorage:WaitForChild("debugScript"))
-- Logging infomation
local scriptName = debg.Start(script.Name, script.ClassName)
debg.LogMsg(scriptName, "Humanoid Found 1")
wait(1)
debg.LogMsg(scriptName, "Humanoid Found 2")
wait(2)
local num = 5
local num2 = 4
debg.LogVar(scriptName, "number", num)
debg.LogVar(scriptName, "number2", num2)
wait(2)
num = 4
num2 = 1
debg.LogVar(scriptName, "number", num)
debg.LogVar(scriptName, "number2", num2)
local num3 = 1
debg.LogVar(scriptName, "number3", num3)
wait(1)
debg.LogVar(scriptName, "number3", num2 - 1)
debg.End(scriptName) -- end the log
Output:-
------------------------------------------------------- Script Print -------------------------------------------------------
Script Name:- example script Type:- LocalScript
Log Started:- 14611492251258 sec(Unix) Log End:- 14611492311283 sec(Unix) Run Duration:- 6.0025 sec
------------------------------------------------------- Log Start -------------------------------------------------------
Time Message
[0.0000] Humanoid Found 1
[1.0009] Humanoid Found 2
Variable Add Time Variable Name Time Value(String) Type
[3.0017] number
[3.0017] 5 number
[5.0020] 4 number
[5.0020] number3
[5.0021] 1 number
[6.0025] 0 number
[3.0017] number2
[3.0017] 4 number
[5.0020] 1 number
------------------------------------------------------- Log End -------------------------------------------------------

4 comments
It is impossible to sort a table without some sort of looping mechanism. Even
table.sortuses one.TL;DR, how is the table sorted? Is it a dictionary? You can't sort a dictionary, because they don't have 'order' the same way an array does.
An intertion sort is a realitively simple algorithm with low complexity that you could use: http://wiki.roblox.com/index.php?title=Insertion_sort
Thanks for the link. I will probably just include a function to find the shortest time for each log, then just remove the log entry till none exist. Thanks for the help.