~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/NOutputDevice.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-02 03:28:11 UTC
  • Revision ID: neil.patel@canonical.com-20100902032811-i2m18tfb6pkasnvt
Remove Win EOL chars

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
 
22
22
 
23
 
#include "NKernel.h"
24
 
#include "NParsing.h"
25
 
 
26
 
NAMESPACE_BEGIN
27
 
//! Create a backup copy of a file if it exist. The copy feature a timestamp in the filename.
28
 
/*!
29
 
    @param Filename The name of the file to check.
30
 
*/
31
 
static void CreateBackupCopy(const TCHAR* Filename)
32
 
{
33
 
    if(GFileManager.FileSize(Filename) > 0)
34
 
    {
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;
41
 
        if(Extension.Size())
42
 
        {
43
 
            BackupFilename = NString::Printf(TEXT("%s-backup-%i.%02i.%02i-%02i.%02i.%02i.%s"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec, Extension.GetTCharPtr());
44
 
        }
45
 
        else
46
 
        {
47
 
            BackupFilename = NString::Printf(TEXT("%s-backup-%i.%02i.%02i-%02i.%02i.%02i"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec);
48
 
        }
49
 
        GFileManager.Copy(BackupFilename.GetTCharPtr(), Filename, true, true, NULL);
50
 
    }
51
 
}
52
 
 
53
 
INL_IMPLEMENT_GLOBAL_OBJECT(NNullOutput);
54
 
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputDeviceRedirector);
55
 
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputLogFile);
56
 
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputVisualDebugConsole)
57
 
 
58
 
void NOutputDevice::Shutdown()
59
 
{
60
 
    m_ObjectDestroyed = TRUE;
61
 
}
62
 
 
63
 
void NOutputDevice::Flush()
64
 
{
65
 
 
66
 
}
67
 
 
68
 
VARARG_BODY( void /*FuncRet*/, NOutputDevice::LogFunction/*FuncName*/, const TCHAR* /*FmtType*/, VARARG_NONE/*ExtraDecl*/)
69
 
{
70
 
    if(m_ObjectDestroyed)
71
 
        return;
72
 
 
73
 
    INT         BufferSize      = 1024;
74
 
    TCHAR*      Buffer          = NULL;
75
 
    INT         Result          = -1;
76
 
 
77
 
    while(Result == -1)
78
 
    {
79
 
        Buffer = (TCHAR*) inlSystemRealloc( Buffer, BufferSize * sizeof(TCHAR) );
80
 
        GET_VARARGS_RESULT(Buffer, BufferSize, BufferSize-1, Fmt, Result);
81
 
        if(Result == -1)
82
 
            BufferSize *= 2;
83
 
    };
84
 
    Buffer[Result] = 0;
85
 
    Serialize( Buffer, TEXT("Log") );
86
 
 
87
 
    inlSystemFree( Buffer );
88
 
}
89
 
 
90
 
void NOutputLogFile::Constructor()
91
 
{
92
 
    m_LogSerializer = NULL;
93
 
    m_Opened = false;
94
 
    m_Closed = false;
95
 
 
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();
102
 
 
103
 
    // Create the directory tree where the Logs file will be stored.
104
 
    GFileManager.MakeDirectory(m_Filename.GetTCharPtr(), 1);
105
 
 
106
 
    m_Filename += (const TCHAR*)INL_PATH_SEPARATOR_STRING;
107
 
    m_Filename += TEXT("inalogic");
108
 
    m_Filename += TEXT(".log");
109
 
 
110
 
    // if the file already exists, create a backup as we are going to overwrite it
111
 
    if (!m_Opened)
112
 
    {
113
 
        CreateBackupCopy(m_Filename.GetTCharPtr());
114
 
    }
115
 
 
116
 
    // Open log file.
117
 
    m_LogSerializer = GFileManager.CreateFileWriter(m_Filename.GetTCharPtr(), NSerializer::Read|NSerializer::Write|NSerializer::OverWriteReadOnly|(m_Opened ? NSerializer::Append : 0));
118
 
 
119
 
    if(m_LogSerializer)
120
 
    {
121
 
        m_Opened = true;
122
 
#if UNICODE && !INL_LOG_FILE_ANSI
123
 
        m_LogSerializer->Serialize( (void*)&INL_UTF16_BE[1], INL_UTF16_BE[0] /*size*/ );
124
 
#endif
125
 
        LogFunction(TEXT("Log file open, %s"), GetFormattedLocalTime());
126
 
    }
127
 
    else
128
 
    {
129
 
        m_Closed = true;
130
 
    }
131
 
#endif
132
 
 
133
 
}
134
 
 
135
 
