-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiagnostic.cpp
More file actions
91 lines (77 loc) · 2.55 KB
/
Diagnostic.cpp
File metadata and controls
91 lines (77 loc) · 2.55 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
#include "Base.h"
#include "Diagnostic.h"
#ifdef _DEBUG
#include "TimeUtils.h"
#include "FileSystemUtils.h"
#include "FilePathUtils.h"
#include <mutex>
NS_BEGIN
static std::unique_ptr<std::mutex> g_pDiagnosticMutex;
//#################################################################################################
void DiagnosticInit(void)
{
if(!g_pDiagnosticMutex)
g_pDiagnosticMutex = std::make_unique<std::mutex>();
}
//#################################################################################################
void DiagnosticFree(void)
{
g_pDiagnosticMutex.reset();
}
//#################################################################################################
void DiagnosticAssert(const bool bCondition, PCNSTR szFile, const long nLine)
{
if(!bCondition)
{
CStr strMsg(CStr::EFT_Format, _N("[{0}] Assertion failed. {1} ({2}){3}"), GetFormattedTime(), szFile, nLine, g_szEol);
if(g_pDiagnosticMutex)
{
CFilePath pathFile = GetExecutablePath();
pathFile.SetExtension(_N("txt"));
NHANDLE hFile = INVALID_NHANDLE;
std::lock_guard<std::mutex> lock(*g_pDiagnosticMutex);
if(FileCreate(pathFile.Get(), EFM_AlwaysReadWrite, hFile) == FW_NO_ERROR)
{
size_t nBytesWritten;
FileSetPosition(hFile, 0, EFS_End); // Move to the end so we append
FileWrite(hFile, (PCBYTE)(PCNSTR)strMsg, strMsg.GetSize(), nBytesWritten);
FileClose(hFile);
}
}
strMsg.Delete(strMsg.GetLength() - g_nEolLen, CStr::end);
#ifdef _WIN32
strMsg.DebugPrint();
std::cout << strMsg.AsUtf8() << std::endl;
#else
std::cout << strMsg << std::endl;
#endif
}
}
//#################################################################################################
void DiagnosticMessage(PCNSTR szMsg)
{
CStr strMsg(CStr::EFT_Format, _N("[{0}] {1}{2}"), GetFormattedTime(), szMsg, g_szEol);
if(g_pDiagnosticMutex)
{
CFilePath pathFile = GetExecutablePath();
pathFile.SetExtension(_N("txt"));
NHANDLE hFile = INVALID_NHANDLE;
std::lock_guard<std::mutex> lock(*g_pDiagnosticMutex);
if(FileCreate(pathFile.Get(), EFM_AlwaysReadWrite, hFile) == FW_NO_ERROR)
{
size_t nBytesWritten;
FileSetPosition(hFile, 0, EFS_End); // Move to the end so we append
FileWrite(hFile, (PCBYTE)(PCNSTR)strMsg, strMsg.GetSize(), nBytesWritten);
FileClose(hFile);
}
}
strMsg.Delete(strMsg.GetLength() - g_nEolLen, CStr::end);
#ifdef _WIN32
strMsg.DebugPrint();
std::cout << strMsg.AsUtf8() << std::endl;
#else
std::cout << strMsg << std::endl;
#endif
}
NS_END
#endif