EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PyTraceLog.h
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: Zhur, Captnoord
24 */
25 
26 #ifndef PY_TRACE_LOG_H
27 #define PY_TRACE_LOG_H
28 
29 #include "PyRep.h"
30 
31 /* ascents cross platform color thingy's */
32 #ifdef HAVE_WINDOWS_H
33 # define TRED FOREGROUND_RED | FOREGROUND_INTENSITY
34 # define TGREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
35 # define TYELLOW FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
36 # define TNORMAL FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE
37 # define TWHITE TNORMAL | FOREGROUND_INTENSITY
38 # define TBLUE FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
39 #else /* !HAVE_WINDOWS_H */
40 # define TRED 1
41 # define TGREEN 2
42 # define TYELLOW 3
43 # define TNORMAL 4
44 # define TWHITE 5
45 # define TBLUE 6
46 #endif /* !HAVE_WINDOWS_H */
47 
48 /* ascents UNIX color thingy's */
49 #ifndef HAVE_WINDOWS_H
50 static const char* colorstrings[TBLUE+1] = {
51  "",
52  "\033[22;31m",
53  "\033[22;32m",
54  "\033[01;33m",
55  //"\033[22;37m",
56  "\033[0m",
57  "\033[01;37m",
58  "\033[22;34m",
59 };
60 #endif /* !HAVE_WINDOWS_H */
61 
74 class PyTraceLog {
75 public:
85  PyTraceLog(const char *path, bool toConsole = false, bool toFile = false)
86  : mFout(NULL),
87  mInitialized(false),
88  mLogToConsole(toConsole),
89  mLogToFile(toFile)
90 #ifdef HAVE_WINDOWS_H
91  ,mStderrHandle(NULL),
92  mStdoutHandle(NULL)
93 #endif /* HAVE_WINDOWS_H */
94  {
95  if (toFile == true)
96  {
97  mFout = fopen(path, "w+");
98  if (mFout == NULL)
99  printf("[error]PyTraceLog: sorry initializing the output file failed\n");
100  }
101 
102 #ifdef HAVE_WINDOWS_H
103  // get error handle
104  if (mLogToConsole == true)
105  {
106  mStderrHandle = GetStdHandle(STD_ERROR_HANDLE);
107  mStdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
108  }
109 #endif /* HAVE_WINDOWS_H */
110  mInitialized = true;
111  }
112 
117  {
118  if (mLogToFile == true && mFout != NULL)
119  {
120  fflush(mFout);
121  fclose(mFout);
122  }
123  }
124 
133  bool logTrace(PyTuple &tuple)
134  {
135  assert(mInitialized && "PyTraceLog isn't initialized");
136 
137  // base object should be a tuple
138  if(tuple.IsTuple() == false)
139  {
140  _logInternMessage("trace error because the base object isn't a tuple");
141  return false;
142  }
143 
144  /* the next part is commented out because the payload of the error message contains similar data */
145 
146  // the first object should be a tuple
147  /*if (tuple[0]->IsTuple() == false)
148  {
149  _logInternMessage("trace error because the first object within the tuple isn't a tuple");
150  return false;
151  }
152 
153  if (mLogToFile)
154  fprintf(mFout, "\nStackTraceAlert:\n");
155 
156  // tuple 0 should contain the error message or something binary
157  PyRepTuple& tuple0_0 = tuple[0]->AsTuple();
158  //tuple0_0[0] some integer... which I don't care about
159  if( tuple0_0[1]->IsString() == true )
160  {
161  _logInternStringMessage(tuple0_0[1]);
162  }
163  else if (tuple0_0[1]->IsBuffer() == true)
164  {
165  _logInternBufferMessage(tuple0_0[1]);
166  }
167  else
168  {
169  _logInternMessage("trace log error");
170  return false;
171  }
172 
173  if (mLogToFile == true)
174  {
175  fputc('\n', mFout);
176  fflush(mFout);
177  }*/
178 
179  /* python stack trace payload */
180  //if (tuple.GetItem(1)->IsString() == true)
181  {
183  }
184 
185  if (mLogToFile == true)
186  {
187  fputc('\n', mFout);
188  fflush(mFout);
189  }
190 
191  //tuple[2] should contain a string "Error" but that is a assumption and I am sure it has more stings to give us.
192  //tuple[3] gives me for the moment the object None. So its marked as unknown for the moment.
193 
194  return true;
195  }
196 
197 protected:
198 
200  {
201  PyString & msg = *packet->AsString();
202  if (mLogToConsole == true)
203  {
204  fprintf(stdout, "%s\n", msg.content().c_str());
205  }
206 
207  if (mLogToFile == true)
208  {
209  fprintf(mFout, "%s\n", msg.content().c_str());
210  }
211  }
212 
214  {
215  PyBuffer & msg = *packet->AsBuffer();
216  if (mLogToConsole == true)
217  {
218  fwrite(&msg.content()[0], msg.size(), 1, stdout);
219  fputc('\n', stdout);
220  }
221 
222  if (mLogToFile == true)
223  {
224  fwrite(&msg.content()[0], msg.size(), 1, mFout);
225  fputc('\n', mFout);
226  }
227  }
228 
229  // its unclear what this consists of so the current code mimics the one of _logInternBufferMessage
231  {
232  // just placement code atm...
233  /*PyBuffer & msg = *packet->AsBuffer();
234  if (mLogToConsole == true)
235  {
236  fwrite(&msg.content()[0], msg.size(), 1, stdout);
237  fputc('\n', stdout);
238  }
239 
240  if (mLogToFile == true)
241  {
242  fwrite(&msg.content()[0], msg.size(), 1, mFout);
243  fputc('\n', mFout);
244  }*/
245 
246  if (packet->GetType() != PyRep::PyTypeString)
247  return;
248 
249  PyString & msg = *packet->AsString();
250  if (mLogToConsole == true)
251  {
252  fwrite(msg.content().c_str(), msg.content().size(), 1, stdout);
253  fputc('\n', stdout);
254  }
255 
256  if (mLogToFile == true)
257  {
258  fwrite(msg.content().c_str(), msg.content().size(), 1, mFout);
259  fputc('\n', mFout);
260  }
261 
262  }
263 
271  void _logInternMessage(const char* str, ...)
272  {
273  va_list ap;
274  va_start(ap, str);
275 
277  // I know its code duplication....on a small scale
278  if (mLogToConsole == true)
279  {
280  fprintf(stdout, "[PyTrace] ");
281  vfprintf(stdout, str, ap);
282  putc('\n', stdout);
283  }
284 
285  if (mLogToFile == true)
286  {
287  fprintf(mFout, "[PyTrace] ");
288  vfprintf(mFout, str, ap);
289  putc('\n', mFout);
290  fflush(mFout);
291  }
293  va_end(ap);
294  }
295 
303  void _setLogColor(uint32 color)
304  {
305 #ifdef HAVE_WINDOWS_H
306  SetConsoleTextAttribute(mStdoutHandle, TNORMAL);
307 #else /* !HAVE_WINDOWS_H */
308  puts(colorstrings[TNORMAL]);
309 #endif /* !HAVE_WINDOWS_H */
310  }
311 
312 private:
313  FILE *mFout;
317 
318 #ifdef HAVE_WINDOWS_H
319  HANDLE mStdoutHandle, mStderrHandle;
320 #endif /* HAVE_WINDOWS_H */
321 };
322 
323 #endif//PY_TRACE_LOG_H
a python client stack trace logger for Evemu
Definition: PyTraceLog.h:74
Base Python wire object.
Definition: PyRep.h:66
void _logInternMessage(const char *str,...)
intern function for handling logger related messages
Definition: PyTraceLog.h:271
void _logInternBufferPacket(PyRep *packet)
Definition: PyTraceLog.h:230
void _logInternStringMessage(PyRep *packet)
Definition: PyTraceLog.h:199
PyRep * GetItem(size_t index) const
Returns Python object.
Definition: PyRep.h:602
Python string.
Definition: PyRep.h:430
const Buffer & content() const
Get the const PyBuffer content.
Definition: PyRep.h:407
bool IsTuple() const
Definition: PyRep.h:108
bool mLogToFile
Definition: PyTraceLog.h:316
void _logInternBufferMessage(PyRep *packet)
Definition: PyTraceLog.h:213
Python tuple.
Definition: PyRep.h:567
PyType GetType() const
Definition: PyRep.h:98
#define TRED
Definition: PyTraceLog.h:40
static const char * colorstrings[TBLUE+1]
Definition: PyTraceLog.h:50
#define TBLUE
Definition: PyTraceLog.h:45
size_t size() const
Obtains length of the buffer.
Definition: PyRep.cpp:422
PyTraceLog(const char *path, bool toConsole=false, bool toFile=false)
The constructor of the python stack trace logger.
Definition: PyTraceLog.h:85
FILE * mFout
Definition: PyTraceLog.h:313
PyString * AsString()
Definition: PyRep.h:132
PyBuffer * AsBuffer()
Definition: PyRep.h:130
unsigned __int32 uint32
Definition: eve-compat.h:50
bool logTrace(PyTuple &tuple)
logTrace is the function what its all about.
Definition: PyTraceLog.h:133
const std::string & content() const
Get the PyString content.
Definition: PyRep.h:458
Python buffer.
Definition: PyRep.h:382
bool mLogToConsole
Definition: PyTraceLog.h:315
~PyTraceLog()
the destructor.... nothing special
Definition: PyTraceLog.h:116
void _setLogColor(uint32 color)
Internal function to set the console output color.
Definition: PyTraceLog.h:303
#define TNORMAL
Definition: PyTraceLog.h:43
bool mInitialized
Definition: PyTraceLog.h:314