Avionics
Dropship Simulator
Logger.cpp
Go to the documentation of this file.
1 #include "Logger.h"
2 
4 #include <WinSock2.h>
5 #include <Windows.h>
6 //#include <WS2tcpip.h>
7 #include "DXUT.h" // for DXUT_ERR_MSGBOX
8 
9 #include <time.h>
10 
11 #include <io.h>
12 #include <fcntl.h>
13 #include <share.h>
14 #include <sys/stat.h>
15 
16 void Logger::Log(const char* msg, Level level, int errorCode)
17 {
18  lastError = msg;
19  lastLevel = level;
20  lastErrorCode = errorCode;
21 
22  //#if !defined(DEBUG) && !defined(_DEBUG)
23  // if(level >= TRACE_INFO)
24  // {
25  //#endif
26 
27  SYSTEMTIME st;
28  GetLocalTime(&st);
29  char timemsg[999];
30  sprintf_s(timemsg, 999, "%i-%02i-%02i %02i:%02i:%02i.%03i\t", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
31  switch (level)
32  {
33  case Level::Info:
34  break;
35  case Level::Warn:
36  strcat_s(timemsg, 999, "Warning: ");
37  break;
38  case Level::Fatal:
39  strcat_s(timemsg, 999, "FATAL: ");
40  break;
41  default:
42  strcat_s(timemsg, 999, "ERROR: ");
43  break;
44  }
45  strcat_s(timemsg, 999, msg);
46  if (errorCode != 0)
47  {
48  char errorcodemsg[99];
49  sprintf_s(errorcodemsg, 99, " (0x%x)", errorCode);
50  strcat_s(timemsg, 999, errorcodemsg);
51  }
52  strcat_s(timemsg, 999, "\n");
53 
54  _write(logfile, timemsg, static_cast<unsigned int>(strlen(timemsg)));
55 
56  //#if !defined(DEBUG) && !defined(_DEBUG)
57  // }
58  //#endif
59 
61  if (level > Level::Info)
62  {
63  HANDLE h_event_log = RegisterEventSourceA(nullptr, "Avionics");
64  if (!h_event_log)
65  {
66  DXUT_ERR_MSGBOX(L"Failed to open event source!", GetLastError());
67  }
68  else
69  {
70  WORD eventType;
71  switch (level)
72  {
73  case Level::Fatal:
74  case Level::Error:
75  eventType = EVENTLOG_ERROR_TYPE;
76  break;
77  case Level::Warn:
78  eventType = EVENTLOG_WARNING_TYPE;
79  break;
80  default:
81  eventType = EVENTLOG_INFORMATION_TYPE;
82  break;
83  }
84 
85  if (!ReportEventA(h_event_log, eventType, 0, 0, nullptr, 1, 0, &msg, nullptr))
86  {
87  DXUT_ERR_MSGBOX(L"Failed to report event!", GetLastError());
88  }
89 
90  DeregisterEventSource(h_event_log);
91  }
92  }
93 }
94 
96 {
97  SYSTEMTIME st;
98  GetLocalTime(&st);
99  char fileName[99];
100  sprintf_s(fileName, 99, "%i-%02i-%02i.log", st.wYear, st.wMonth, st.wDay);
101  _sopen_s(&logfile, fileName, _O_RDWR | _O_CREAT | _O_TEXT | _O_APPEND | _O_SEQUENTIAL, SH_DENYWR, S_IWRITE);
102  if (logfile == -1)
103  {
104  DXUT_ERR_MSGBOX(L"Could not open logfile for writing!", errno);
105  DXUTShutdown(errno);
106  }
107  char msg[99];
108  sprintf_s(msg, 99, "Logger::ctor Opened logfile \"%s\"", fileName);
109  Log(msg);
110 }
111 
113 {
114  Log("Logger::dtor Closing logfile...");
115  _close(logfile);
116  logfile = -1;
117 }
~Logger()
Definition: Logger.cpp:112
Level lastLevel
Definition: Logger.h:23
int lastErrorCode
Definition: Logger.h:24
std::string lastError
Definition: Logger.h:22
int logfile
Definition: Logger.h:8
void Log(const char *msg, Level level=Info, int errorCode=0)
These have to be in this order.
Definition: Logger.cpp:16
Level
Definition: Logger.h:11
Logger()
Definition: Logger.cpp:95