a kind of log file for print messages?

I use print commands to see whats going on in a codea project (cargo bot). Now I get lots and lots of helpfull print messages. How do I write them in a kind of log file to analyse them after the games stopped? Any ideas?

Codea doesn’t have the ability to save to a textfile. If you want to be able to analyse the logfile “offline” when the program is not running, the best solution I can think of is to create a “Log” tab in your code, and write the log file to that tab (as a very long comment)

Thanks Ignatz,
so the only way is using e.g. saveProjectTab(“Test”, “-- This is a test!”) ?

I think that’s the best way if you want to analyse offline, because it gives you a full screen log display

What I have done in the past is create a debug print. if the variable DEBUG=true then it will print. If no then no printing. This can be done on a tab by tab basis or in the main covering the whole project.

@Ignatz I really like that idea. Do you have an implementation of this I can copy?

Codea does allow you to use text files. You can write to and read from the files, but there isn’t much support or documentation for it. There is information in previous posts giving examples on how to do it.

Good idea @ignatz, I never thought of that, it would be simple to do. Either overload the print function or create a debug one. Think I’ll give it a go :slight_smile:

call dev.print() to print to a tab. Call dev.dump(table,indent char)to print a table to tab,


dev = {}
dev.debugStr = "--[[".. "--]]"

--Print to tab
dev.print = function(str)
    dev.debugStr = string.gsub(dev.debugStr,"--]]"," ").."\
".."- "..str.."--]]"
    saveProjectTab("DebugPrint",dev.debugStr) 

end
    
--Dump a table to tab
dev.dump = function(t,indent)
    local names = {}
    if not indent then indent = "" end
    for n,g in pairs(t) do
        table.insert(names,n)
    end
    table.sort(names)
    for i,n in pairs(names) do
        local v = t[n]
        if type(v) == "table" then
            if(v==t) then -- prevent endless loop if table contains reference to itself
                dev.print(indent..tostring(n)..": <-")
            else
                dev.print(indent..tostring(n)..":")
                dev.dump(v,indent.."   ")
            end
        else
            if type(v) == "function" then
                dev.print(indent..tostring(n).."()")
            else
                dev.print(indent..tostring(n)..": "..tostring(v))
            end
        end
    end
end

Thank you for your help. Now I get what I wanted :slight_smile:

I call your function from my own function:

function p(txt,a,b,c,d,e) 
    
    -- choose senders
    i_strfound = string.find(txt,"Main*",1,plain) 
    if i_strfound == nil then i_strfound = string.find(txt,"Splash*",1,plain) end
   --if i_strfound == nil then i_strfound = string.find(txt,"Screen*",1,plain) end
   -- ...
    
    if  i_strfound ~= nil then
        
            -- print only once 
            if t_pm[i_ppos]  ~= txt  then
    
                i_ppos = i_ppos + 1
                table.insert(t_pm,i_ppos,txt)     
                
                if a ~= nil and b ~= nil then
                    s_output = "-- " .. t_pm[i_ppos-1] .. "=>" .. t_pm[i_ppos] .. " ".. a
                               .. " " .. b
                elseif a ~= nil then
                    s_output = "-- " .. t_pm[i_ppos-1] .. "=>" .. t_pm[i_ppos] .. " ".. a             
                else
                    s_output = "-- " .. t_pm[i_ppos-1] .. "=>" .. t_pm[i_ppos]
                end

                dev.print(s_output)
       end
   end
    
end

e.g. a call from the Main tab:

touched(t)
p("Main touched(t)", t.x, t.y) -- call my clumsy first function in codea
...
end

Its kind of clumsy and not finished but it returns the result I was after. A couple of elseifs missing and the pm table grows and grows without need. If a,b,c,d are strings …BAM!.. another thing to improve …

You are welcome to polish it up …

@Briarfox - thank you, very useful.

@Briarfox - I’ve tested out your code, now, just a couple of minor suggestions

  • when dumping a table, and creating a table of names, convert them to string otherwise a table with mixed number and string keys (eg t[1]=5 t.fred=3) will throw an error
  • insert a
    before the final comment delimiter for neatness

I took out the " -" on each line, I prefer it without, but that’s just my preference.

I am writing this up as part of a blog post on debugging, creds to you =D>

@ignatz thanks for the kind words. However, I did not write the table dump. I’ve had it in my library but I forgot who it came from. I just adapted it to print to a tab.

I ended up removing the - as well looks neater without it. I’ll play with the dump function, I do not recall having an issue with combining strings and numbers. Thanks for pointing it out.

I think this will be a really cool utility to link in via a dependency when needed

Yeah, its part of a larger set I use when working on a project. I’m still adding to it and will share it when its done. I also have a command line thats on the FULLSCREEN that I use to change values/levels while the game is running.