EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LogNew.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Captnoord
24 */
25 
26 #include "eve-core.h"
27 
28 #include "log/LogNew.h"
29 #include "log/logtypes.h"
30 #include "log/logsys.h"
31 
32 /*************************************************************************/
33 /* NewLog */
34 /*************************************************************************/
35 const char* const NewLog::COLOR_TABLE[ COLOR_COUNT ] =
36 {
37  "\033[" "00" "m", // COLOR_DEFAULT
38  "\033[" "30;22" "m", // COLOR_BLACK
39  "\033[" "31;22" "m", // COLOR_RED
40  "\033[" "32;22" "m", // COLOR_GREEN
41  "\033[" "33;01" "m", // COLOR_YELLOW
42  "\033[" "34;01" "m", // COLOR_BLUE
43  "\033[" "35;22" "m", // COLOR_MAGENTA
44  "\033[" "36;01" "m", // COLOR_CYAN
45  "\033[" "37;01" "m" // COLOR_WHITE
46 };
47 
49 : mLogfile( NULL ),
50  mTime( 0 ),
51  m_initialized(false)
52 {
53  // open default logfile
54  std::string logPath = EVEMU_ROOT "/logs/";
55  SetLogfileDefault(logPath);
56 
57  m_initialized = true;
58 }
59 
60 NewLog::NewLog(std::string logPath)
61 : mLogfile( NULL ),
62 mTime( 0 )
63 {
64  // open default logfile
65  if( logPath.empty() )
66  logPath = EVEMU_ROOT "/logs/";
67 
68  SetLogfileDefault(logPath);
69 
70  m_initialized = true;
71 }
72 
73 
75 {
76  Debug( "Log", "Log system shutting down" );
77 
78  // close logfile
79  SetLogfile( (FILE*)NULL );
80 }
81 
83 {
84  Blue(" Log System", "Log System Initialized.");
85 }
86 
87 
88 void NewLog::InitializeLogging( std::string logPath )
89 {
90  // open default logfile
91  if( logPath.empty() )
92  logPath = EVEMU_ROOT "/logs/";
93 
94  m_initialized = true;
95 
96  SetLogfileDefault(logPath);
97 }
98 
99 void NewLog::Log( const char* source, const char* fmt, ... )
100 {
101  va_list ap;
102  va_start( ap, fmt );
103 
104  PrintMsg( COLOR_DEFAULT, 'L', source, fmt, ap );
105 
106  va_end( ap );
107 }
108 
109 void NewLog::White( const char* source, const char* fmt, ... )
110 {
111  if (!is_log_enabled(DEBUG__DEV_LOG))
112  return;
113  va_list ap;
114  va_start( ap, fmt );
115 
116  PrintMsg( COLOR_WHITE, 'W', source, fmt, ap );
117 
118  va_end( ap );
119 }
120 
121 void NewLog::Error( const char* source, const char* fmt, ... )
122 {
123  va_list ap;
124  va_start( ap, fmt );
125 
126  PrintMsg( COLOR_RED, 'E', source, fmt, ap );
127 
128  va_end( ap );
129 }
130 
131 void NewLog::Warning( const char* source, const char* fmt, ... )
132 {
133  va_list ap;
134  va_start( ap, fmt );
135 
136  PrintMsg( COLOR_YELLOW, 'W', source, fmt, ap );
137 
138  va_end( ap );
139 }
140 
141 void NewLog::Cyan( const char* source, const char* fmt, ... )
142 {
143  va_list ap;
144  va_start( ap, fmt );
145 
146  PrintMsg( COLOR_CYAN, 'C', source, fmt, ap );
147 
148  va_end( ap );
149 }
150 
151 void NewLog::Green( const char* source, const char* fmt, ... )
152 {
153  va_list ap;
154  va_start( ap, fmt );
155 
156  PrintMsg( COLOR_GREEN, 'G', source, fmt, ap );
157 
158  va_end( ap );
159 }
160 
161 void NewLog::Blue( const char* source, const char* fmt, ... )
162 {
163  va_list ap;
164  va_start( ap, fmt );
165 
166  PrintMsg( COLOR_BLUE, 'B', source, fmt, ap );
167 
168  va_end( ap );
169 }
170 
171 void NewLog::Magenta( const char* source, const char* fmt, ... )
172 {
173  va_list ap;
174  va_start( ap, fmt );
175 
176  PrintMsg( COLOR_MAGENTA, 'M', source, fmt, ap );
177 
178  va_end( ap );
179 }
180 
181 void NewLog::Yellow( const char* source, const char* fmt, ... )
182 {
183  va_list ap;
184  va_start( ap, fmt );
185 
186  PrintMsg( COLOR_YELLOW, 'Y', source, fmt, ap );
187 
188  va_end( ap );
189 }
190 
191 void NewLog::Debug( const char* source, const char* fmt, ... )
192 {
193  if (!is_log_enabled(DEBUG__DEBUG))
194  return;
195  va_list ap;
196  va_start( ap, fmt );
197 
198  PrintMsg( COLOR_WHITE, 'D', source, fmt, ap );
199 
200  va_end( ap );
201 }
202 
203 bool NewLog::SetLogfile( const char* filename )
204 {
205  MutexLock l( mMutex );
206 
207  FILE* file = NULL;
208 
209  if( NULL != filename )
210  {
211  file = fopen( filename, "w" );
212  if( NULL == file )
213  return false;
214  }
215 
216  return SetLogfile( file );
217 }
218 
219 bool NewLog::SetLogfile( FILE* file )
220 {
221  MutexLock l( mMutex );
222 
223  if( NULL != mLogfile )
224  assert( 0 == fclose( mLogfile ) );
225 
226  mLogfile = file;
227  return true;
228 }
229 
230 void NewLog::PrintMsg( Color color, char pfx, const char* source, const char* fmt, va_list ap )
231 {
232  if( !m_initialized )
233  return;
234 
235  MutexLock l( mMutex );
236 
237  PrintTime();
238 
239  SetColor( color );
240  Print( " %c ", pfx );
241 
242  if( source && *source )
243  {
245  Print( "%s: ", source );
246 
247  SetColor( color );
248  }
249 
250  PrintVa( fmt, ap );
251  Print( "\n" );
252 
254 }
255 
257 {
258  MutexLock l( mMutex );
259 
260  // this will be replaced my a timing thread somehow
261  SetTime( time( NULL ) );
262 
263  tm t;
264  localtime_r( &mTime, &t );
265 
266  Print( "%02u:%02u:%02u", t.tm_hour, t.tm_min, t.tm_sec );
267 }
268 
269 void NewLog::Print( const char* fmt, ... )
270 {
271  va_list ap;
272  va_start( ap, fmt );
273 
274  PrintVa( fmt, ap );
275 
276  va_end( ap );
277 }
278 
279 void NewLog::PrintVa( const char* fmt, va_list ap )
280 {
281  MutexLock l( mMutex );
282 
283  if( NULL != mLogfile )
284  {
285  // this is a design flaw ( UNIX related )
286  va_list ap2;
287  va_copy( ap2, ap );
288 
289  vfprintf( mLogfile, fmt, ap2 );
290 
291 #ifndef NDEBUG
292  // flush immediately so logfile is accurate if we crash
293  fflush( mLogfile );
294 #endif /* !NDEBUG */
295 
296  va_end( ap2 );
297  }
298 
299  vprintf( fmt, ap );
300 }
301 
302 void NewLog::SetColor( Color color )
303 {
304  assert( 0 <= color && color < COLOR_COUNT );
305 
306  MutexLock l( mMutex );
307 
308 #ifdef HAVE_WINDOWS_H
309  ::SetConsoleTextAttribute( mStdOutHandle, COLOR_TABLE[ color ] );
310 #else /* !HAVE_WINDOWS_H */
311  ::fputs( COLOR_TABLE[ color ], stdout );
312 #endif /* !HAVE_WINDOWS_H */
313 }
314 
315 void NewLog::SetLogfileDefault(std::string logPath)
316 {
317  MutexLock l( mMutex );
318 
319  // set initial log system time
320  SetTime( time( NULL ) );
321 
322  tm t;
323  localtime_r( &mTime, &t );
324 
325  // open default logfile
326  char filename[ FILENAME_MAX + 1 ];
327  std::string logFile = logPath + "%02u-%02u-%04u-%02u-%02u.log";
328  snprintf( filename, FILENAME_MAX + 1, logFile.c_str(),
329  t.tm_mday, t.tm_mon + 1, t.tm_year + 1900, t.tm_hour, t.tm_min );
330  //snprintf( filename, FILENAME_MAX + 1, EVEMU_ROOT "/logs/%02u-%02u-%04u-%02u-%02u.log",
331  // t.tm_mday, t.tm_mon + 1, t.tm_year + 1900, t.tm_hour, t.tm_min );
332 
333  // this isnt accurate as log system is not initalized yet. all Print calls will fail
334  if (!SetLogfile(filename))
335  Warning( "Log", "Unable to open logfile '%s': %s", filename, strerror( errno ) );
336 }
void PrintTime()
Prints current time.
Definition: LogNew.cpp:256
void SetColor(Color color)
Sets the color of the output text.
Definition: LogNew.cpp:302
Magenta color.
Definition: LogNew.h:170
tm * localtime_r(const time_t *timep, tm *result)
Definition: eve-compat.cpp:184
Red color.
Definition: LogNew.h:166
void Cyan(const char *source, const char *fmt,...)
Logs a message to console in cyan.
Definition: LogNew.cpp:141
A default color.
Definition: LogNew.h:164
A lock for a Lockable object.
Definition: Lock.h:70
void Blue(const char *source, const char *fmt,...)
Logs a message to console in blue.
Definition: LogNew.cpp:161
void Debug(const char *source, const char *fmt,...)
Logs a debug message to file and console.
Definition: LogNew.cpp:191
void Yellow(const char *source, const char *fmt,...)
Logs a message to console in yellow.
Definition: LogNew.cpp:181
#define va_copy(a, b)
Definition: eve-compat.h:177
Mutex mMutex
Protection against concurrent log messages.
Definition: LogNew.h:231
FILE * mLogfile
The active logfile.
Definition: LogNew.h:227
Green color.
Definition: LogNew.h:167
void Magenta(const char *source, const char *fmt,...)
Logs a message to console in magenta.
Definition: LogNew.cpp:171
void SetLogfileDefault(std::string logPath)
Sets the default logfile.
Definition: LogNew.cpp:315
Yellow color.
Definition: LogNew.h:168
void Log(const char *source, const char *fmt,...)
Logs a message to file.
Definition: LogNew.cpp:99
static const char *const COLOR_TABLE[COLOR_COUNT]
Color translation table.
Definition: LogNew.h:245
Cyan color.
Definition: LogNew.h:171
#define is_log_enabled(type)
Definition: logsys.h:78
Number of colors.
Definition: LogNew.h:174
time_t mTime
Current timestamp.
Definition: LogNew.h:229
bool m_initialized
Definition: LogNew.h:233
#define snprintf
Definition: eve-compat.h:184
bool SetLogfile(const char *filename)
Sets the logfile to be used.
Definition: LogNew.cpp:203
void SetTime(time_t time)
Sets the log system time every main loop.
Definition: LogNew.h:158
void Warning(const char *source, const char *fmt,...)
Logs a warning message to file.
Definition: LogNew.cpp:131
Color
A convenience color enum.
Definition: LogNew.h:162
Blue color.
Definition: LogNew.h:169
void InitializeLogging(std::string logPath)
Initializes and sets the Log file path.
Definition: LogNew.cpp:88
NewLog()
Primary constructor, initializes logging.
Definition: LogNew.cpp:48
~NewLog()
Destructor, closes the logfile.
Definition: LogNew.cpp:74
White color.
Definition: LogNew.h:172
void Green(const char *source, const char *fmt,...)
Logs a message to console in green.
Definition: LogNew.cpp:151
void PrintVa(const char *fmt, va_list ap)
Prints a raw message.
Definition: LogNew.cpp:279
void White(const char *source, const char *fmt,...)
Logs a message to console in white.
Definition: LogNew.cpp:109
void PrintMsg(Color color, char pfx, const char *source, const char *fmt, va_list ap)
Prints a message.
Definition: LogNew.cpp:230
void Error(const char *source, const char *fmt,...)
Logs error message to console and file.
Definition: LogNew.cpp:121
void Initialize()
Definition: LogNew.cpp:82
void Print(const char *fmt,...)
Prints a raw message.
Definition: LogNew.cpp:269