Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Logging in Lua/Gideros — Gideros Forum

Logging in Lua/Gideros

trostiktrostik Member
edited June 2012 in Code snippets
Hi guys,

Wanted to share a code snippet for logging your project. ( currently used/tested for items that have stage "ref" )

In main.lua add:
stage:addEventListener("logEvent",
function(event)
print(os.date(),"LOG:" , event["log"])
end
)

in any Sprite add:
function sendLog(message)
event = Event.new("logEvent")
event["log"] = message
stage:dispatchEvent(event)
end

and then use like this:
function someFunction()
sendLog("someFunction called")
end

The log will look something like :
Sat Jun 9 21:41:37 2012 LOG: soundButtonHandler
Sat Jun 9 21:41:37 2012 LOG: soundButtonHandler
Sat Jun 9 21:41:38 2012 LOG: howToHandler
Sat Jun 9 21:41:38 2012 LOG: howToClick
Sat Jun 9 21:41:38 2012 LOG: goToStage
Sat Jun 9 21:41:38 2012 LOG: goBackToStage
Sat Jun 9 21:41:39 2012 LOG: startButtonHandler
Sat Jun 9 21:41:39 2012 LOG: startButtonClick

Hope it helps.

Comments

  • Remake here :

    Create an object called logger:
    logger = Core.class()

    function logger:init(t)
    print("logger here")
    end

    function logger:log(message)
    print(os.date(),"LOG:" , message)
    end

    init in main
    logger = logger.new()

    and then call from everywhere:
    logger:log("goBackToStage")
  • atilimatilim Maintainer
    edited June 2012
    Thank you @trostik. Also one of the advantage of your logger class is that you can disable the logging before releasing your app.

    For who doesn't use a logger class, redefining an empty print function as:
    function print()
    end
    can also improve the performance of your app before releasing.
  • edited July 2012
    I use a slightly different approach so I can disabled logging per file and so I can keep most of the syntax used for calling print(args)
    -- <a href="http://www.lua.org/pil/5.2.html" rel="nofollow">http://www.lua.org/pil/5.2.html</a>
    function Log:log(enabled, ...)
    	if enabled == true then
    		print(unpack(arg))
    	end
    end
    Then I define a local variable in each class to enable logging in my calls to log()

    -- variable definition
    local logEnabled = true

    -- example call
    Log:log(logEnabled, 'Test', 'Message')

    Likes: ktc

    Wuji Touch Software
    http://wujitouch.com
    +1 -1 (+1 / -0 )Share on Facebook
  • ktcktc Member
    @ victor_wujitouch the unpack is the trick I have been looking for. Other implementations cannot print multiple arguments, unpack(arg) can.. Good job.
  • @ktc, using "arg" has been deprecated: http://www.luafaq.org/#T8.1.1

    In the example above you should be able to simply write "if enabled then print(...) end"
  • ktcktc Member
    @paulclinger thanks. Good to know.
Sign In or Register to comment.