working on buffered logging

Pawkette [03-01-14 - 16:23]
working on buffered logging
Filename
Console.lua
Console.txt
LogLine.lua
LogList.lua
diff --git a/Console.lua b/Console.lua
index 7517571..61a493f 100644
--- a/Console.lua
+++ b/Console.lua
@@ -1,6 +1,7 @@
 -------------------------------------------------
 -- Console - a debug log
 --
+-- @classmod Console
 -- @author Pawkette ( pawkette.heals@gmail.com )
 -- @copyright MIT
 -------------------------------------------------
@@ -16,15 +17,10 @@ LogLevels =
     DEBUG = 5 -- Log Everything
 }

----
--- @local
-local LogStrings =
+local DirtyFlags =
 {
-    '',
-    'INF',
-    'WRN',
-    'ERR',
-    'DBG'
+    NEW_LINES = 1,
+    FILTER_CHANGED = 2
 }

 ---
@@ -66,6 +62,9 @@ _G['debug']   = function( ... ) CONSOLE:LogDebug( ... )   end
 --
 Console = ZO_Object:Subclass()
 Console.log_level = LogLevels.DEBUG
+Console.log_lines = LogList:New()
+Console.dirty_flags = {}
+Console.last_index = 1

 function Console:New( ... )
     local result = ZO_Object.New( self )
@@ -94,11 +93,56 @@ function Console:Initialize( control )

     self.dropDown:SetSelectedItem( LevelToString( self.log_level ) )

+    self.control:SetHandler( 'OnUpdate', function() self:OnUpdate() end )
     self.closeBtn:SetHandler( 'OnClicked', function() self:Hide() end )
     self.clearBtn:SetHandler( 'OnClicked', function() self.textBuffer:Clear() end )
     self.textBuffer:SetHandler( 'OnMouseWheel', function( ... ) self:OnScroll( ... ) end )
 end

+function Console:IsDirty( flag )
+    if ( not flag ) then return #self.dirty_flags ~= 0 end
+
+    for k,v in pairs( self.dirty_flags ) do
+        if ( v == flag ) then
+            return true
+        end
+    end
+
+    return false
+end
+
+function Console:OnUpdate()
+    if ( not self:IsDirty() ) then
+        return
+    end
+
+    if ( self:IsDirty( DirtyFlags.FILTER_CHANGED ) ) then
+        self.textBuffer:Clear()
+        self.last_index = 1
+        self:AddNewLines()
+        self.last_index = self.log_lines:Last()
+    end
+
+    if ( self:IsDirty( DirtyFlags.NEW_LINES ) ) then
+        self:AddNewLines()
+        self.last_index = self.log_lines:Last()
+    end
+
+    self.dirty_flags = {}
+end
+
+function Console:AddNewLines()
+    local color = {}
+    local entry = {}
+    for i = self.last_index, self.log_lines:Last(), 1 do
+        entry = self.log_lines[ i ]
+        if ( self.log_level >= entry:GetTag() ) then
+            color = LogLevelColors[ entry:GetTag() ]
+            self.textBuffer:AddMessage( entry:GetFormatted(), color.r, color.g, color.b, nil )
+        end
+    end
+end
+
 --- Show this
 --
 function Console:Show()
@@ -132,19 +176,19 @@ end
 -- @tparam string fmt will convert to string if not a string
 -- @param ...
 function Console:Log( logLevel, fmt, ... )
-    if ( self.log_level < logLevel or self.log_level == LogLevels.NONE ) then
-        return
-    end
-
     if ( type( fmt ) ~= 'string' ) then
         fmt = tostring( fmt )
     end

-    local logLine = '[' .. GetTimeString() .. '][' .. LogStrings[ logLevel ] .. ']: ' .. fmt:format( ... )
+    if ( self.log_level >= logLevel ) then
+        table.insert( self.dirty_flags, DirtyFlags.NEW_LINES )
+    end

-    local color = LogLevelColors[ logLevel ]
+    if ( self.log_lines:Size() > 500 ) then
+        self.log_lines:Pop()
+    end

-    self.textBuffer:AddMessage( logLine, color.r, color.g, color.b, nil )
+    self.log_lines:Push( LogLine:New( logLevel, GetTimeString(), fmt:format( ... ) ) )
 end

 --- Log something under info channel
@@ -179,6 +223,7 @@ end
 -- @tparam LogLevels level
 function Console:SetLogLevel( level )
     self.log_level = level
+    table.insert( self.dirty_flags, DirtyFlags.FILTER_CHANGED )
 end

 --- Initialize the console in global space
diff --git a/Console.txt b/Console.txt
index f22d081..7984973 100644
--- a/Console.txt
+++ b/Console.txt
@@ -1,5 +1,7 @@
 ## Title: Console - By Pawkette
 ## APIVersion: 100000

+LogList.lua
+LogLine.lua
 Console.lua
 Console.xml
\ No newline at end of file
diff --git a/LogLine.lua b/LogLine.lua
new file mode 100644
index 0000000..6e1f330
--- /dev/null
+++ b/LogLine.lua
@@ -0,0 +1,57 @@
+-------------------------------------------------
+-- Console - a debug log
+--
+-- @classmod LogLine
+-- @author Pawkette ( pawkette.heals@gmail.com )
+-- @copyright MIT
+-------------------------------------------------
+
+LogLine =
+{
+    text    = '', --string
+    tag     = 0, -- log category
+    stamp   = '' -- timestamp
+}
+
+---
+-- @local
+local LogStrings =
+{
+    '',
+    'INF',
+    'WRN',
+    'ERR',
+    'DBG'
+}
+
+function LogLine:New( tag, stamp, text )
+    local self = {}
+    setmetatable( self, { __index = LogLine } )
+
+    self:Initialize( tag, stamp, text )
+    return self
+end
+
+function LogLine:Initialize( tag, stamp, text )
+    self.tag    = tag
+    self.text   = text
+    self.stamp  = stamp
+end
+
+function LogLine:GetTag()
+    return self.tag
+end
+
+function LogLine:GetText()
+
+    return self.text
+end
+
+function LogLine:GetTimestamp()
+    return self.stamp
+end
+
+function LogLine:GetFormatted()
+    local result = '[' .. self:GetTimestamp() .. '][' .. LogStrings[ self:GetTag() ] .. ']: ' .. self:GetText()
+    return result
+end
\ No newline at end of file
diff --git a/LogList.lua b/LogList.lua
new file mode 100644
index 0000000..17a5b5e
--- /dev/null
+++ b/LogList.lua
@@ -0,0 +1,53 @@
+-------------------------------------------------
+-- Console - a debug log
+--
+-- @classmod LogList
+-- @author Pawkette ( pawkette.heals@gmail.com )
+-- @copyright MIT
+-------------------------------------------------
+
+LogList =
+{
+    first = 0,
+    last = -1,
+}
+
+function LogList:New( ... )
+    local self = {}
+    setmetatable( self, { __index = LogList } )
+
+    self:Initialize()
+    return self
+end
+
+function LogList:Initialize()
+
+end
+
+function LogList:Push( value )
+    self.last = self.last + 1
+    self[ self.last ] = value
+end
+
+function LogList:Pop()
+    if ( self.first > self.last ) then
+        return nil
+    end
+
+    local value = self[ self.first ]
+    self[ self.first ] = nil
+
+    self.first = self.first + 1
+end
+
+function LogList:Size()
+    return self.last - self.first
+end
+
+function LogList:First()
+    return self.first
+end
+
+function LogList:Last()
+    return self.last
+end
\ No newline at end of file