Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
LogFunctions.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 
14 #include "LogFunctions.h"
15 
16 #include "FileFunctions.h"
17 #include "StringFunctions.h"
18 #include "CoreMacros.h"
19 
20 #include <map>
21 #include <list>
22 #include <utility>
23 
24 
26 int g_logLevel = LOG_LEVEL_WARN;
27 
29 void (*g_outputLogFun)( const char*, int ) = NULL;
30 
31 
33 int g_errorCode = 0;
34 
36 int g_logListSize = 0;
37 
39 std::string g_errorMessage;
40 
42 std::string g_tmperrorMessage;
43 
45 std::list< std::pair< int, std::string > > g_logList;
46 
48 static const char* LOG_HEADERS[7] = {
49  "",
50  "[Fatal] :",
51  "[Error] :",
52  "[Warn] :",
53  "[Info] :",
54  "[Debug] :",
55  "[Trace] :"
56 };
57 
58 
65 void setErrorCode( int code, const char* msg ) {
66  g_errorCode = code;
67  g_errorMessage = std::string( msg );
68 }
69 
70 // set log level
71 void setloglevel( int level ) {
72  // check parameter
73  if( level < MIN_LOG_LEVEL ) {
74  level = MIN_LOG_LEVEL;
75  }
76  if( level > MAX_LOG_LEVEL ) {
77  level = MAX_LOG_LEVEL;
78  }
79 
80  // set parameter
81  g_logLevel = level;
82 }
83 
84 // get log level
85 int getloglevel() {
86  return g_logLevel;
87 }
88 
89 // get last error code
90 int getlasterror() {
91  return g_errorCode;
92 }
93 
94 // get last error message
95 const char* getlasterrormessage() {
96 
98 
99  // Delete the error message after it has been used.
100  seterrorcode(-1, NULL);
101 
102  return g_tmperrorMessage.c_str();
103 }
104 
105 // output log
106 void outputlog( const char* msg, int level, const char* file, int line ) {
107  // check log level and message
108  level = CLAMP( level, MIN_LOG_LEVEL, MAX_LOG_LEVEL );
109  if( g_logLevel < level ) {
110  return;
111  }
112 
113  // create message
114  std::string s = std::string( LOG_HEADERS[ level ] );
115  if( file != NULL ) {
116  s += " ";
117  s += getfilename( file );
118  }
119  if( line >= 0 ) {
120  s += strfmt( "(%d)", line );
121  }
122  if( msg != NULL ) {
123  if( file == NULL && line < 0 ) {
124  s += strfmt( "%s", msg );
125  }
126  else {
127  s += strfmt( " - %s ", msg );
128  }
129  }
130 
131  // output log
132  if( g_outputLogFun != NULL ) {
133  (*g_outputLogFun)( s.c_str(), level );
134  }
135 
136  // add to list
137  if( g_logListSize != 0 ) {
138  std::pair< int, std::string > logInfo = std::make_pair( level, s );
139  g_logList.push_back( logInfo );
140 
142  }
143 }
144 
145 // set error code
146 void seterrorcode( int code, const char* msg ) {
147  g_errorCode = code;
148 
149  if( msg == NULL ) {
150  g_errorMessage.clear();
151  }
152  else {
153  g_errorMessage = std::string( msg );
154  }
155 }
156 
157 // output fatal log
158 void logfatal( const char* msg, const char* file, int line ) {
159  outputlog( msg, LOG_LEVEL_FATAL, file, line );
160 }
161 
162 void logfatalcode( int code, const char* msg, const char* file, int line ) {
163  seterrorcode( code, msg );
164  logfatal( msg, file, line );
165 }
166 
167 // output error log
168 void logerror( const char* msg, const char* file, int line ) {
169  outputlog( msg, LOG_LEVEL_ERROR, file, line );
170 }
171 
172 void logerrorcode( int code, const char* msg, const char* file, int line ) {
173  seterrorcode( code, msg );
174  logerror( msg, file, line );
175 }
176 
177 // output warning log
178 void logwarn( const char* msg, const char* file, int line ) {
179  outputlog( msg, LOG_LEVEL_WARN, file, line );
180 }
181 
182 void logwarncode( int code, const char* msg, const char* file, int line ) {
183  seterrorcode( code, msg );
184  logwarn( msg, file, line );
185 }
186 
187 // output information log
188 void loginfo( const char* msg, const char* file, int line ) {
189  outputlog( msg, LOG_LEVEL_INFO, file, line );
190 }
191 
192 void loginfocode( int code, const char* msg, const char* file, int line ) {
193  seterrorcode( code, msg );
194  loginfo( msg, file, line );
195 }
196 
197 // output debug log
198 void logdebug( const char* msg, const char* file, int line ) {
199  outputlog( msg, LOG_LEVEL_DEBUG, file, line );
200 }
201 
202 void logdebugcode( int code, const char* msg, const char* file, int line ) {
203  seterrorcode( code, msg );
204  logdebug( msg, file, line );
205 }
206 
207 // output trace log
208 void logtrace( const char* msg, const char* file, int line ) {
209  outputlog( msg, LOG_LEVEL_TRACE, file, line );
210 }
211 
212 void logtracecode( int code, const char* msg, const char* file, int line ) {
213  seterrorcode( code, msg );
214  logtrace( msg, file, line );
215 }
216 
217 // set log list size
218 void setloglistsize( const int size ) {
219  // set to variable
220  g_logListSize = MAX( size, -1 );
221 
222  // check size
223  if( size > 0 ) {
224  while( (int)g_logList.size() > size ) {
225  g_logList.pop_front();
226  }
227  }
228 }
229 
230 // get log list size
232  return g_logListSize;
233 }
234 
235 // flush log
236 void logflush() {
237  // output
238  for( std::list< std::pair< int, std::string > >::iterator it = g_logList.begin();
239  it != g_logList.end(); it++ ) {
240  g_outputLogFun( (*it).second.c_str(), (*it).first );
241  }
242 
243  // clear list
244  g_logList.clear();
245 }
246 
247 // set output log function
248 void setoutputlogfunction( void (*outputLogFun)( const char*, int ) ) {
249  g_outputLogFun = outputLogFun;
250 }
std::string getfilename(const char *path)
get file name
int g_logListSize
std::string strfmt(const char *fmt,...)
get formated string
void outputlog(const char *msg, int level, const char *file, int line)
output log
std::string g_errorMessage
void logdebugcode(int code, const char *msg, const char *file, int line)
output debug log and set error code
void logwarncode(int code, const char *msg, const char *file, int line)
output warning log and set error code
interfaces of file system function.
void logfatal(const char *msg, const char *file, int line)
output fatal log
void logwarn(const char *msg, const char *file, int line)
output warning log
void setoutputlogfunction(void(*outputLogFun)(const char *, int))
set output log function
basic macro collection
void logtracecode(int code, const char *msg, const char *file, int line)
output trace log and set error code
void logdebug(const char *msg, const char *file, int line)
output debug log
void logerrorcode(int code, const char *msg, const char *file, int line)
output error log and set error code
void(* g_outputLogFun)(const char *, int)
void logflush()
flushes logs to be stored in the list
int g_errorCode
#define NULL
Definition: CoreMacros.h:18
void logerror(const char *msg, const char *file, int line)
output error log
void setErrorCode(int code, const char *msg)
set error code and message
void loginfocode(int code, const char *msg, const char *file, int line)
output information log and set error code
#define MAX(x, y)
Definition: CoreMacros.h:27
void seterrorcode(int code, const char *msg)
set error code
std::string g_tmperrorMessage
#define CLAMP(x, low, high)
Definition: CoreMacros.h:35
const char * getlasterrormessage()
get last error message
void logfatalcode(int code, const char *msg, const char *file, int line)
output fatal log and set error code
std::list< std::pair< int, std::string > > g_logList
interfaces of log function
interfaces of string function
void setloglevel(int level)
set log level
int g_logLevel
void logtrace(const char *msg, const char *file, int line)
output trace log
void loginfo(const char *msg, const char *file, int line)
output information log
int getloglistsize()
gets log list size
int getloglevel()
get log level
void setloglistsize(const int size)
sets log list size
int getlasterror()
get last error code