SuperTuxKart
log.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2013-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 
20 #ifndef HEADER_LOG_HPP
21 #define HEADER_LOG_HPP
22 
23 #include "utils/synchronised.hpp"
24 
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string>
30 #include <vector>
31 
32 
33 #if defined(__GLIBC__)
34 # define VALIST __gnuc_va_list
35 #else
36 # define VALIST va_list
37 #endif
38 
39 #if defined(_WIN32) && defined(_MSC_VER) && _MSC_VER < 1800
40 # define va_copy(dest, src) dest = src
41 #endif
42 
43 class Log
44 {
45 public:
47  enum LogLevel { LL_DEBUG,
48  LL_VERBOSE,
49  LL_INFO,
50  LL_WARN,
51  LL_ERROR,
52  LL_FATAL
53  };
54 
55 private:
56 
59 
61  static bool m_no_colors;
62 
64  static bool m_console_log;
65 
67  static FILE* m_file_stdout;
68 
70  struct LineInfo
71  {
72  std::string m_line;
73  int m_level;
74  };
75  static Synchronised<std::vector<struct LineInfo> > m_line_buffer;
76 
79  static size_t m_buffer_size;
80 
81  static void setTerminalColor(LogLevel level);
82  static void resetTerminalColor();
83  static void writeLine(const char *line, int level);
84 
85  static void printMessage(int level, const char *component,
86  const char *format, VALIST va_list);
87 
88 public:
89 
90  // ------------------------------------------------------------------------
94 #define LOG(NAME, LEVEL) \
95  static void NAME(const char *component, const char *format, ...) \
96  { \
97  if(LEVEL < m_min_log_level) return; \
98  va_list args; \
99  va_start(args, format); \
100  printMessage(LEVEL, component, format, args); \
101  va_end(args); \
102  \
103  if (LEVEL == LL_FATAL) \
104  { \
105  assert(false); \
106  exit(1); \
107  } \
108  }
109  LOG(verbose, LL_VERBOSE);
110  LOG(debug, LL_DEBUG);
111  LOG(info, LL_INFO);
112  LOG(warn, LL_WARN);
113  LOG(error, LL_ERROR);
114  LOG(fatal, LL_FATAL);
115 
116  static void openOutputFiles(const std::string &logout);
117 
118  static void closeOutputFiles();
119  static void flushBuffers();
120  static void toggleConsoleLog(bool val);
121 
122  // ------------------------------------------------------------------------
125  static void setBufferSize(size_t n) { m_buffer_size = n; }
126  // ------------------------------------------------------------------------
128  static void setLogLevel(int n)
129  {
130  if(n<0 || n>LL_FATAL)
131  {
132  warn("Log", "Log level %d not in range [%d-%d] - ignored.\n",
133  n, LL_VERBOSE, LL_FATAL);
134  return;
135  }
137  } // setLogLevel
138 
139  // ------------------------------------------------------------------------
143  static LogLevel getLogLevel() { return m_min_log_level; }
144  // ------------------------------------------------------------------------
146  static void disableColor()
147  {
148  m_no_colors = true;
149  } // disableColor
150  // ------------------------------------------------------------------------
154  static void setPrefix(const char* prefix);
155 }; // Log
156 #endif
Definition: log.hpp:44
static void setPrefix(const char *prefix)
Sets a prefix to be printed before each line.
Definition: log.cpp:52
static void openOutputFiles(const std::string &logout)
This function opens the files that will contain the output.
Definition: log.cpp:317
static bool m_no_colors
If set this will disable coloring of log messages.
Definition: log.hpp:61
static void closeOutputFiles()
Function to close output files.
Definition: log.cpp:334
static LogLevel getLogLevel()
Returns the log level.
Definition: log.hpp:143
static FILE * m_file_stdout
The file where stdout output will be written.
Definition: log.hpp:67
static size_t m_buffer_size
<0 if no buffered logging is to be used, otherwise this is the maximum number of lines the buffer sho...
Definition: log.hpp:79
static bool m_console_log
If false that logging will only be saved to a file.
Definition: log.hpp:64
LogLevel
The various log levels used in STK.
Definition: log.hpp:47
static void flushBuffers()
Flushes all stored log messages to the various output devices (thread safe).
Definition: log.cpp:300
static void disableColor()
Disable coloring of log messages.
Definition: log.hpp:146
static void setTerminalColor(LogLevel level)
Selects background/foreground colors for the message depending on log level.
Definition: log.cpp:67
static void printMessage(int level, const char *component, const char *format, VALIST va_list)
This actually creates a log message.
Definition: log.cpp:156
static void resetTerminalColor()
Resets the terminal color to the default, and adds a new line (if a new line is added as part of the ...
Definition: log.cpp:132
static LogLevel m_min_log_level
Which message level to print.
Definition: log.hpp:58
static void setBufferSize(size_t n)
Sets the number of lines to buffer.
Definition: log.hpp:125
static void writeLine(const char *line, int level)
Writes the specified line to the various output devices, e.g.
Definition: log.cpp:238
static void setLogLevel(int n)
Defines the minimum log level to be displayed.
Definition: log.hpp:128
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:28
An optional buffer for lines to be output.
Definition: log.hpp:71