27
//! Create a backup copy of a file if it exist. The copy feature a timestamp in the filename.
29
@param Filename The name of the file to check.
31
static void CreateBackupCopy(const TCHAR* Filename)
33
if(GFileManager.FileSize(Filename) > 0)
35
// Create string with system time to create a unique filename.
36
unsigned int Year, Month, Day, Hour, Min, Sec, MSec;
37
GetLocalTime(Year, Month, Day, Hour, Min, Sec, MSec);
38
NString Name, Extension;
39
NString(Filename).SplitAtLastOccurenceOf(TEXT("."), Name, Extension);
40
NString BackupFilename;
43
BackupFilename = NString::Printf(TEXT("%s-backup-%i.%02i.%02i-%02i.%02i.%02i.%s"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec, Extension.GetTCharPtr());
47
BackupFilename = NString::Printf(TEXT("%s-backup-%i.%02i.%02i-%02i.%02i.%02i"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec);
49
GFileManager.Copy(BackupFilename.GetTCharPtr(), Filename, true, true, NULL);
53
INL_IMPLEMENT_GLOBAL_OBJECT(NNullOutput);
54
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputDeviceRedirector);
55
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputLogFile);
56
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputVisualDebugConsole)
58
void NOutputDevice::Shutdown()
60
m_ObjectDestroyed = TRUE;
63
void NOutputDevice::Flush()
68
VARARG_BODY( void /*FuncRet*/, NOutputDevice::LogFunction/*FuncName*/, const TCHAR* /*FmtType*/, VARARG_NONE/*ExtraDecl*/)
73
INT BufferSize = 1024;
79
Buffer = (TCHAR*) inlSystemRealloc( Buffer, BufferSize * sizeof(TCHAR) );
80
GET_VARARGS_RESULT(Buffer, BufferSize, BufferSize-1, Fmt, Result);
85
Serialize( Buffer, TEXT("Log") );
87
inlSystemFree( Buffer );
90
void NOutputLogFile::Constructor()
92
m_LogSerializer = NULL;
96
#if (defined DEBUG) || (defined _DEBUG) || (defined Debug)
97
// The Editor requires a fully qualified directory to not end up putting the log in various directories.
98
m_Filename = GetProgramDirectory();
99
if((m_Filename[m_Filename.Size()-1] != INL_SLASH_CHAR) || (m_Filename[m_Filename.Size()-1] != INL_BACKSLASH_CHAR))
100
m_Filename += (const TCHAR*)INL_PATH_SEPARATOR_STRING;
101
m_Filename += GetLogDirectory();
103
// Create the directory tree where the Logs file will be stored.
104
GFileManager.MakeDirectory(m_Filename.GetTCharPtr(), 1);
106
m_Filename += (const TCHAR*)INL_PATH_SEPARATOR_STRING;
107
m_Filename += TEXT("inalogic");
108
m_Filename += TEXT(".log");
110
// if the file already exists, create a backup as we are going to overwrite it
113
CreateBackupCopy(m_Filename.GetTCharPtr());
117
m_LogSerializer = GFileManager.CreateFileWriter(m_Filename.GetTCharPtr(), NSerializer::Read|NSerializer::Write|NSerializer::OverWriteReadOnly|(m_Opened ? NSerializer::Append : 0));
122
#if UNICODE && !INL_LOG_FILE_ANSI
123
m_LogSerializer->Serialize( (void*)&INL_UTF16_BE[1], INL_UTF16_BE[0] /*size*/ );
125
LogFunction(TEXT("Log file open, %s"), GetFormattedLocalTime());
135
void NOutputLogFile::Destructor()
141
Closes output device and cleans up. This can't happen in the destructor
142
as we have to call "delete" which cannot be done for static/ global objects.
144
void NOutputLogFile::Shutdown()
148
LogFunction(TEXT("Log file closed, %s"), GetFormattedLocalTime());
150
delete m_LogSerializer;
151
m_LogSerializer = NULL;
156
void NOutputLogFile::Flush()
160
m_LogSerializer->Flush();
165
//! Write data to the output log file.
167
@param Data Text to log.
168
@param LogPrefix Prefix for the text
170
void NOutputLogFile::Serialize( const TCHAR* Data, const TCHAR* LogPrefix )
172
if(m_ObjectDestroyed)
177
#if UNICODE && INL_LOG_FILE_ANSI
181
while(Data[DataOffset])
183
for(i = 0;i < INL_ARRAY_COUNT(ACh) && Data[DataOffset];i++,DataOffset++)
185
ACh[i] = ConvertTCHARToAnsiChar(Data[DataOffset]);
187
// serialize chunks of 1024 characters
188
m_LogSerializer->Serialize(ACh,i);
191
for(i = 0; INL_LINE_TERMINATOR[i]; i++)
193
ACh[i] = INL_LINE_TERMINATOR[i];
196
m_LogSerializer->Serialize(ACh, i);
198
NString Raw = NString(LogPrefix) + NString(TEXT(": ")) + NString(Data) + NString(INL_LINE_TERMINATOR);
199
SerializeRaw(Raw.GetTCharPtr());
204
void NOutputLogFile::SerializeRaw(const TCHAR* Data)
206
t_u32 s = (t_u32)StringLength(Data)*sizeof(TCHAR);
207
m_LogSerializer->Serialize(INL_CONST_CAST(TCHAR*, Data), s);
210
void NOutputDeviceRedirector::Constructor()
215
void NOutputDeviceRedirector::Destructor()
220
void NOutputDeviceRedirector::AddOutputDevice( NOutputDevice* OutputDevice )
224
if(std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end())
226
OutputDevices.push_back(OutputDevice);
230
void NOutputDeviceRedirector::RemoveOutputDevice( NOutputDevice* OutputDevice )
232
std::vector<NOutputDevice*>::iterator it = std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice);
233
OutputDevices.erase(it);
236
bool NOutputDeviceRedirector::IsRedirectingTo(NOutputDevice* OutputDevice)
238
if(std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end())
243
void NOutputDeviceRedirector::Serialize(const TCHAR* Data, const TCHAR* LogPrefix)
245
for(t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
247
OutputDevices[OutputDeviceIndex]->Serialize( Data, LogPrefix );
251
void NOutputDeviceRedirector::Flush()
253
for(t_u32 OutputDeviceIndex=0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
255
OutputDevices[OutputDeviceIndex]->Flush();
259
void NOutputDeviceRedirector::Shutdown()
261
for(t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
263
OutputDevices[OutputDeviceIndex]->Shutdown();
264
// do not delete the output device. This is the responsability of the owwners.
266
OutputDevices.clear();
270
void NOutputVisualDebugConsole::Constructor(){}
272
void NOutputVisualDebugConsole::Destructor(){}
274
//! Write data to visual studio output debug console.
276
@param Data Text to log.
277
@param LogPrefix Prefix for the text
279
void NOutputVisualDebugConsole::Serialize( const TCHAR* Data, const TCHAR* LogPrefix )
282
Snprintf(Temp, 4096, 4096 - 1, TEXT("%s: %s%s"), LogPrefix, Data, INL_LINE_TERMINATOR);
284
#if (defined _WIN32) && (defined _MSC_VER)
285
OutputDebugString(Temp);
287
printf("%s", &Temp[0]);
291
void NNullOutput::Constructor(){}
293
void NNullOutput::Destructor(){}
27
//! Create a backup copy of a file if it exist. The copy feature a timestamp in the filename.
29
@param Filename The name of the file to check.
31
static void CreateBackupCopy(const TCHAR* Filename)
33
if(GFileManager.FileSize(Filename) > 0)
35
// Create string with system time to create a unique filename.
36
unsigned int Year, Month, Day, Hour, Min, Sec, MSec;
37
GetLocalTime(Year, Month, Day, Hour, Min, Sec, MSec);
38
NString Name, Extension;
39
NString(Filename).SplitAtLastOccurenceOf(TEXT("."), Name, Extension);
40
NString BackupFilename;
43
BackupFilename = NString::Printf(TEXT("%s-backup-%i.%02i.%02i-%02i.%02i.%02i.%s"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec, Extension.GetTCharPtr());
47
BackupFilename = NString::Printf(TEXT("%s-backup-%i.%02i.%02i-%02i.%02i.%02i"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec);
49
GFileManager.Copy(BackupFilename.GetTCharPtr(), Filename, true, true, NULL);
53
INL_IMPLEMENT_GLOBAL_OBJECT(NNullOutput);
54
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputDeviceRedirector);
55
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputLogFile);
56
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputVisualDebugConsole)
58
void NOutputDevice::Shutdown()
60
m_ObjectDestroyed = TRUE;
63
void NOutputDevice::Flush()
68
VARARG_BODY( void /*FuncRet*/, NOutputDevice::LogFunction/*FuncName*/, const TCHAR* /*FmtType*/, VARARG_NONE/*ExtraDecl*/)
73
INT BufferSize = 1024;
79
Buffer = (TCHAR*) inlSystemRealloc( Buffer, BufferSize * sizeof(TCHAR) );
80
GET_VARARGS_RESULT(Buffer, BufferSize, BufferSize-1, Fmt, Result);
85
Serialize( Buffer, TEXT("Log") );
87
inlSystemFree( Buffer );
90
void NOutputLogFile::Constructor()
92
m_LogSerializer = NULL;
96
#if (defined DEBUG) || (defined _DEBUG) || (defined Debug)
97
// The Editor requires a fully qualified directory to not end up putting the log in various directories.
98
m_Filename = GetProgramDirectory();
99
if((m_Filename[m_Filename.Size()-1] != INL_SLASH_CHAR) || (m_Filename[m_Filename.Size()-1] != INL_BACKSLASH_CHAR))
100
m_Filename += (const TCHAR*)INL_PATH_SEPARATOR_STRING;
101
m_Filename += GetLogDirectory();
103
// Create the directory tree where the Logs file will be stored.
104
GFileManager.MakeDirectory(m_Filename.GetTCharPtr(), 1);
106
m_Filename += (const TCHAR*)INL_PATH_SEPARATOR_STRING;
107
m_Filename += TEXT("inalogic");
108
m_Filename += TEXT(".log");
110
// if the file already exists, create a backup as we are going to overwrite it
113
CreateBackupCopy(m_Filename.GetTCharPtr());
117
m_LogSerializer = GFileManager.CreateFileWriter(m_Filename.GetTCharPtr(), NSerializer::Read|NSerializer::Write|NSerializer::OverWriteReadOnly|(m_Opened ? NSerializer::Append : 0));
122
#if UNICODE && !INL_LOG_FILE_ANSI
123
m_LogSerializer->Serialize( (void*)&INL_UTF16_BE[1], INL_UTF16_BE[0] /*size*/ );
125
LogFunction(TEXT("Log file open, %s"), GetFormattedLocalTime());
135
void NOutputLogFile::Destructor()
141
Closes output device and cleans up. This can't happen in the destructor
142
as we have to call "delete" which cannot be done for static/ global objects.
144
void NOutputLogFile::Shutdown()
148
LogFunction(TEXT("Log file closed, %s"), GetFormattedLocalTime());
150
delete m_LogSerializer;
151
m_LogSerializer = NULL;
156
void NOutputLogFile::Flush()
160
m_LogSerializer->Flush();
165
//! Write data to the output log file.
167
@param Data Text to log.
168
@param LogPrefix Prefix for the text
170
void NOutputLogFile::Serialize( const TCHAR* Data, const TCHAR* LogPrefix )
172
if(m_ObjectDestroyed)
177
#if UNICODE && INL_LOG_FILE_ANSI
181
while(Data[DataOffset])
183
for(i = 0;i < INL_ARRAY_COUNT(ACh) && Data[DataOffset];i++,DataOffset++)
185
ACh[i] = ConvertTCHARToAnsiChar(Data[DataOffset]);
187
// serialize chunks of 1024 characters
188
m_LogSerializer->Serialize(ACh,i);
191
for(i = 0; INL_LINE_TERMINATOR[i]; i++)
193
ACh[i] = INL_LINE_TERMINATOR[i];
196
m_LogSerializer->Serialize(ACh, i);
198
NString Raw = NString(LogPrefix) + NString(TEXT(": ")) + NString(Data) + NString(INL_LINE_TERMINATOR);
199
SerializeRaw(Raw.GetTCharPtr());
204
void NOutputLogFile::SerializeRaw(const TCHAR* Data)
206
t_u32 s = (t_u32)StringLength(Data)*sizeof(TCHAR);
207
m_LogSerializer->Serialize(INL_CONST_CAST(TCHAR*, Data), s);
210
void NOutputDeviceRedirector::Constructor()
215
void NOutputDeviceRedirector::Destructor()
220
void NOutputDeviceRedirector::AddOutputDevice( NOutputDevice* OutputDevice )
224
if(std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end())
226
OutputDevices.push_back(OutputDevice);
230
void NOutputDeviceRedirector::RemoveOutputDevice( NOutputDevice* OutputDevice )
232
std::vector<NOutputDevice*>::iterator it = std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice);
233
OutputDevices.erase(it);
236
bool NOutputDeviceRedirector::IsRedirectingTo(NOutputDevice* OutputDevice)
238
if(std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end())
243
void NOutputDeviceRedirector::Serialize(const TCHAR* Data, const TCHAR* LogPrefix)
245
for(t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
247
OutputDevices[OutputDeviceIndex]->Serialize( Data, LogPrefix );
251
void NOutputDeviceRedirector::Flush()
253
for(t_u32 OutputDeviceIndex=0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
255
OutputDevices[OutputDeviceIndex]->Flush();
259
void NOutputDeviceRedirector::Shutdown()
261
for(t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
263
OutputDevices[OutputDeviceIndex]->Shutdown();
264
// do not delete the output device. This is the responsability of the owwners.
266
OutputDevices.clear();
270
void NOutputVisualDebugConsole::Constructor(){}
272
void NOutputVisualDebugConsole::Destructor(){}
274
//! Write data to visual studio output debug console.
276
@param Data Text to log.
277
@param LogPrefix Prefix for the text
279
void NOutputVisualDebugConsole::Serialize( const TCHAR* Data, const TCHAR* LogPrefix )
282
Snprintf(Temp, 4096, 4096 - 1, TEXT("%s: %s%s"), LogPrefix, Data, INL_LINE_TERMINATOR);
284
#if (defined _WIN32) && (defined _MSC_VER)
285
OutputDebugString(Temp);
287
printf("%s", &Temp[0]);
291
void NNullOutput::Constructor(){}
293
void NNullOutput::Destructor(){}