OpenBarnyard
 
Loading...
Searching...
No Matches
TUtil.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TUtil.h"
3#include "Toshi/TSystem.h"
4#include "Toshi/TError.h"
5#include "Toshi/T2String.h"
6#include "File/TLogFile.h"
8
9//-----------------------------------------------------------------------------
10// Enables memory debugging.
11// Note: Should be the last include!
12//-----------------------------------------------------------------------------
13#include "Core/TMemoryDebugOn.h"
14
16
17void TUtil::LogInitialise()
18{
19#ifndef TOSHI_NO_LOGS
20 TLogFile* logfile = new TLogFile;
21 TUtil::GetSingleton()->m_pDefaultLogFile = logfile;
22 TUtil::GetSingleton()->m_pCurrentLogFile = logfile;
23
24 TrimLog( "*.log", 9 );
25
26 time_t seconds;
27 time( &seconds );
28 tm* time = gmtime( &seconds );
29
30 T2FormatString256 filename;
31
32 filename.Format(
33 "Logs\\%s_%d%02d%02d_%02d_%02d_%02d.log",
34 ms_oToshiParams.szLogFileName,
35 time->tm_year + 1900,
36 time->tm_mon + 1,
37 time->tm_mday,
38 time->tm_hour,
39 time->tm_min,
40 time->tm_sec
41 );
42
43 GetCurrentLogFile()->Create( filename.Get(), "Toshi 2.0", TFALSE );
46#endif // TOSHI_NO_LOGS
47}
48
49void TUtil::CreateTPStringPool()
50{
51 TASSERT( ms_poStringPool == TNULL );
52 ms_poStringPool = new TPString8Pool*;
53 *ms_poStringPool = TNULL;
54}
55
56void TUtil::DestroyTPStringPool()
57{
58 if ( ms_poStringPool )
59 {
60 delete ms_poStringPool;
61 ms_poStringPool = TNULL;
62 }
63}
64
65void TUtil::Log( const TCHAR* a_szFormat, ... )
66{
68 {
69 auto pLogFile = GetCurrentLogFile();
70
71 T2FormatString2048 formatString;
72
73 va_list args;
74 va_start( args, a_szFormat );
75 formatString.FormatV( a_szFormat, args );
76 va_end( args );
77
78 pLogFile->Log( LogType_Info, ms_oToshiParams.szLogAppName, ms_oToshiParams.szLogAppDirName, formatString.Get() );
79 GetLogEmitter().Throw( LogEvent( pLogFile, LogType_Info, formatString.Get() ) );
80 }
81}
82
83void TUtil::Log( LogType a_eLogType, const TCHAR* a_szFormat, ... )
84{
85 auto pLogFile = GetCurrentLogFile();
86
87 if ( pLogFile )
88 {
89 T2FormatString2048 formatString;
90
91 va_list args;
92 va_start( args, a_szFormat );
93 formatString.FormatV( a_szFormat, args );
94 va_end( args );
95
96 pLogFile->Log( a_eLogType, ms_oToshiParams.szLogAppName, ms_oToshiParams.szLogAppDirName, formatString.Get() );
97 GetLogEmitter().Throw( LogEvent( pLogFile, a_eLogType, formatString.Get() ) );
98 }
99}
100
102{
103 TUtil::GetSingletonSafe()->m_pCurrentLogFile->Down();
104}
105
107{
108 TUtil::GetSingletonSafe()->m_pCurrentLogFile->Up();
109}
110
111void TUtil::LogConsole( const TCHAR* a_szFormat, ... )
112{
113 auto pLogFile = GetCurrentLogFile();
114
115 if ( pLogFile )
116 {
117 T2FormatString2048 formatString;
118
119 va_list args;
120 va_start( args, a_szFormat );
121 formatString.FormatV( a_szFormat, args );
122 va_end( args );
123
124 OutputDebugStringA( formatString.Get() );
125 printf( formatString.Get() );
126 }
127}
128
129void TUtil::LogSet( TLogFile* a_pLogFile )
130{
131 Log( "Changing log file." );
132 TUtil* pUtil = TUtil::GetSingleton();
133
134 if ( pUtil->m_pCurrentLogFile )
135 {
136 pUtil->m_pCurrentLogFile->Close();
137 }
138
139 pUtil->m_pCurrentLogFile = ( !a_pLogFile ) ? pUtil->m_pDefaultLogFile : a_pLogFile;
140}
141
143 : m_LogEmitter( this )
144{
145 m_pDefaultLogFile = TNULL;
146 m_pCurrentLogFile = TNULL;
147}
148
149TBOOL TUtil::ToshiCreate( const TOSHIParams& a_rToshiParams )
150{
151 ms_oToshiParams = a_rToshiParams;
152
154 CreateKernelInterface();
156 CreateTPStringPool();
157 Create();
158
159 return TTRUE;
160}
161
162void TUtil::CreateKernelInterface()
163{
165}
166
168{
169 TIMPLEMENT();
170 ms_oGlobalMutex.Destroy();
171
173}
174
175void TUtil::Create()
176{
177 TUtil::CreateSingleton()->LogInitialise();
179}
180
181// Source: https://lentz.com.au/blog/tag/crc-table-generator
183{
184 TUINT32 crc;
185
186 for ( TINT i = 0; i < CRC_TABSIZE; i++ )
187 {
188 crc = i;
189 for ( TINT j = 8; j > 0; j-- )
190 {
191 if ( crc & 1 )
192 crc = ( crc >> 1 ) ^ CRC32POLY;
193 else
194 crc >>= 1;
195 }
196 s_aiCRC32LUT[ i ] = crc;
197 }
198}
199
200// Source: https://lentz.com.au/blog/tag/crc-table-generator
201TUINT32 TUtil::CRC32( void* buffer, TUINT32 len )
202{
203 TBYTE* bytes = (TBYTE*)buffer;
204 TUINT32 crc = 0;
205
206 while ( len-- )
207 crc = crc32upd( s_aiCRC32LUT, crc, *bytes++ );
208
209 return CRC32POST( crc );
210}
211
Logging system for the Toshi engine.
#define TIMPLEMENT()
Definition Defines.h:136
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
T2FormatString< 256, T2StringTraits< TCHAR > > T2FormatString256
Definition T2String.h:243
T2FormatString< 2048, T2StringTraits< TCHAR > > T2FormatString2048
Definition T2String.h:246
#define crc32upd(crctab, crc, c)
Definition TUtil.h:176
#define CRC32POST(crc)
Definition TUtil.h:174
#define CRC_TABSIZE
Definition TUtil.h:179
#define CRC32POLY
Definition TUtil.h:173
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 TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
uint8_t TBYTE
Definition Typedefs.h:19
static TBOOL Create()
void Down()
Definition TLogFile.cpp:165
Error Create(const TCHAR *fileName, const TCHAR *str2, TBOOL writeExisting)
Definition TLogFile.cpp:31
void AllowIndentation(TBOOL a_bAllow)
Definition TLogFile.h:46
void SetSimpleMode(TBOOL a_bEnable)
Definition TLogFile.h:49
void Close()
Definition TLogFile.cpp:69
void Up()
Definition TLogFile.cpp:171
static void Uninitialise()
void Format(const CharType *a_szFormat,...)
Definition T2String.h:172
constexpr CharType * Get()
Definition T2String.h:204
void FormatV(const CharType *a_szFormat, va_list a_Args)
Definition T2String.h:183
void Throw(Data *pData)
Definition TEvent.h:56
static TFORCEINLINE TKernelInterface * CreateSingleton(Args &&... args)
Definition TSingleton.h:17
static TFORCEINLINE TBOOL IsSingletonCreated()
Definition TSingleton.h:43
static TFORCEINLINE TUtil * GetSingleton()
Definition TSingleton.h:49
static TFORCEINLINE TUtil * GetSingletonSafe()
Definition TSingleton.h:37
static TBOOL Create()
Definition TSystem.cpp:60
static void TrimLog(const TCHAR *fileExtension, TSIZE trimTo)
Definition TUtil_Win.cpp:16
static void LogDown()
Definition TUtil.cpp:101
static void Log(const TCHAR *a_szFormat,...)
Definition TUtil.cpp:65
LogType
Definition TUtil.h:21
@ LogType_Info
Definition TUtil.h:22
static TEmitter< TUtil, TUtil::LogEvent > & GetLogEmitter()
Definition TUtil.h:117
static TBOOL ToshiCreate(const TOSHIParams &a_rToshiParams)
Definition TUtil.cpp:149
static void CRCInitialise()
Definition TUtil.cpp:182
static TLogFile * GetCurrentLogFile()
Definition TUtil.h:116
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 void LogSet(TLogFile *a_logFile)
Definition TUtil.cpp:129