OpenXcom  1.0
Open-source clone of the original X-Com
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Logger.h
1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef OPENXCOM_LOGGER_H
20 #define OPENXCOM_LOGGER_H
21 
22 #include <sstream>
23 #include <string>
24 #include <stdio.h>
25 #ifdef _WIN32
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
28 // the following macros interfere with std::max and std::min as used throughout...
29 #undef min
30 #undef max
31 #ifndef LOCALE_INVARIANT
32 #define LOCALE_INVARIANT 0x007f
33 #endif
34 #else
35 #include <time.h>
36 #endif
37 
38 namespace OpenXcom
39 {
40 
41 inline std::string now();
42 
48 {
54 };
55 
62 class Logger
63 {
64 public:
65  Logger();
66  virtual ~Logger();
67  std::ostringstream& get(SeverityLevel level = LOG_INFO);
68 
69  static SeverityLevel& reportingLevel();
70  static std::string& logFile();
71  static std::string toString(SeverityLevel level);
72 protected:
73  std::ostringstream os;
74 private:
75  Logger(const Logger&);
76  Logger& operator =(const Logger&);
77 };
78 
79 inline Logger::Logger()
80 {
81 }
82 
83 inline std::ostringstream& Logger::get(SeverityLevel level)
84 {
85  os << "[" << toString(level) << "]" << "\t";
86  return os;
87 }
88 
89 inline Logger::~Logger()
90 {
91  os << std::endl;
92  if (reportingLevel() == LOG_DEBUG)
93  {
94  fprintf(stderr, "%s", os.str().c_str());
95  fflush(stderr);
96  }
97  std::ostringstream ss;
98  ss << "[" << now() << "]" << "\t" << os.str();
99  FILE *file = fopen(logFile().c_str(), "a");
100  fprintf(file, "%s", ss.str().c_str());
101  fflush(file);
102  fclose(file);
103 }
104 
105 inline SeverityLevel& Logger::reportingLevel()
106 {
107  static SeverityLevel reportingLevel = LOG_DEBUG;
108  return reportingLevel;
109 }
110 
111 inline std::string& Logger::logFile()
112 {
113  static std::string logFile = "openxcom.log";
114  return logFile;
115 }
116 
117 inline std::string Logger::toString(SeverityLevel level)
118 {
119  static const char* const buffer[] = {"FATAL", "ERROR", "WARN", "INFO", "DEBUG"};
120  return buffer[level];
121 }
122 
123 #define Log(level) \
124  if (level > Logger::reportingLevel()) ; \
125  else Logger().get(level)
126 
127 inline std::string now()
128 {
129  const int MAX_LEN = 25, MAX_RESULT = 80;
130 #ifdef _WIN32
131  char date[MAX_LEN], time[MAX_LEN];
132  if (GetDateFormatA(LOCALE_INVARIANT, 0, 0,
133  "dd'-'MM'-'yyyy", date, MAX_LEN) == 0)
134  return "Error in Now()";
135  if (GetTimeFormatA(LOCALE_INVARIANT, TIME_FORCE24HOURFORMAT, 0,
136  "HH':'mm':'ss", time, MAX_LEN) == 0)
137  return "Error in Now()";
138 
139  char result[MAX_RESULT] = {0};
140  sprintf(result, "%s %s", date, time);
141 #else
142  char buffer[MAX_LEN];
143  time_t rawtime;
144  struct tm *timeinfo;
145  time(&rawtime);
146  timeinfo = localtime(&rawtime);
147  strftime(buffer, MAX_LEN, "%d-%m-%Y %H:%M:%S", timeinfo);
148  char result[MAX_RESULT] = {0};
149  sprintf(result, "%s", buffer);
150 #endif
151  return result;
152 }
153 
154 }
155 
156 #endif
SeverityLevel
Defines the various severity levels of information logged by the game.
Definition: Logger.h:47
Something horrible has happened and the game is going to die!
Definition: Logger.h:49
Something weird happened, nothing special but it's good to know.
Definition: Logger.h:51
Something bad happened but we can still move on.
Definition: Logger.h:50
A basic logging and debugging class, prints output to stdout/files and can capture stack traces of fa...
Definition: Logger.h:62
Purely test stuff to help developers implement, not really relevant to users.
Definition: Logger.h:53
Useful information for users/developers to help debug and figure stuff out.
Definition: Logger.h:52