Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Logging.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 
6 
8 #include "inexor/network/SharedVar.hpp" // for SharedVar
9 
10 #include <spdlog/spdlog.h>
11 #include <spdlog/fmt/ostr.h>
12 
13 #undef LOG_INFO //conflicting between spdlog and cef
14 #undef LOG_WARNING
15 
16 #include <array>
17 #include <iomanip>
18 #include <map>
19 #include <memory> // for shared_ptr
20 #include <ostream> // for operator<<
21 #include <string> // for string
22 #include <vector>
23 
24 
25 
26 namespace inexor {
27 namespace util {
28 
29 #define DEFAULT_LOG_PATTERN "%H:%M:%S [%n] [%l] %v"
30 
33  class Logger
34  {
35  public:
36  std::shared_ptr<spdlog::logger> spdlog_logger;
38  SharedVar<int> level; // TODO: ranges min max value
39 
40  Logger() : pattern((char*)DEFAULT_LOG_PATTERN), level(2) // spdlog::level::info
41  {
43  pattern.onChange.connect([this](char *const &old_pattern, char *const &new_pattern)
44  {
45  try
46  {
47  spdlog_logger->set_pattern(new_pattern);
48  }
49  catch(const spdlog::spdlog_ex& ex) {}
50  });
51  level.onChange.connect([this](const int &old_level, const int &new_level)
52  {
53  try
54  {
55  spdlog_logger->set_level(static_cast<spdlog::level::level_enum>(new_level));
56  }
57  catch(const spdlog::spdlog_ex& ex) {}
58  });
59  }
60  Logger(const Logger &old) : spdlog_logger(old.spdlog_logger), pattern(old.pattern), level(old.level) {}
61 
62  const std::shared_ptr<spdlog::logger> operator->() const
63  {
64  return spdlog_logger;
65  };
66 
67  void create_spdlog_logger(const std::string logger_name);
68 
71  };
72 
76  {
77  private:
81 
82  public:
83  log_manager();
84 
87 
89  static Logger std;
90 
93 
96  static Logger sync;
97 
100  static Logger io;
101 
104  static Logger util;
105 
107  static Logger render;
108 
110  static Logger ui;
111 
114  static Logger game;
115 
118 
121 
123  static Logger chat;
124 
127 
130  static Logger world;
131 
135  static Logger edit;
136 
139  {
140  return std;
141  };
142 
143  static Logger create_and_register_logger(std::string logger_name,
144  const std::string pattern = DEFAULT_LOG_PATTERN,
145  int level = spdlog::level::info);
146  };
147 
148 
150 
151 
152  // ANSI Console colors: everything behind it will be in that particular color.
153  // \x1b[38;2; is the ANSI escape sequence for 24bit foreground color, then the RGB values follow (+ closing 'm').
154  #define COL_GREEN "\x1b[38;2;64;255;128m" // == legacy \f0
155  #define COL_BLUE "\x1b[38;2;96;160;255m" // == legacy \f1
156  #define COL_YELLOW "\x1b[38;2;255;192;64m" // == legacy \f2
157  #define COL_RED "\x1b[38;2;255;64;64m" // == legacy \f3
158  #define COL_GREY "\x1b[38;2;128;128;128m" // == legacy \f4
159  #define COL_MAGENTA "\x1b[38;2;192;64;192m" // == legacy \f5
160  #define COL_ORANGE "\x1b[38;2;255;128;0m" // == legacy \f6
161  #define COL_WHITE "\x1b[38;2;255;255;255m" // == legacy \f7
162 
165  struct embraced
166  {
168  const char *_leading, // e.g. "{"
169  *_trailing; // e.g. "}"
170 
171  embraced(const char *text, const char *leading, const char *trailing) : _text(text), _leading(leading), _trailing(trailing) {}
172 
174  template<typename T>
175  embraced(T number, const char *leading, const char *trailing) : _text(std::to_string(number)), _leading(leading), _trailing(trailing) {}
176 
178  operator std::string() const
179  {
180  std::string quotedStr = _leading;
181  quotedStr += _text;
182  quotedStr += _trailing;
183  return quotedStr;
184  }
185 
187  friend std::ostream &operator<< (std::ostream & ostr, const embraced & q)
188  {
189  ostr << q._leading << q._text << q._trailing;
190  return ostr;
191  }
192  };
193 
196  struct quoted : embraced
197  {
198  quoted(const char *text) : embraced(text, "\"", "\"") {}
199  };
200 } } // ns inexor::util
201 
203 
static Logger world
Logger for world related functions.
Definition: Logging.hpp:130
friend std::ostream & operator<<(std::ostream &ostr, const embraced &q)
Pass this class directly into a stream.
Definition: Logging.hpp:187
void all_loggers_apply_sinks_change()
After adding a sink we need to recreate all loggers to make them use them.
Definition: Logging.cpp:194
The global inexor logging API TODO: enable sharing.
Definition: Logging.hpp:75
SharedVar< int > level
Definition: Logging.hpp:38
static Logger frag_not_involved
Logger for "player1 killed playerC".
Definition: Logging.hpp:120
SharedVar< char * > logfile
The absolute path of the logfile which gets created as sink.
Definition: Logging.hpp:86
static Logger team_chat
Logger for team chat messages.
Definition: Logging.hpp:126
static Logger io
Logger for the filesystem messages and other input/output.
Definition: Logging.hpp:100
static Logger std
Logger for everything not fitting elsewhere.
Definition: Logging.hpp:89
SharedVar< char * > pattern
Definition: Logging.hpp:37
Helper class to allow the writing of std::cout << embraced("I should be inside curly brackets"...
Definition: Logging.hpp:165
static Logger start_stop
Logger for the startup and the shut down of the game.
Definition: Logging.hpp:92
quoted(const char *text)
Definition: Logging.hpp:198
std::shared_ptr< spdlog::logger > spdlog_logger
Definition: Logging.hpp:36
void create_spdlog_logger(const std::string logger_name)
Definition: Logging.cpp:155
static Logger sync
Logger for synchronization messages.
Definition: Logging.hpp:96
Wrapper around spdlog::logger to put it into the InexorTree We want to set the pattern and the level ...
Definition: Logging.hpp:33
static Logger ui
Logger for the user interface rendering (CEF bindings).
Definition: Logging.hpp:110
A simple helper class to allow you writing inexor::util::quoted(string) which automatically adds quot...
Definition: Logging.hpp:196
static Logger frag_involved
Logger for "you got killed" or "you killed xy".
Definition: Logging.hpp:117
void recreate_spdlog_logger()
Wrapper for create_spdlog_logger, taking the current logger_name as argument.
Definition: Logging.cpp:166
static Logger edit
Logger for messages of the editing mode.
Definition: Logging.hpp:135
const std::shared_ptr< spdlog::logger > operator->() const
Definition: Logging.hpp:62
static Logger util
Logger for utility functions.
Definition: Logging.hpp:104
const std::string _text
Definition: Logging.hpp:167
log_manager()
Definition: Logging.cpp:172
Logger()
Definition: Logging.hpp:40
inexor::util::log_manager Log
Definition: Logging.cpp:241
Logger(const Logger &old)
Definition: Logging.hpp:60
std::string to_string(T x)
Convert stuff to a string.
Definition: util.hpp:16
static Logger render
Logger for rendering messages.
Definition: Logging.hpp:107
const char * _trailing
Definition: Logging.hpp:168
static Logger create_and_register_logger(std::string logger_name, const std::string pattern=DEFAULT_LOG_PATTERN, int level=spdlog::level::info)
Register logger to spdlog or replace it if the name is already taken.
Definition: Logging.cpp:213
const char * _leading
Definition: Logging.hpp:168
Logger & operator->() const
We allow the syntax Log.info("hallo") and forward it to the logger log_manager::default.
Definition: Logging.hpp:138
char string[MAXSTRLEN]
cube-strings (char arrays of fixed size).
Definition: cube_types.hpp:18
GLenum GLenum GLuint GLint level
Definition: glexts.hpp:296
SharedVar wrapper for primitive/immutable objects.
Definition: SharedVar.hpp:55
signal< void(const T &, const T &)> onChange
Event that is fired when a new value is assigned;.
Definition: SharedVar.hpp:71
static Logger chat
Logger for chat messages.
Definition: Logging.hpp:123
embraced(T number, const char *leading, const char *trailing)
Also allow all kind of number formats beeing embraced:
Definition: Logging.hpp:175
#define DEFAULT_LOG_PATTERN
Definition: Logging.hpp:29
embraced(const char *text, const char *leading, const char *trailing)
Definition: Logging.hpp:171
static Logger game
Logger for all general game messages.
Definition: Logging.hpp:114