~thumper/nux/next-changes

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Copyright 2010 Inalogic® Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License, as
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */


#include "NuxCore.h"
#include "Parsing.h"

namespace nux
{

#if defined(NUX_DEBUG)
//! Create a backup copy of a file if it exist. The copy feature a timestamp in the filename.
  /*!
      @param Filename The name of the file to check.
  */
  static void CreateBackupCopy (const TCHAR *Filename)
  {
    if (GFileManager.FileSize (Filename) > 0)
    {
      // Create string with system time to create a unique filename.
      unsigned int Year, Month, Day, Hour, Min, Sec, MSec;
      GetLocalTime (Year, Month, Day, Hour, Min, Sec, MSec);
      NString Name, Extension;
      NString (Filename).SplitAtLastOccurenceOf (TEXT ("."), Name, Extension);
      NString BackupFilename;

      if (Extension.Size() )
      {
        BackupFilename = NString::Printf (TEXT ("%s-backup-%i.%02i.%02i-%02i.%02i.%02i.%s"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec, Extension.GetTCharPtr() );
      }
      else
      {
        BackupFilename = NString::Printf (TEXT ("%s-backup-%i.%02i.%02i-%02i.%02i.%02i"), Name.GetTCharPtr(), Year, Month, Day, Hour, Min, Sec);
      }

      GFileManager.Copy (BackupFilename.GetTCharPtr(), Filename, true, true, NULL);
    }
  }
#endif

  NUX_IMPLEMENT_GLOBAL_OBJECT (NullOutput);
  NUX_IMPLEMENT_GLOBAL_OBJECT (LogOutputRedirector);
  NUX_IMPLEMENT_GLOBAL_OBJECT (LogFileOutput);
  NUX_IMPLEMENT_GLOBAL_OBJECT (VisualOutputConsole)
  NUX_IMPLEMENT_GLOBAL_OBJECT (PrintfOutputConsole)

  LogOutputDevice::LogOutputDevice()
  : _object_destroyed (false)
  {
    _enabled = true;
  }

  LogOutputDevice::~LogOutputDevice()
  {
    _object_destroyed = true;
  }

  void LogOutputDevice::Shutdown()
  {
    _object_destroyed = true;
  }

  void LogOutputDevice::Flush()
  {

  }

  void LogOutputDevice::Enable ()
  {
    _enabled = true;
  }

  void LogOutputDevice::Disable ()
  {
    _enabled = false;
  }


  VARARG_BODY ( void /*FuncRet*/, LogOutputDevice::LogFunction/*FuncName*/, const TCHAR* /*FmtType*/, VARARG_EXTRA (int severity) /*ExtraDecl*/)
  {
    if (_object_destroyed)
      return;

    t_int   BufferSize	= 1024;
    t_int   NewBufferSize	= 0;
    TCHAR  *Buffer		= NULL;
    t_int   Result		= -1;

    while (Result == -1)
    {
      if (NewBufferSize)
      {
        TCHAR *temp = new TCHAR[NewBufferSize];
        Memcpy (temp, Buffer, BufferSize);
        NUX_SAFE_DELETE_ARRAY (Buffer);
        Buffer = temp;
        BufferSize = NewBufferSize;
      }
      else
      {
        Buffer = new TCHAR[BufferSize];
      }

      GET_VARARGS_RESULT (Buffer, BufferSize, BufferSize - 1, Fmt, Result);

      if (Result == -1)
        NewBufferSize = 2 * BufferSize;
    };

    Buffer[Result] = 0;

    Serialize (Buffer, TEXT ("Nux"), severity);

    NUX_SAFE_DELETE_ARRAY (Buffer);
  }

  void LogFileOutput::Constructor()
  {
    m_LogSerializer = NULL;
    m_Opened = false;
    m_Closed = false;

#if defined(NUX_DEBUG)
    // The Editor requires a fully qualified directory to not end up putting the log in various directories.
    m_Filename = GetProgramDirectory();

    if ( (m_Filename[m_Filename.Size()-1] != NUX_SLASH_CHAR) || (m_Filename[m_Filename.Size()-1] != NUX_BACKSLASH_CHAR) )
      m_Filename += (const TCHAR *) NUX_PATH_SEPARATOR_STRING;

    m_Filename += GetLogDirectory();

    // Create the directory tree where the Logs file will be stored.
    GFileManager.MakeDirectory (m_Filename.GetTCharPtr(), 1);

    m_Filename += (const TCHAR *) NUX_PATH_SEPARATOR_STRING;
    m_Filename += TEXT ("nux");
    m_Filename += TEXT (".log");

    // if the file already exists, create a backup as we are going to overwrite it
    if (!m_Opened)
    {
      CreateBackupCopy (m_Filename.GetTCharPtr() );
    }

    // Open log file.
    m_LogSerializer = GFileManager.CreateFileWriter (m_Filename.GetTCharPtr(), NSerializer::Read | NSerializer::Write | NSerializer::OverWriteReadOnly | (m_Opened ? NSerializer::Append : 0) );

    if (m_LogSerializer)
    {
      m_Opened = true;
#if UNICODE && !NUX_LOG_FILE_ANSI
      m_LogSerializer->Serialize ( (void *) &NUX_UTF16_BE[1], NUX_UTF16_BE[0] /*size*/ );
#endif
      LogFunction (NUX_MSG_SEVERITY_NONE, TEXT ("Log file open, %s"), GetFormattedLocalTime().GetTCharPtr());
    }
    else
    {
      m_Closed = true;
    }

#endif

  }

  void LogFileOutput::Destructor()
  {
    Shutdown();
  }

  /*!
      Closes output device and cleans up. This can't happen in the destructor
      as we have to call "delete" which cannot be done for static/ global objects.
  */
  void LogFileOutput::Shutdown()
  {
    if (m_LogSerializer)
    {
      LogFunction (NUX_MSG_SEVERITY_NONE, TEXT ("Log file closed, %s"), GetFormattedLocalTime().GetTCharPtr());
      Flush();
      delete m_LogSerializer;
      m_LogSerializer = NULL;
    }

    m_Closed = true;
  }

  void LogFileOutput::Flush()
  {
    if (m_LogSerializer)
    {
      m_LogSerializer->Flush();
    }
  }


  //! Write data to the output log file.
  /*!
      @param  log_data    Text to log.
      @param  log_prefix	Prefix for the text
  */
  void LogFileOutput::Serialize (const TCHAR *log_data, const TCHAR *log_prefix, int severity)
  {
    if (!_enabled)
      return;

    if (_object_destroyed)
      return;

    if (m_LogSerializer)
    {
#if UNICODE && NUX_LOG_FILE_ANSI
      ANSICHAR ACh[1024];
      INT DataOffset = 0;
      INT i;

      while (log_data[DataOffset])
      {
        for (i = 0; i < NUX_ARRAY_COUNT (ACh) && log_data[DataOffset]; i++, DataOffset++)
        {
          ACh[i] = ConvertTCHARToAnsiChar (log_data[DataOffset]);
        }

        // serialize chunks of 1024 characters
        m_LogSerializer->Serialize (ACh, i);
      };

      for (i = 0; NUX_LINE_TERMINATOR[i]; i++)
      {
        ACh[i] = NUX_LINE_TERMINATOR[i];
      }

      m_LogSerializer->Serialize (ACh, i);
#else
      NString Raw = NString (log_prefix) + NString (TEXT (": ") ) + NString (log_data) + NString (NUX_LINE_TERMINATOR);
      SerializeRaw (Raw.GetTCharPtr() );
#endif
    }
  }

  void LogFileOutput::SerializeRaw (const TCHAR *log_data)
  {
    t_u32 s = (t_u32) StringLength (log_data) * sizeof (TCHAR);
    m_LogSerializer->Serialize (NUX_CONST_CAST (TCHAR *, log_data), s);
  }

  void LogOutputRedirector::Constructor()
  {

  }

  void LogOutputRedirector::Destructor()
  {
    Shutdown();
  }

  void LogOutputRedirector::AddOutputDevice (LogOutputDevice *OutputDevice)
  {
    if ( OutputDevice )
    {
      if (std::find (OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end() )
        return;

      OutputDevices.push_back (OutputDevice);
    }
  }

  void LogOutputRedirector::RemoveOutputDevice (LogOutputDevice *OutputDevice)
  {
    std::vector<LogOutputDevice *>::iterator it = std::find (OutputDevices.begin(), OutputDevices.end(), OutputDevice);
    OutputDevices.erase (it);
  }

  bool LogOutputRedirector::IsRedirectingTo (LogOutputDevice *OutputDevice)
  {
    if (std::find (OutputDevices.begin(), OutputDevices.end(), OutputDevice) != OutputDevices.end() )
      return true;

    return false;
  }

  void LogOutputRedirector::Serialize (const TCHAR *log_data, const TCHAR *log_prefix, int severity)
  {
    if (!_enabled)
      return;

    for (t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
    {
      OutputDevices[OutputDeviceIndex]->Serialize (log_data, log_prefix, severity);
    }
  }

  void LogOutputRedirector::Flush()
  {
    for (t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
    {
      OutputDevices[OutputDeviceIndex]->Flush();
    }
  }

  void LogOutputRedirector::Shutdown()
  {
    for (t_u32 OutputDeviceIndex = 0; OutputDeviceIndex < OutputDevices.size(); OutputDeviceIndex++)
    {
      OutputDevices[OutputDeviceIndex]->Shutdown();
      // do not delete the output device. This is the responsibility of the owwners.
    }

    OutputDevices.clear();
  }


  void VisualOutputConsole::Constructor() {}

  void VisualOutputConsole::Destructor() {}

//! Write data to visual studio output debug console.
  /*!
      @param  log_data        Text to log.
      @param  log_prefix	Prefix for the text
  */
  void VisualOutputConsole::Serialize (const TCHAR *text, const TCHAR *log_prefix, int severity)
  {
    if (!_enabled)
      return;

#if defined (NUX_OS_WINDOWS)
    TCHAR Temp[4096];

    Snprintf (Temp, 4096, 4096 - 1, TEXT ("%s: %s%s"), log_prefix, text, NUX_LINE_TERMINATOR);
    OutputDebugString (Temp);
#endif
  }

  void PrintfOutputConsole::Constructor() {}

  void PrintfOutputConsole::Destructor() {}

//! Write data to visual studio output debug console.
  /*!
      @param  log_data        Text to log.
      @param  log_prefix	Prefix for the text
  */
  void PrintfOutputConsole::Serialize (const TCHAR *text, const TCHAR *log_prefix, int severity)
  {
    if (!_enabled)
      return;

    TCHAR Temp[4096];
#if defined (NUX_OS_WINDOWS)
    Snprintf (Temp, 4096, 4096 - 1, TEXT ("%s: %s%s"), log_prefix, text, NUX_LINE_TERMINATOR);

    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    

    if (severity == NUX_MSG_SEVERITY_CRITICAL)
    {
      SetConsoleTextAttribute(hConsole, FOREGROUND_RED|FOREGROUND_INTENSITY|BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY);
    }
    else if (severity == NUX_MSG_SEVERITY_ALERT)
    {
      SetConsoleTextAttribute(hConsole, FOREGROUND_RED|FOREGROUND_INTENSITY);
    }
    else if (severity == NUX_MSG_SEVERITY_WARNING)
    {
      SetConsoleTextAttribute(hConsole, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
    }
    else if (severity == NUX_MSG_SEVERITY_INFO)
    {
      SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
    }
    else if (severity == NUX_MSG_SEVERITY_NONE)
    {
      SetConsoleTextAttribute(hConsole, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
    }

    printf ("%s", &Temp[0]);

    SetConsoleTextAttribute(hConsole, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);

#elif defined (NUX_OS_LINUX)
//     {attr} is one of following
//
//     0	Reset All Attributes (return to normal mode)
//     1	Bright (Usually turns on BOLD)
//     2 	Dim
//     3	Underline
//     5	Blink
//     7 	Reverse
//     8	Hidden
//
//     {fg} is one of the following
//
//     30   Black
//     31   Red
//     32   Green
//     33   Yellow
//     34   Blue
//     35   Magenta
//     36   Cyan
//     37   White (greyish)
//     38   White
//
//     {bg} is one of the following
//
//     40   Black
//     41   Red
//     42   Green
//     43   Yellow
//     44   Blue
//     45   Magenta
//     46   Cyan
//     47   White (greyish)
//     48   White

    int Foreground = 38;
    int Background = 48;
    int Bold = 0;

    if (severity == NUX_MSG_SEVERITY_CRITICAL)
    {
      Foreground = 31;
      Background = 44;
      Bold = 1;
    }
    else if (severity == NUX_MSG_SEVERITY_ALERT)
    {
      Foreground = 31;
      Bold = 1;
    }
    else if (severity == NUX_MSG_SEVERITY_WARNING)
    {
      Foreground = 33;
      Bold = 1;
    }
    else if (severity == NUX_MSG_SEVERITY_INFO)
    {
      Foreground = 32;
      Bold = 1;
    }
    else if (severity == NUX_MSG_SEVERITY_NONE)
    {
      Foreground = 38;
      Bold = 0;
    }


    Snprintf (Temp, 4096, 4096 - 1, TEXT ("%c[%d;%d;%dm%s: %s%c[%d;%d;%dm%s"), 0x1B, Bold, Foreground, Background, log_prefix, text, 0x1B, 0, 38, 48, NUX_LINE_TERMINATOR);
    printf ("%s", &Temp[0]);

#else
    Snprintf (Temp, 4096, 4096 - 1, TEXT ("%s: %s%s"), log_prefix, text, NUX_LINE_TERMINATOR);
    printf ("%s", &Temp[0]);
#endif
  }


  void NullOutput::Constructor() {}

  void NullOutput::Destructor() {}


}