Angel 3.2
A 2D Game Prototyping Engine
Log.cpp
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "stdafx.h"
31 #include "../Infrastructure/Log.h"
32 
33 #include "../Infrastructure/World.h"
34 #include "../Util/MathUtil.h"
35 #include "../Util/FileUtil.h"
36 
37 #include <stdio.h>
38 #include <time.h>
39 #include <cstdarg>
40 
41 
42 CompoundLog* CompoundLog::_sysLog = NULL;
43 
44 #define MAX_LOG_STRING 1024
45 
46 const String __GetTimeString()
47 {
48  time_t rawtime;
49  time ( &rawtime );
50 
51  #if defined(WIN32)
52  const unsigned int timeWidth = 26;
53  char timeString[timeWidth]; //ctime_s fills a string of exactly 26 characters
54  ctime_s(timeString, timeWidth, &rawtime);
55  #elif defined(__APPLE__) || defined(__linux__)
56  char *timeString = ctime(&rawtime);
57  #endif
58  return TrimString(timeString);
59 }
60 
61 void DeveloperLog::Printf(const char* format, ...)
62 {
63  char buff[MAX_LOG_STRING];
64  va_list args;
65  va_start(args, format);
66  vsnprintf(buff, MAX_LOG_STRING, format, args);
67  va_end(args);
68 
69  this->Log(String(buff));
70 }
71 
73 {
74  //Add unique
75  for( unsigned int i = 0; i < _logs.size(); i++ )
76  {
77  if( addLog == _logs[i] )
78  return;
79  }
80 
81  _logs.push_back( addLog );
82 }
83 
84 void CompoundLog::Log( const String& val )
85 {
86  for( unsigned int i = 0; i < _logs.size(); i++ )
87  {
88  _logs[i]->Log( val );
89  }
90 }
91 
93 {
94  if (_sysLog == NULL)
95  {
96  _sysLog = new CompoundLog();
97  _sysLog->AddLog(new SystemLog());
98  }
99  return *_sysLog;
100 }
101 
102 
103 void ConsoleLog::Log( const String& val)
104 {
105  if (theWorld.GetConsole() != NULL)
106  {
107  #if !ANGEL_MOBILE
108  theWorld.GetConsole()->WriteToOutput(val + "\n");
109  #endif
110  }
111 }
112 
113 String FileLog::MakeLogFileName( const String& fileName )
114 {
115  String logDir = GetStorageDirectory() + "Logs/";
116  #if defined(WIN32)
117  // logDir is already sufficient
118  #elif defined(__APPLE__)
119  MakeDirectories(logDir);
120  #elif defined(__linux__)
121  // logDir is already sufficient
122  #endif
123 
124  return logDir + fileName + ".log";
125 }
126 
127 
128 FileLog::FileLog( const String& fileName )
129 : _fileName(fileName)
130 {
131  //TODO: may want to backup the old log?
132  //Clear the current log
133  StringList logHeader;
134  logHeader.push_back( String("Opened Log: ") + _fileName );
135  logHeader.push_back( String("On: ") + __GetTimeString() );
136  logHeader.push_back("");
137  WriteLinesToFile( _fileName, logHeader );
138 }
139 
140 void FileLog::Log( const String& val)
141 {
142  AppendLineToFile( _fileName, __GetTimeString() + ": " + val );
143 }
144 
145 
146 void SystemLog::Log( const String &val)
147 {
148  #if defined(_MSC_VER)
149  //output to pane
150  OutputDebugString(val.c_str());
151  OutputDebugString("\n");
152  #else
153  std::cout << val << std::endl;
154  #endif
155 }