GameKit  0.0.1a
C++ gamedev tools
Debug.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Debug.hpp
5  *
6  * Description:
7  *
8  * Created: 14/09/2014 23:50:22
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_DEBUG_HPP_
15 #define GK_DEBUG_HPP_
16 
17 #include <cstring>
18 #include <iostream>
19 #include <sstream>
20 #include <string>
21 #include <vector>
22 
23 #include "gk/core/IntTypes.hpp"
24 
25 #define _FILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
26 
27 #ifdef DEBUG_ENABLED
28  #define DEBUG(args...) { std::cout << gk::Debug::textColor(gk::Debug::TextColor::Red, true) << _FILE << ":" << __LINE__ << ":" << gk::Debug::textColor(); gk::Debug::print(args); }
29  #define TRACE(s) { DEBUG("Function called: " #s); s }
30 #else
31  #define DEBUG(args...) {}
32  #define TRACE(s) { s }
33 #endif
34 
35 namespace gk {
36 
37 namespace Debug {
38  enum TextColor {
39  White = 0,
40  Red = 31,
41  Blue = 36
42  };
43 
44  inline std::string textColor(u8 color = TextColor::White, bool bold = false) {
45 #ifdef DEBUG_COLOR
46  return std::string("\33[0;") + ((color < 10) ? "0" : "") + std::to_string(color) + ";0" + ((bold) ? "1" : "0") + "m";
47 #else
48  return std::string("");
49 #endif
50  }
51 
52  template<typename... Args>
53  std::string makeString(Args &&...args) {
54  std::ostringstream stream;
55  std::vector<int> tmp{0, ((void)(stream << args << " "), 0)...};
56 
57  return stream.str();
58  }
59 
60  template<typename... Args>
61  void print(Args &&...args) {
62  std::cerr << makeString(std::forward<Args>(args)...) << std::endl;
63  }
64 }
65 
66 } // namespace gk
67 
68 #endif // GK_DEBUG_HPP_
std::string makeString(Args &&...args)
Definition: Debug.hpp:53
std::string textColor(u8 color=TextColor::White, bool bold=false)
Definition: Debug.hpp:44
unsigned char u8
Definition: IntTypes.hpp:21
void print(Args &&...args)
Definition: Debug.hpp:61