void NOutputLogFile::Destructor()
136
 
{
137
 
    Shutdown();
138
 
}
139
 
 
140
 
/*!
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.
143
 
*/
144
 
void NOutputLogFile::Shutdown()
145
 
{
146
 
    if(m_LogSerializer)
147
 
    {
148
 
        LogFunction(TEXT("Log file closed, %s"), GetFormattedLocalTime());
149
 
        Flush();
150
 
        delete m_LogSerializer;
151
 
        m_LogSerializer = NULL;
152
 
    }
153
 
    m_Closed = true;
154
 
}
155
 
 
156
 
void NOutputLogFile::Flush()
157
 
{
158
 
    if(m_LogSerializer)
159
 
    {
160
 
        m_LogSerializer->Flush();
161
 
    }
162
 
}
163
 
 
164
 
 
165
 
//! Write data to the output log file.
166
 
/*!
167
 
    @param  Data        Text to log.
168
 
    @param  LogPrefix   Prefix for the text
169
 
*/
170
 
void NOutputLogFile::Serialize( const TCHAR* Data, const TCHAR* LogPrefix )
171
 
{
172
 
    if(m_ObjectDestroyed)
173
 
        return;
174
 
 
175
 
    if(m_LogSerializer)
176
 
    {
177
 
#if UNICODE && INL_LOG_FILE_ANSI
178
 
        ANSICHAR ACh[1024];
179
 
        INT DataOffset = 0;
180
 
        INT i;
181
 
        while(Data[DataOffset])
182
 
        {
183
 
            for(i = 0;i < INL_ARRAY_COUNT(ACh) && Data[DataOffset];i++,DataOffset++)
184
 
            {
185
 
                ACh[i] = ConvertTCHARToAnsiChar(Data[DataOffset]);
186
 
            }
187
 
            // serialize chunks of 1024 characters
188
 
            m_LogSerializer->Serialize(ACh,i);
189
 
        };
190
 
 
191
 
        for(i = 0; INL_LINE_TERMINATOR[i]; i++)
192
 
        {
193
 
            ACh[i] = INL_LINE_TERMINATOR[i];
194
 
        }
195
 
 
196
 
        m_LogSerializer->Serialize(ACh, i);
197
 
#else
198
 
        NString Raw = NString(LogPrefix) + NString(TEXT(": ")) + NString(Data) + NString(INL_LINE_TERMINATOR);
199
 
        SerializeRaw(Raw.GetTCharPtr());
200
 
#endif
201
 
    }
202
 
}
203
 
 
204
 
void NOutputLogFile::SerializeRaw(const TCHAR* Data)
205
 
{
206
 
    t_u32 s = (t_u32)StringLength(Data)*sizeof(TCHAR);
207
 
    m_LogSerializer->Serialize(INL_CONST_CAST(TCHAR*, Data), s);
208
 
}
209
 
 
210
 
void NOutputDeviceRedirector::Constructor()
211
 
{
212
 
 
213
 
}
214
 
 
215
 
void NOutputDeviceRedirector::Destructor()
216
 
{
217
 
    Shutdown();
218
 
}
219
 
 
220
 
void NOutputDeviceRedirector::AddOutputDevice( NOutputDevice* OutputDevice )
221
 
{
222
 
    if( OutputDevice )
223
 
    {
224
 
        if(std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end())
225
 
            return;
226
 
        OutputDevices.push_back(OutputDevice);
227
 
    }
228
 
}
229
 
 
230
 
void NOutputDeviceRedirector::RemoveOutputDevice( NOutputDevice* OutputDevice )
231
 
{
232
 
    std::vector<NOutputDevice*>::iterator it = std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice);
233
 
    OutputDevices.erase(it);
234
 
}
235
 
 
236
 
bool NOutputDeviceRedirector::IsRedirectingTo(NOutputDevice* OutputDevice)
237
 
