root/lu/LULog.lua

Revision 814 (checked in by rsz, 3 years ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               LULog.lua
3 -- Description:         Like a square peg in a round hole
4 -- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5 -- Creation Date:       February 1, 2005
6 -- Legal:               Copyright (C) 2005 Raphaël Szwarc
7 --------------------------------------------------------------------------------
8
9 -- import dependencies
10 local LUObject = require( "LUObject" )
11 local LUString = require( "LUString" )
12 local debug = require( "debug" )
13 local io = require( "io" )
14 local os = require( "os" )
15 local table = require( "table" )
16
17 -- define the class
18 local super = LUObject
19 local self = super()
20
21 -- constant(s)
22 self.Trace = 1
23 self.Debug = 2
24 self.Info = 3
25 self.Warning = 4
26 self.Error = 5
27
28 self.All = 0
29 self.None = 10
30
31 self._verbosity = self.Debug
32
33 local Descriptions = { [ self.Trace ] = "(Trace)", [ self.Debug ] = "(Debug)", [ self.Info ] = "(Info)", [ self.Warning ] = "(Warning)", [ self.Error ] = "(ERROR)" }
34
35 -- private method to Log a message
36 local function Log( aLevel, ... )
37         local someInfo = debug.getinfo( 3, "Sn" )
38         local aClass = someInfo.source
39         local aMethod = someInfo.name
40         local aBuffer = {}
41
42         for aComponent in aClass:gmatch( "([^?/?\]+)" ) do
43                 aClass = aComponent
44         end
45        
46         aClass = aClass:sub( 1, aClass:find( "%." ) - 1 )
47         aClass = aClass:gsub( "@", "" )
48                                
49         aBuffer[ #aBuffer + 1 ] = os.date( "%m/%d %H:%M:%S", os.time() )
50         aBuffer[ #aBuffer + 1 ] = Descriptions[ aLevel ]
51        
52         if aClass ~= nil and aMethod ~= nil then
53                 aMethod = aClass .. "." .. aMethod
54                 aMethod = aMethod .. ":"
55                 aBuffer[ #aBuffer + 1 ] = aMethod
56         elseif aClass ~= nil then
57                 aBuffer[ #aBuffer + 1 ] = aClass .. ":"
58         elseif aMethod ~= nil then
59                 aBuffer[ #aBuffer + 1 ] = aMethod .. ":"
60         end
61
62         do
63                 local aCount = select( "#", ... )
64                
65                 for anIndex = 1, aCount do
66                         local aValue = select( anIndex, ... )
67                
68                         aBuffer[ #aBuffer + 1 ] = LUString:toString( aValue )
69                         aBuffer[ #aBuffer + 1 ] = "\t"
70                 end
71
72                 aBuffer[ #aBuffer ] = nil
73         end
74
75         aBuffer[ #aBuffer + 1 ] = "\n"
76        
77         if aLevel < self.Warning then
78                 io.stdout:write( table.concat( aBuffer, " " ) )
79         else
80                 io.stderr:write( table.concat( aBuffer, " " ) )
81         end
82 end
83
84 -- method to Log a trace message
85 function self:trace( ... )
86         if self.Trace >= self._verbosity then
87                 Log( self.Trace, ... )
88         end
89        
90         return self
91 end
92
93 -- method to Log a debug message
94 function self:debug( ... )
95         if self.Debug >= self._verbosity then
96                 return Log( self.Debug, ... )
97         end
98        
99         return self
100 end
101
102 -- method to Log an info message
103 function self:info( ... )
104         if self.Info >= self._verbosity then
105                 return Log( self.Info, ... )
106         end
107        
108         return self
109 end
110
111 -- method to Log a warning message
112 function self:warning( ... )
113         if self.Warning >= self._verbosity then
114                 return Log( self.Warning, ... )
115         end
116        
117         return self
118 end
119
120 -- method to Log an error message
121 function self:error( ... )
122         if self.Error >= self._verbosity then
123                 return Log( self.Error, ... )
124         end
125        
126         return self
127 end
128
129 function self:verbosity()
130         return self._verbosity
131 end
132
133 function self:setVerbosity( aValue )
134         self._verbosity = aValue
135 end
136
137 return self
Note: See TracBrowser for help on using the browser.