Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Log package

edited March 2012 in Code Sharing Posts: 4

This is a small log package that can provide projects with log functionality. Intended use is for debugging.
Features: Automatically numbering and timestamp of logs, 4 severity levels: DEBUG, NORMAL, WARNING, ERROR, Turn all logging on/off with a single command, Direct output to console or screen.

Information on how to use the log class is provided within the code itself and in the below example.

Happy logging.

Example:

    l = Log("Main",l_TYPE_CONSOLE)
--    l = Log("Main",l_TYPE_SCREEN)
    l:setLoglevel(l_LVL_DEBUG)

    l:debug("Start")
    l:normal("Testing")
    l:warning("User not found")
    l:error("Syntax error")
    l:debug("Finished")

Output:

1: 18:53:39 DEBUG   Main Start 
2: 18:53:39 NORMAL  Main Testing 
3: 18:53:39 WARNING Main User not found 
4: 18:53:39 ERROR   Main Syntax error 
5: 18:53:39 DEBUG   Main Start 
6: 18:53:39 DEBUG   Main Finished

Log package code

-----------------------------
-- Global constants
-----------------------------
l_TYPE_CONSOLE = 1
l_TYPE_SCREEN  = 2
l_TYPE_FILE    = 3
l_LVL_DEBUG    = 4
l_LVL_NORMAL   = 3
l_LVL_WARNING  = 2
l_LVL_ERROR    = 1
l_LVL_OFF      = 0
l_LVL_DEFAULT  = l_LVL_NORMAL
l_TYPE_DEFAULT = l_TYPE_CONSOLE
l_FONTSIZE     = 12
l_COLOR        = color(150,145,145,255)
l_LOGLEVEL_LBL = {"ERROR  ","WARNING","NORMAL ","DEBUG  "}  
l_VERSION      = 0.9


 -----------------------------
-- Global variables
-----------------------------
l_logcnt   = 0
l_logTable = {}


-----------------------------
-- Class Log
-----------------------------

-- Class: Log(pName, pType)
-- Desc:
-- Member variables:
--     name                The name of this log, typically a class name.
--     type                Log output. 
--                         values: l_TYPE_CONSOLE, l_TYPE_SCREEN
--     loglevel            The highest loglevel that will be directed to the log output
--                         values: l_LVL_DEBUG, l_LVL_NORMAL, l_LVL_WARNING, 
--                                 l_LVL_ERROR, l_LVL_OFF
-- Member functions:
--    write(pLevel,pText)
--    draw()                Needed to direct output to screen.
--    debug(pText)          Writes a debug log
--    normal(pText)         Writes an error log
--    warning(pText)        Writes an warning log
--    error(pText)          Writes an error log
--    setLoglevel(pLevel)   Sets the log level. If ERROR level is set then only ERROR logs
--                          will be printed, if WARNING levvel is set then error and WARNING logs
--                          will be printed, and so foth. use OFF to disable logging.
--                          values: l_LVL_DEBUG, l_LVL_NORMAL, l_LVL_WARNING, 
--                                  l_LVL_ERROR, l_LVL_OFF

Log = class()

function Log:init(pName, pType, ploglevel)
    self.name = pName
    self.type = l_TYPE_DEFAULT
    self.loglevel = l_LVL_DEFAULT
        
    if pType ~= nil then      
        self.type = pType
    end
    if ploglevel ~= nil then      
        self.loglevel = ploglevel
    end
end

function Log:write(pLevel, pText)   
    if pLevel <= self.loglevel then
        -- Build log
        l_logcnt = l_logcnt + 1
        t = os.date()
        tStart = string.find(t,":")
        t = string.sub(t,tStart-2,tStart+5)
        logKey = l_logcnt
        logValue = ": "..t.." "..l_LOGLEVEL_LBL[pLevel].." "..self.name.." "..pText.." "
        
        if self.type == l_TYPE_CONSOLE then                          
            print(logKey..logValue)
        elseif self.type == l_TYPE_SCREEN then
            table.insert(l_logTable,logKey..logValue)
            if table.maxn(l_logTable) > 40 then
                table.remove(l_logTable,1)
            end
        end
    end
end

function Log:draw()
    f = font()
    fs = fontSize()
    tm = textMode()
    ta = textAlign()
    tww = textWrapWidth()
    fc = fill()
    font("Courier")
    fontSize(l_FONTSIZE)
    textMode(CORNER)
    textAlign(LEFT)
    textWrapWidth(0)
    fill(l_COLOR)
    for i=1,table.maxn(l_logTable) do
        text(l_logTable[i],10,(HEIGHT-l_FONTSIZE*i)-10)
    end
    font(f)
    fontSize(fs)
    textMode(tm)
    textAlign(ta)
    textWrapWidth(tww)
    fill(fc)
end

function Log:debug(pText)
    self:write(l_LVL_DEBUG,pText)
end

function Log:normal(pText)
    self:write(l_LVL_NORMAL,pText)
end

function Log:warning(pText)
    self:write(l_LVL_WARNING,pText)
end

function Log:error(pText)
    self:write(l_LVL_ERROR,pText)
end

function Log:setLoglevel(pLevel)
   self.loglevel = pLevel 
end
Tagged:

Comments

Sign In or Register to comment.