{
238
 
    if(std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end())
239
 
        return true;
240
 
    return false;
241
 
}
242
 
 
243
 
void NOutputDeviceRedirector::Serialize(const TCHAR* Data, const TCHAR* LogPrefix)
244
 
{
245
 
    for(t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
246
 
    {
247
 
        OutputDevices[OutputDeviceIndex]->Serialize( Data, LogPrefix );
248
 
    }
249
 
}
250
 
 
251
 
void NOutputDeviceRedirector::Flush()
252
 
{
253
 
    for(t_u32 OutputDeviceIndex=0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
254
 
    {
255
 
        OutputDevices[OutputDeviceIndex]->Flush();
256
 
    }
257
 
}
258
 
 
259
 
void NOutputDeviceRedirector::Shutdown()
260
 
{
261
 
    for(t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
262
 
    {
263
 
        OutputDevices[OutputDeviceIndex]->Shutdown();
264
 
        // do not delete the output device. This is the responsability of the owwners.
265
 
    }
266
 
    OutputDevices.clear();
267
 
}
268
 
 
269
 
 
270
 
void NOutputVisualDebugConsole::Constructor(){}
271
 
 
272
 
void NOutputVisualDebugConsole::Destructor(){}
273
 
 
274
 
//! Write data to visual studio output debug console.
275
 
/*!
276
 
    @param  Data        Text to log.
277
 
    @param  LogPrefix   Prefix for the text
278
 
*/
279
 
void NOutputVisualDebugConsole::Serialize( const TCHAR* Data, const TCHAR* LogPrefix )
280
 
{
281
 
    TCHAR Temp[4096];
282
 
    Snprintf(Temp, 4096, 4096 - 1, TEXT("%s: %s%s"), LogPrefix, Data, INL_LINE_TERMINATOR);
283
 
 
284
 
#if (defined _WIN32) && (defined _MSC_VER)
285
 
    OutputDebugString(Temp);
286
 
#else
287
 
    printf("%s", &Temp[0]);
288
 
#endif
289
 
}
290
 
 
291
 
void NNullOutput::Constructor(){}
292
 
 
293
 
void NNullOutput::Destructor(){}
294
 
 
295
 
 
296
 
NAMESPACE_END
 
23
#include "NKernel.h"
 
24
#include "NParsing.h"
 
25
 
 
26
NAMESPACE_BEGIN
 
27
//! Create a backup copy of a file if it exist. The copy feature a timestamp in the filename.
 
28
/*!
 
29
    @param Filename The name of the file to check.
 
30
*/
 
31
static void CreateBackupCopy(const TCHAR* Filename)
 
32
{
 
33
    if(GFileManager.FileSize(Filename) > 0)
 
34
    {
 
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;
 
41
        if(Extension.Size())
 
42
        {
 
43
            BackupFilename = NString::Printf(TEXT("%s-backup-%i.%02i.%02i-%02i.%02i.%02i.%s"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec, Extension.GetTCharPtr());
 
44
        }
 
45
        else
 
46
        {
 
47
            BackupFilename = NString::Printf(TEXT("%s-backup-%i.%02i.%02i-%02i.%02i.%02i"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec);
 
48
        }
 
49
        GFileManager.Copy(BackupFilename.GetTCharPtr(), Filename, true, true, NULL);
 
50
    }
 
51
}
 
52
 
 
53
INL_IMPLEMENT_GLOBAL_OBJECT(NNullOutput);
 
54
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputDeviceRedirector);
 
55
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputLogFile);
 
56
INL_IMPLEMENT_GLOBAL_OBJECT(NOutputVisualDebugConsole)
 
57
 
 
58
void NOutputDevice::Shutdown()
 
59
{
 
60
    m_ObjectDestroyed = TRUE;
 
61
}
 
62
 
 
63
void NOutputDevice::Flush()
 
64
{
 
65
 
 
66
}
 
67
 
 
68
VARARG_BODY( void /*FuncRet*/, NOutputDevice::LogFunction/*FuncName*/, const TCHAR* /*FmtType*/, VARARG_NONE/*ExtraDecl*/)
 
69
{
 
70
    if(m_ObjectDestroyed)
 
71
        return;
 
72
 
 
73
    INT         BufferSize      = 1024;
 
74
    TCHAR*      Buffer          = NULL;
 
75
    INT         Result          = -1;
 
76
 
 
77
    while(Result == -1)
 
78
    {
 
79
        Buffer = (TCHAR*) inlSystemRealloc( Buffer, BufferSize * sizeof(TCHAR) );
 
80
        GET_VARARGS_RESULT(Buffer, BufferSize, BufferSize-1, Fmt, Result);
 
81
        if(Result == -1)
 
82
            BufferSize *= 2;
 
83
    };
 
84
    Buffer[Result] = 0;
 
85
    Serialize( Buffer, TEXT("Log") );
 
86
 
 
87
    inlSystemFree( Buffer );
 
88
}
 
89
 
 
90
void NOutputLogFile::Constructor()
 
91
{
 
92
    m_LogSerializer = NULL;
 
93
    m_Opened = false;
 
94
    m_Closed = false;
 
95
 
 
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();
 
102
 
 
103
    // Create the directory tree where the Logs file will be stored.
 
104
    GFileManager.MakeDirectory(m_Filename.GetTCharPtr(), 1);
 
105
 
 
106
    m_Filename += (const TCHAR*)INL_PATH_SEPARATOR_STRING;
 
107
    m_Filename += TEXT("inalogic");
 
108
    m_Filename += TEXT(".log");
 
109
 
 
110
    // if the file already exists, create a backup as we are going to overwrite it
 
111
    if (!m_Opened)
 
112
    {
 
113
        CreateBackupCopy(m_Filename.GetTCharPtr());
 
114
    }
 
115
 
 
116
    // Open log file.
 
117
    m_LogSerializer = GFileManager.CreateFileWriter(m_Filename.GetTCharPtr(), NSerializer::Read|NSerializer::Write|NSerializer::OverWriteReadOnly|(m_Opened ? NSerializer::Append : 0));
 
118
 
 
119
    if(m_LogSerializer)
 
120
    {
 
121
        m_Opened = true;
 
122
#if UNICODE && !INL_LOG_FILE_ANSI
 
123
        m_LogSerializer->Serialize( (void*)&INL_UTF16_BE[1], INL_UTF16_BE[0] /*size*/ );
 
124
#endif
 
125
        LogFunction(TEXT("Log file open, %s"), GetFormattedLocalTime());
 
126
    }
 
127
    else
 
128
    {
 
129
        m_Closed = true;
 
130
    }
 
131
#endif
 
132
 
 
133
}
 
134
 
 
135
void NOutputLogFile::Destructor()
 
136
{
 
137
    Shutdown();
 
138
}
 
139
 
 
140
/*!
 
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.
 
143
*/
 
144
void NOutputLogFile::Shutdown()
 
145
{
 
146
    if(m_LogSerializer)
 
147
    {
 
148
        LogFunction(TEXT("Log file closed, %s"), GetFormattedLocalTime());
 
149
        Flush();
 
150
        delete m_LogSerializer;
 
151
        m_LogSerializer = NULL;
 
152
    }
 
153
    m_Closed = true;
 
154
}
 
155
 
 
156
void NOutputLogFile::Flush()
 
157
{
 
158
    if(m_LogSerializer)
 
159
    {
 
160
        m_LogSerializer->Flush();
 
161
    }
 
162
}
 
163
 
 
164
 
 
165
//! Write data to the output log file.
 
166
/*!
 
167
    @param  Data        Text to log.
 
168
    @param  LogPrefix   Prefix for the text
 
169
*/
 
170
void NOutputLogFile::Serialize( const TCHAR* Data, const TCHAR* LogPrefix )
 
171
{
 
172
    if(m_ObjectDestroyed)
 
173
        return;
 
174
 
 
175
    if(m_LogSerializer)
 
176
    {
 
177
#if UNICODE && INL_LOG_FILE_ANSI
 
178
        ANSICHAR ACh[1024];
 
179
        INT DataOffset = 0;
 
180
        INT i;
 
181
        while(Data[DataOffset])
 
182
        {
 
183
            for(i = 0;i < INL_ARRAY_COUNT(ACh) && Data[DataOffset];i++,DataOffset++)
 
184
            {
 
185
                ACh[i] = ConvertTCHARToAnsiChar(Data[DataOffset]);
 
186
            }
 
187
            // serialize chunks of 1024 characters
 
188
            m_LogSerializer->Serialize(ACh,i);
 
189
        };
 
190
 
 
191
        for(i = 0; INL_LINE_TERMINATOR[i]; i++)
 
192
        {
 
193
            ACh[i] = INL_LINE_TERMINATOR[i];
 
194
        }
 
195
 
 
196
        m_LogSerializer->Serialize(ACh, i);
 
197
#else
 
198
        NString Raw = NString(LogPrefix) + NString(TEXT(": ")) + NString(Data) + NString(INL_LINE_TERMINATOR);
 
199
        SerializeRaw(Raw.GetTCharPtr());
 
200
#endif
 
201
    }
 
202
}
 
203
 
 
204
void NOutputLogFile::SerializeRaw(const TCHAR* Data)
 
205
{
 
206
    t_u32 s = (t_u32)StringLength(Data)*sizeof(TCHAR);
 
207
    m_LogSerializer->Serialize(INL_CONST_CAST(TCHAR*, Data), s);
 
208
}
 
209
 
 
210
void NOutputDeviceRedirector::Constructor()
 
211
{
 
212
 
 
213
}
 
214
 
 
215
void NOutputDeviceRedirector::Destructor()
 
216
{
 
217
    Shutdown();
 
218
}
 
219
 
 
220
void NOutputDeviceRedirector::AddOutputDevice( NOutputDevice* OutputDevice )
 
221
{
 
222
    if( OutputDevice )
 
223
    {
 
224
        if(std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end())
 
225
            return;
 
226
        OutputDevices.push_back(OutputDevice);
 
227
    }
 
228
}
 
229
 
 
230
void NOutputDeviceRedirector::RemoveOutputDevice( NOutputDevice* OutputDevice )
 
231
{
 
232
    std::vector<NOutputDevice*>::iterator it = std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice);
 
233
    OutputDevices.erase(it);
 
234
}
 
235
 
 
236
bool NOutputDeviceRedirector::IsRedirectingTo(NOutputDevice* OutputDevice)
 
237
{
 
238
    if(std::find(OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end())
 
239
        return true;
 
240
    return false;
 
241
}
 
242
 
 
243
void NOutputDeviceRedirector::Serialize(const TCHAR* Data, const TCHAR* LogPrefix)
 
244
{
 
245
    for(t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
 
246
    {
 
247
        OutputDevices[OutputDeviceIndex]->Serialize( Data, LogPrefix );
 
248
    }
 
249
}
 
250
 
 
251
void NOutputDeviceRedirector::Flush()
 
252
{
 
253
    for(t_u32 OutputDeviceIndex=0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
 
254
    {
 
255
        OutputDevices[OutputDeviceIndex]->Flush();
 
256
    }
 
257
}
 
258
 
 
259
void NOutputDeviceRedirector::Shutdown()
 
260
{
 
261
    for(t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
 
262
    {
 
263
        OutputDevices[OutputDeviceIndex]->Shutdown();
 
264
        // do not delete the output device. This is the responsability of the owwners.
 
265
    }
 
266
    OutputDevices.clear();
 
267
}
 
268
 
 
269
 
 
270
void NOutputVisualDebugConsole::Constructor(){}
 
271
 
 
272
void NOutputVisualDebugConsole::Destructor(){}
 
273
 
 
274
//! Write data to visual studio output debug console.
 
275
/*!
 
276
    @param  Data        Text to log.
 
277
    @param  LogPrefix   Prefix for the text
 
278
*/
 
279
void NOutputVisualDebugConsole::Serialize( const TCHAR* Data, const TCHAR* LogPrefix )
 
280
{
 
281
    TCHAR Temp[4096];
 
282
    Snprintf(Temp, 4096, 4096 - 1, TEXT("%s: %s%s"), LogPrefix, Data, INL_LINE_TERMINATOR);
 
283
 
 
284
#if (defined _WIN32) && (defined _MSC_VER)
 
285
    OutputDebugString(Temp);
 
286
#else
 
287
    printf("%s", &Temp[0]);
 
288
#endif
 
289
}
 
290
 
 
291
void NNullOutput::Constructor(){}
 
292
 
 
293
void NNullOutput::Destructor(){}
 
294
 
 
295
 
 
296
NAMESPACE_END