A Better Print Statement For Logging!

thanks jose95. Alan, heres that quick workaround for iOS builds breaking on that fp system.PathForFile returning nil.  Means your console print is not overiden but at least it doesnt break builds. Sorry its only a workaround.

for those of you just picking up on this print override. just name this a .lua file and require it in your main before your print statements start for it to work 

[lua]

– -- debug print overide

– -- you can use _print() in code if you want original

_print = print

local localdir

local projectCodeRootString 

function string.join( … )

    – function requires a string, a separator, and at least one argument;

    – if this condition is not met, print and error and return nil

    if ( #arg < 3 ) then

        _print( “Error: required string and separator missing” )

        return nil

    end

    – the string is the first argument

    local str = arg[1]

    – the separator is the second argument

    local separator = arg[2]

    – loop from the third index (the first real argument to join to the string)

    for i = 3, #arg do

        – if the argument is a string, append it along with the separator

        if ( type(arg[i]) == “string” ) then

            if ( str:len() ~= 0 ) then

                str = str … separator

            end

            str = str … arg[i]

        – if the argument is a table, loop through it just like above

        elseif ( type(arg[i]) == “table” ) then

            local t = arg[i]

            for j = 1, #t do

                if ( type( t[j] ) == “string” ) then

                    if ( str:len() ~= 0 ) then

                        str = str … separator

                    end

                    str = str … t[j]

                end

            end

        end

    end

    return str

end

function initialise_print( … )

    print = function(…)

        

        if arg == nil then 

            return

        end

        local function cleanText(text)

           if text == nil then

              return “nil”

           end

           return text

        end

        – local fp = system.pathForFile( “main.lua”, system.ResourceDirectory )

        – local localdir = fp:gsub( “main.lua”, “” )

        – local projectCodeRootString = localdir

         

        – local projectCodeRootString = ‘/LeapFrog/code/’

        local source_file = cleanText(debug.getinfo(2).source)

        local path_start, path_end = source_file:find(projectCodeRootString)

         

        if path_start == nil then

            return 

        end

        local debug_path = cleanText(source_file:sub(path_end, source_file:len()))

        – This gets the line number:

        local line = cleanText(debug.getinfo(2).currentline)

         

        – This gets the function name:

        local funcName = cleanText(debug.getinfo(2, “n”).name)

            

        local str = string.join("", " ", arg)

        _print(“°”…debug_path…":"…funcName…"()"…":"…line…": "…str) – using the ° degree symbol as easy file_regex target

        – _print(debug_path…":"…funcName…"()"…":"…line…": "…str)

    end

end

– this is done so that the ios build doesnt break

local fp = system.pathForFile( “main.lua”, system.ResourceDirectory )

if fp ~= nil then

    localdir = fp:gsub( “main.lua”, “” )

    projectCodeRootString = localdir

    _print("Project Location: "…“°°”…localdir…“°°”) – this is to create a nice easy tag

    initialise_print()

else

    print = _print – cancel overriding

    print( “ios error: fp: is nil. customm print overide cancelled” )

end

[/lua]