-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatformPrint.cpp
More file actions
102 lines (87 loc) · 2.98 KB
/
PlatformPrint.cpp
File metadata and controls
102 lines (87 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2006-13 HumaNature Studios Inc.
#include "PhyreCorePch.h"
#include "HumaNature/Logging/PlatformPrint.h"
namespace core {
#ifdef __HNS_WIN32__
static HANDLE gOutputHandle = INVALID_HANDLE_VALUE;
static const WORD gDefaultAttib = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
static WORD gSeverityCharacterAttribs[] =
{
FOREGROUND_INTENSITY | FOREGROUND_RED,// kFatal,
FOREGROUND_INTENSITY | FOREGROUND_RED,// kError,
gDefaultAttib, // kForce,
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,// kWarn,
gDefaultAttib, // kInfo,
gDefaultAttib, // kDebug,
gDefaultAttib, // kTrace,
};
static bool gOutputFileCreated = false;
static const char gOutputFilePath[] = "C:/temp/dokilog.txt";
static const char gOutputFileTestPath[] = "C:/temp/iwantdokilogging.txt";
static CRITICAL_SECTION gCriticalSection;
#endif // __HNS_WIN32__
void PlatformPrint::initialize()
{
#ifdef __HNS_WIN32__
InitializeCriticalSection(&gCriticalSection);
if(AttachConsole(ATTACH_PARENT_PROCESS))
{
gOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
}
FILE* testFile = NULL;
if (0 == fopen_s(&testFile,gOutputFileTestPath,"r"))
{
fclose(testFile);
FILE* outputFile = NULL;
if (0 == fopen_s(&outputFile, gOutputFilePath,"wt"))
{
fclose(outputFile);
gOutputFileCreated = true;
}
}
#if defined __HNS_USESTACKTRACE__
StackTracer::Instance().initialize();
#endif // __HNS_USESTACKTRACE__
#endif
}
void PlatformPrint::finalize()
{
#ifdef __HNS_WIN32__
if (gOutputFileCreated)
{
// create string file
const char gStringTableFilePath[] = "C:/temp/dokistrings.txt";
CompactStringHolderWriteToFile(gStringTableFilePath);
}
DeleteCriticalSection(&gCriticalSection);
#endif //__HNS_WIN32__
}
void PlatformPrint::print(const char* message, int severity)
{
#ifdef __HNS_WIN32__
EnterCriticalSection(&gCriticalSection);
OutputDebugStringA(message);
std::fputs(message, stdout);
fflush(stdout);
if(gOutputHandle != INVALID_HANDLE_VALUE)
{
SetConsoleTextAttribute(gOutputHandle, gSeverityCharacterAttribs[severity]);
WriteFile(gOutputHandle, message, (DWORD)strlen(message), nullptr, nullptr);
SetConsoleTextAttribute(gOutputHandle, gDefaultAttib);
}
if (gOutputFileCreated)
{
FILE* outputFile = NULL;
if (0 == fopen_s(&outputFile, gOutputFilePath,"at"))
{
fputs(message, outputFile);
fclose(outputFile);
}
}
LeaveCriticalSection(&gCriticalSection);
#else //! __HNS_WIN32__
(void)severity;
std::fputs(message, stdout);
#endif //! __HNS_WIN32__
}
} // namespace core