]> git.jsancho.org Git - lugaru.git/blob - Source/logger/logger.h
Initial import.
[lugaru.git] / Source / logger / logger.h
1 // ---------------------------------------------------------------------------------------------------------------------------------
2 //  _                                 _     
3 // | |                               | |    
4 // | | ___   __ _  __ _  ___ _ __    | |__  
5 // | |/ _ \ / _` |/ _` |/ _ \ '__|   | '_ \ 
6 // | | (_) | (_| | (_| |  __/ |    _ | | | |
7 // |_|\___/ \__, |\__, |\___|_|   (_)|_| |_|
8 //           __/ | __/ |                    
9 //          |___/ |___/                     
10 //
11 // Generic informational logging class
12 //
13 // ---------------------------------------------------------------------------------------------------------------------------------
14 //
15 // Restrictions & freedoms pertaining to usage and redistribution of this software:
16 //
17 //  * This software is 100% free
18 //  * If you use this software (in part or in whole) you must credit the author.
19 //  * This software may not be re-distributed (in part or in whole) in a modified
20 //    form without clear documentation on how to obtain a copy of the original work.
21 //  * You may not use this software to directly or indirectly cause harm to others.
22 //  * This software is provided as-is and without warrantee. Use at your own risk.
23 //
24 // For more information, visit HTTP://www.FluidStudios.com
25 //
26 // ---------------------------------------------------------------------------------------------------------------------------------
27 // Originally created on 07/06/2000 by Paul Nettle
28 //
29 // Copyright 2000, Fluid Studios, Inc., all rights reserved.
30 // ---------------------------------------------------------------------------------------------------------------------------------
31
32 #ifndef _H_LOGGER
33 #define _H_LOGGER
34
35 // ---------------------------------------------------------------------------------------------------------------------------------
36 // The global logger
37 // ---------------------------------------------------------------------------------------------------------------------------------
38
39 class   Logger;
40 extern  Logger  logger;
41
42 // ---------------------------------------------------------------------------------------------------------------------------------
43 // Macros (necessary evil to take advantage of __LINE__ and __FILE__)
44 // ---------------------------------------------------------------------------------------------------------------------------------
45
46 #define LOG             logger.sourceLine() = __LINE__, logger.sourceFile() = __FILE__,logger.logTex
47 #define HEX             logger.sourceLine() = __LINE__, logger.sourceFile() = __FILE__,logger.logHex
48 #define RAW             logger.sourceLine() = __LINE__, logger.sourceFile() = __FILE__,logger.logRaw
49 #define INDENT          logger.sourceLine() = __LINE__, logger.sourceFile() = __FILE__,logger.indent
50 #define UNDENT          logger.sourceLine() = __LINE__, logger.sourceFile() = __FILE__,logger.undent
51 #define LOGBLOCK        logger.sourceLine() = __LINE__, logger.sourceFile() = __FILE__;LogBlock __lb__
52
53 // If you compiler supports __FUNCTION__, then replace the "#if 1" with "#if 0". Note that this will change the usage of the macro
54
55 #if 0
56 #define LOGFUNC         logger.sourceLine() = __LINE__, logger.sourceFile() = __FILE__;LogFlow __lf__
57 #else
58 #define LOGFUNC         logger.sourceLine() = __LINE__, logger.sourceFile() = __FILE__;LogFlow __lf__(__FUNCTION__)
59 #endif
60
61 // ---------------------------------------------------------------------------------------------------------------------------------
62 // The logger class: does the actual logging
63 // ---------------------------------------------------------------------------------------------------------------------------------
64
65 class   Logger
66 {
67 public:
68         // Enumerations
69
70         enum    LogFlags
71         {
72                                 LOG_INDENT = 0x00000001,
73                                 LOG_UNDENT = 0x00000002,
74                                 LOG_FLOW   = 0x00000004,
75                                 LOG_BLOK   = 0x00000008,
76                                 LOG_DATA   = 0x00000010,
77                                 LOG_INFO   = 0x00000012,
78                                 LOG_WARN   = 0x00000014,
79                                 LOG_ERR    = 0x00000018,
80                                 LOG_CRIT   = 0x00000020,
81                                 LOG_ALL    = 0xFFFFFFFF
82         };
83
84         // Construction/Destruction
85
86 inline                          Logger()
87                                 : _sourceLine(0), _indentCount(0), _indentChars(4), _fileSizeLimit(-1), _logMask(LOG_ALL),
88                                   _logStarted(false), _lineCharsFlag(false), _logFile("logger.log")
89                                 {
90                                 }
91
92 virtual                         ~Logger()
93                                 {
94                                         stop();
95                                 }
96
97         // Operators
98
99 inline          void            operator +=(const string &s)    {logTex(s);}
100
101         // Accessors
102
103 inline  const   bool            &lineCharsFlag() const  {return _lineCharsFlag;}
104 inline          bool            &lineCharsFlag()        {return _lineCharsFlag;}
105
106 inline  const   int             &fileSizeLimit() const  {return _fileSizeLimit;}
107 inline          int             &fileSizeLimit()        {return _fileSizeLimit;}
108
109 inline  const   unsigned int    &logMask() const        {return _logMask;}
110 inline          unsigned int    &logMask()              {return _logMask;}
111
112 inline  const   string          &logFile() const        {return _logFile;}
113 inline          string          &logFile()              {return _logFile;}
114
115 inline  const   unsigned int    &sourceLine() const     {return _sourceLine;}
116 inline          unsigned int    &sourceLine()           {return _sourceLine;}
117
118 inline  const   string          &sourceFile() const     {return _sourceFile;}
119 inline          string          &sourceFile()           {return _sourceFile;}
120
121 inline          bool            logStarted() const      {return _logStarted;}
122
123         // Utilitarian (public)
124
125 virtual         void            start(const bool reset);
126 virtual         void            stop();
127 virtual         void            logTex(const string &s, const LogFlags logBits = LOG_INFO);
128 virtual         void            logRaw(const string &s);
129 virtual         void            logHex(const char *buffer, const unsigned int count, const LogFlags logBits = LOG_INFO);
130 virtual         void            indent(const string &s, const LogFlags logBits = LOG_INDENT);
131 virtual         void            undent(const string &s, const LogFlags logBits = LOG_UNDENT);
132
133 private:
134         // Utilitarian (private)
135
136 virtual         void            limitFileSize() const;
137 virtual const   string          &headerString(const LogFlags logBits) const;
138
139         // Data
140
141                 string          _logFile;
142                 string          _sourceFile;
143                 unsigned int    _sourceLine;
144                 int             _indentCount;
145                 int             _indentChars;
146                 int             _fileSizeLimit;
147                 unsigned int    _logMask;
148                 bool            _logStarted;
149                 bool            _lineCharsFlag;
150 };
151
152 // ---------------------------------------------------------------------------------------------------------------------------------
153 // The LogBlock class: used for automatic indentation
154 // ---------------------------------------------------------------------------------------------------------------------------------
155
156 class   LogBlock
157 {
158 public:
159 inline                          LogBlock(const string &s)       {str = s;logger.indent("Begin block: " + str, Logger::LOG_INDENT);}
160 inline                          ~LogBlock()                     {logger.undent("", Logger::LOG_UNDENT);}
161 private:
162                 string          str;
163 };
164
165 // ---------------------------------------------------------------------------------------------------------------------------------
166 // The LogFlow class: used for logging code flow
167 // ---------------------------------------------------------------------------------------------------------------------------------
168
169 class   LogFlow
170 {
171 public:
172 inline                          LogFlow(const string &function) {str = function;logger.indent(str, Logger::LOG_FLOW);}
173 inline                          ~LogFlow()                      {logger.undent("", Logger::LOG_FLOW);}
174 private:
175                 string          str;
176 };
177
178 #endif // _H_LOGGER
179 // ---------------------------------------------------------------------------------------------------------------------------------
180 // logger.h - End of file
181 // ---------------------------------------------------------------------------------------------------------------------------------
182