OpenXcom  1.0
Open-source clone of the original X-Com
Logger.h
1 #pragma once
2 /*
3  * Copyright 2010-2016 OpenXcom Developers.
4  *
5  * This file is part of OpenXcom.
6  *
7  * OpenXcom is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * OpenXcom is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
19  */
20 #ifdef _MSC_VER
21 #define _CRT_SECURE_NO_WARNINGS
22 #endif
23 #include <sstream>
24 #include <string>
25 #include <stdio.h>
26 #include "CrossPlatform.h"
27 
28 namespace OpenXcom
29 {
30 
35 enum SeverityLevel
36 {
37  LOG_FATAL,
38  LOG_ERROR,
39  LOG_WARNING,
40  LOG_INFO,
41  LOG_DEBUG,
42  LOG_VERBOSE
43 };
44 
51 class Logger
52 {
53 public:
54  Logger();
55  virtual ~Logger();
56  std::ostringstream& get(SeverityLevel level = LOG_INFO);
57 
58  static SeverityLevel& reportingLevel();
59  static std::string& logFile();
60  static std::string toString(SeverityLevel level);
61 protected:
62  std::ostringstream os;
63 private:
64  Logger(const Logger&);
65  Logger& operator =(const Logger&);
66 };
67 
68 inline Logger::Logger()
69 {
70 }
71 
72 inline std::ostringstream& Logger::get(SeverityLevel level)
73 {
74  os << "[" << toString(level) << "]" << "\t";
75  return os;
76 }
77 
78 inline Logger::~Logger()
79 {
80  os << std::endl;
81  std::ostringstream ss;
82  ss << "[" << CrossPlatform::now() << "]" << "\t" << os.str();
83  FILE *file = fopen(logFile().c_str(), "a");
84  if (file)
85  {
86  fprintf(file, "%s", ss.str().c_str());
87  fflush(file);
88  fclose(file);
89  }
90  if (!file || reportingLevel() == LOG_DEBUG || reportingLevel() == LOG_VERBOSE)
91  {
92  fprintf(stderr, "%s", os.str().c_str());
93  fflush(stderr);
94  }
95 }
96 
97 inline SeverityLevel& Logger::reportingLevel()
98 {
99  static SeverityLevel reportingLevel = LOG_DEBUG;
100  return reportingLevel;
101 }
102 
103 inline std::string& Logger::logFile()
104 {
105  static std::string logFile = "openxcom.log";
106  return logFile;
107 }
108 
109 inline std::string Logger::toString(SeverityLevel level)
110 {
111  static const char* const buffer[] = {"FATAL", "ERROR", "WARN", "INFO", "DEBUG", "VERB"};
112  return buffer[level];
113 }
114 
115 #define Log(level) \
116  if (level > Logger::reportingLevel()) ; \
117  else Logger().get(level)
118 
119 }
A basic logging and debugging class, prints output to stdout/files and can capture stack traces of fa...
Definition: Logger.h:51
std::string now()
Generates a timestamp of the current time.
Definition: CrossPlatform.cpp:1008
Definition: BaseInfoState.cpp:40