All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
DebugFileLogger.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include <fstream>
6 #include <string>
7 #include "Types.h"
8 
9 #ifdef EEGEO_WIN
10 #include "WindowsFileIOHelpers.h"
11 #endif
12 
13 namespace Eegeo
14 {
15  std::string static GetTimeStamp()
16  {
17  char buffer[30];
18 #ifdef EEGEO_WIN
19  SYSTEMTIME currentTime;
20  GetSystemTime(&currentTime);
21  sprintf(buffer, "%d-%d-%d %d:%d:%d (%d)", currentTime.wYear, currentTime.wMonth, currentTime.wDay, currentTime.wHour, currentTime.wMinute, currentTime.wSecond, GetCurrentThreadId());
22 #endif
23  return std::string(buffer);
24  }
25 
27  {
28  std::ofstream file;
29 
30  ofstreamGuard(const std::string& filename)
31  {
32  std::string localAppDataPath;
33  std::string logsDirectory;
34 
35 #ifdef EEGEO_WIN
36  localAppDataPath = Windows::FileIOHelpers::GetLocalAppDataPath();
37  logsDirectory = "\\Logs";
38 
39  Windows::FileIOHelpers::MakeDirIfNotExists(localAppDataPath + logsDirectory);
40 #endif
41 
42  file.open(localAppDataPath + logsDirectory + "\\" + filename, std::iostream::app | std::iostream::out);
43 
44  Eegeo_ASSERT(file, "File cannot be opened!");
45  }
46 
47  ~ofstreamGuard()
48  {
49  file.close();
50  }
51  };
52 
54  {
55  DebugFileLogger() {}
56  ~DebugFileLogger() {}
57  public:
58 
59  static void Log(const std::string& data, std::ofstream& file)
60  {
61  file << GetTimeStamp();
62  file << "=> ";
63  file << data;
64  file << "\n";
65 
66  file.flush();
67  }
68  };
69 }