OpenBarnyard
 
Loading...
Searching...
No Matches
TUtil.h
Go to the documentation of this file.
1#pragma once
2#include "Toshi/TSingleton.h"
3#include "Toshi/TEvent.h"
4#include "Thread/TMutex.h"
5
6#include <utility>
7
9
10class TPString8Pool;
11class TLogFile;
12
13class TUtil : public TSingleton<TUtil>
14{
15public:
16 //-----------------------------------------------------------------------------
17 // Logging
18 //-----------------------------------------------------------------------------
19
28
29 static constexpr const TCHAR* kTypeStrings[] = {
30 "Info",
31 "Warning",
32 "Error",
33 "Critical"
34 };
35
36 static constexpr const TCHAR* LogTypeToString( LogType a_eType )
37 {
38 TSTATICASSERT( Toshi::TUtil::LogType_NUMOF == TARRAYSIZE( kTypeStrings ) );
39
40 return a_eType >= LogType_Info && a_eType < LogType_NUMOF ?
41 kTypeStrings[ a_eType ] :
42 "UNKNOWN";
43 }
44
45 struct LogEvent
46 {
47 constexpr LogEvent( TLogFile* a_pFile, LogType a_eType, const TCHAR* a_szString )
48 : m_pFile( a_pFile ), m_eType( a_eType ), m_szString( a_szString )
49 {}
50
54 };
55
56 // Note: this wasn't in the original engine
58 {
59 TOSHIParams() noexcept {}
60
61 const TCHAR* szCommandLine = "";
62 const TCHAR* szLogFileName = "toshi";
63 const TCHAR* szLogAppName = "Toshi";
64 const TCHAR* szLogAppDirName = "Kernel";
68 };
69
70public:
71 TUtil();
72 ~TUtil() = default;
73
74private:
75 TLogFile* m_pDefaultLogFile;
76 TLogFile* m_pCurrentLogFile;
77 TEmitter<TUtil, LogEvent> m_LogEmitter;
78
79public:
80 //-----------------------------------------------------------------------------
81 // Static members
82 //-----------------------------------------------------------------------------
83
84 static TBOOL ToshiCreate( const TOSHIParams& a_rToshiParams );
85 static void ToshiDestroy();
86
87 static const TCHAR* GetTime();
88
89 static void MemSet( void* ptr, TINT value, TSIZE size ) { std::memset( ptr, value, size ); }
90 static void* MemCopy( void* dst, const void* src, TSIZE size ) { return std::memcpy( dst, src, size ); }
91 static void MemClear( void* ptr, TSIZE size ) { std::memset( ptr, 0, size ); }
92 static TINT MemCompare( const void* ptr1, const void* ptr2, TSIZE size ) { return std::memcmp( ptr1, ptr2, size ); }
93 static void* MemMove( void* dst, const void* src, TSIZE size ) { return std::memmove( dst, src, size ); }
94
95 template <class T>
96 static void Fill( T* a_pStart, T* a_pEnd, const T& a_rcValue = T() )
97 {
98 for ( ; a_pStart != a_pEnd; a_pStart++ )
99 *a_pStart = a_rcValue;
100 }
101
102 //-----------------------------------------------------------------------------
103 // Logging
104 //-----------------------------------------------------------------------------
105
106 static void Log( const TCHAR* a_szFormat, ... );
107 static void Log( LogType a_eLogType, const TCHAR* a_szFormat, ... );
108 static void TrimLog( const TCHAR* fileExtension, TSIZE trimTo );
109
110 static void LogDown();
111 static void LogUp();
112
113 static void LogConsole( const TCHAR* a_szFormat, ... );
114 static void LogSet( TLogFile* a_logFile );
115
116 static TLogFile* GetCurrentLogFile() { return ms_pSingleton->m_pCurrentLogFile; }
118
119 static TMutex& GetGlobalMutex() { return ms_oGlobalMutex; }
120
121 static TPString8Pool* SetTPStringPool( TPString8Pool* a_pStringPool ) { return std::exchange( *ms_poStringPool, a_pStringPool ); }
122
124 {
125 TASSERT( ms_poStringPool != TNULL );
126 return *ms_poStringPool;
127 }
128
130 {
131 return ms_oToshiParams;
132 }
133
134private:
135 static void Create();
136 static void CreateKernelInterface();
137
138 static void LogInitialise();
139 static void CreateTPStringPool();
140 static void DestroyTPStringPool();
141
142private:
143 inline static TPString8Pool** ms_poStringPool;
144 inline static TMutex ms_oGlobalMutex;
145 inline static TOSHIParams ms_oToshiParams;
146
147public:
148#pragma region CRC
149
150// Source: https://lentz.com.au/blog/tag/crc-table-generator
151
152/*
153 LICENCE
154
155 This package may be freely distributed provided the files remain together,
156 in their original unmodified form.
157 All files, executables and sourcecode remain the copyrighted property of
158 Arjen G. Lentz and LENTZ SOFTWARE-DEVELOPMENT.
159 Licence for any use granted, provided this notice & CRC.DOC are included.
160 For executable applications, credit should be given in the appropriate
161 places in the program and documentation.
162 These notices must be retained in any copies of any part of this
163 documentation and/or software.
164
165 Any use of, or operation on (including copying/distributing) any of
166 the above mentioned files implies full and unconditional acceptance of
167 this licence and disclaimer.
168 */
169
170/* ------------------------------------------------------------------------- */
171/* CRC-32 CCITT */
172/* ------------------------------------------------------------------------- */
173#define CRC32POLY ( 0xEDB88320L ) /* Generator polynomial number */
174#define CRC32POST( crc ) ( ~( crc ) ) /* CRC Postconditioning before xmit */
175
176#define crc32upd( crctab, crc, c ) \
177 ( ( crctab )[ ( (TINT)( crc ) ^ ( c ) ) & 0xff ] ^ ( ( crc ) >> 8 ) )
178
179#define CRC_TABSIZE ( 256 ) /* Normal 256-entry table */
180
181 //=============================================================================
182
183 inline static TUINT32 s_aiCRC32LUT[ CRC_TABSIZE ] = {};
184
185 static void CRCInitialise();
186 static TUINT32 CRC32( void* buffer, TUINT32 len );
187
188#pragma endregion
189};
190
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TARRAYSIZE(ARRAY)
Definition Defines.h:70
#define TSTATICASSERT(...)
Definition Defines.h:67
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define CRC_TABSIZE
Definition TUtil.h:179
size_t TSIZE
Definition Typedefs.h:9
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
uint32_t TUINT32
Definition Typedefs.h:13
int TINT
Definition Typedefs.h:7
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
TSingleton(const TSingleton &)=delete
static TUtil * ms_pSingleton
Definition TSingleton.h:71
static constexpr const TCHAR * LogTypeToString(LogType a_eType)
Definition TUtil.h:36
static void MemClear(void *ptr, TSIZE size)
Definition TUtil.h:91
static void TrimLog(const TCHAR *fileExtension, TSIZE trimTo)
Definition TUtil_Win.cpp:16
static void * MemMove(void *dst, const void *src, TSIZE size)
Definition TUtil.h:93
static void LogDown()
Definition TUtil.cpp:101
static void * MemCopy(void *dst, const void *src, TSIZE size)
Definition TUtil.h:90
static void Log(const TCHAR *a_szFormat,...)
Definition TUtil.cpp:65
static const TOSHIParams & GetToshiParameters()
Definition TUtil.h:129
LogType
Definition TUtil.h:21
@ LogType_Warning
Definition TUtil.h:23
@ LogType_Info
Definition TUtil.h:22
@ LogType_Critical
Definition TUtil.h:25
@ LogType_NUMOF
Definition TUtil.h:26
@ LogType_Error
Definition TUtil.h:24
static void MemSet(void *ptr, TINT value, TSIZE size)
Definition TUtil.h:89
static const TCHAR * GetTime()
Definition TUtil_Win.cpp:54
static TEmitter< TUtil, TUtil::LogEvent > & GetLogEmitter()
Definition TUtil.h:117
~TUtil()=default
static TINT MemCompare(const void *ptr1, const void *ptr2, TSIZE size)
Definition TUtil.h:92
static void Fill(T *a_pStart, T *a_pEnd, const T &a_rcValue=T())
Definition TUtil.h:96
static TBOOL ToshiCreate(const TOSHIParams &a_rToshiParams)
Definition TUtil.cpp:149
static TPString8Pool * GetTPStringPool()
Definition TUtil.h:123
static void CRCInitialise()
Definition TUtil.cpp:182
static TLogFile * GetCurrentLogFile()
Definition TUtil.h:116
static TPString8Pool * SetTPStringPool(TPString8Pool *a_pStringPool)
Definition TUtil.h:121
static TMutex & GetGlobalMutex()
Definition TUtil.h:119
static TUINT32 CRC32(void *buffer, TUINT32 len)
Definition TUtil.cpp:201
static void LogUp()
Definition TUtil.cpp:106
TUtil()
Definition TUtil.cpp:142
static void LogConsole(const TCHAR *a_szFormat,...)
Definition TUtil.cpp:111
static TUINT32 s_aiCRC32LUT[(256)]
Definition TUtil.h:183
static void ToshiDestroy()
Definition TUtil.cpp:167
static constexpr const TCHAR * kTypeStrings[]
Definition TUtil.h:29
static void LogSet(TLogFile *a_logFile)
Definition TUtil.cpp:129
TLogFile * m_pFile
Definition TUtil.h:51
const TCHAR * m_szString
Definition TUtil.h:53
LogType m_eType
Definition TUtil.h:52
constexpr LogEvent(TLogFile *a_pFile, LogType a_eType, const TCHAR *a_szString)
Definition TUtil.h:47
const TCHAR * szCommandLine
Definition TUtil.h:61
const TCHAR * szLogAppDirName
Definition TUtil.h:64
TOSHIParams() noexcept
Definition TUtil.h:59
TBOOL bLogToConsole
Definition TUtil.h:65
const TCHAR * szLogAppName
Definition TUtil.h:63
const TCHAR * szLogFileName
Definition TUtil.h:62