OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
debugging.hpp
Go to the documentation of this file.
1 #ifndef DEBUG_DEBUGGING_H
2 #define DEBUG_DEBUGGING_H
3 
4 #include <boost/filesystem/fstream.hpp>
5 #include <boost/iostreams/stream.hpp>
6 
8 
9 #include <SDL_messagebox.h>
10 
11 #include "debuglog.hpp"
12 
13 namespace Debug
14 {
15  // ANSI colors for terminal
16  enum Color
17  {
18  Reset = 0,
19  DarkGray = 90,
20  Red = 91,
21  Yellow = 93
22  };
23 
25  {
26  public:
28  {
31  }
32 
33  virtual std::streamsize write(const char *str, std::streamsize size);
34 
35  protected:
36  static Level getLevelMarker(const char *str);
37 
38  static void fillCurrentDebugLevel();
39 
40  virtual std::streamsize writeImpl(const char *str, std::streamsize size, Level debugLevel)
41  {
42  return size;
43  }
44  };
45 
46 #if defined(_WIN32) && defined(_DEBUG)
47  class DebugOutput : public DebugOutputBase
48  {
49  public:
50  std::streamsize writeImpl(const char *str, std::streamsize size, Level debugLevel)
51  {
52  // Make a copy for null termination
53  std::string tmp (str, static_cast<unsigned int>(size));
54  // Write string to Visual Studio Debug output
55  OutputDebugString (tmp.c_str ());
56  return size;
57  }
58 
59  virtual ~DebugOutput() {}
60  };
61 #else
62 
63  class Tee : public DebugOutputBase
64  {
65  public:
66  Tee(std::ostream &stream, std::ostream &stream2)
67  : out(stream), out2(stream2)
68  {
69  // TODO: check which stream is stderr?
71 
72  mColors[Error] = Red;
74  mColors[Info] = Reset;
77  }
78 
79  virtual std::streamsize writeImpl(const char *str, std::streamsize size, Level debugLevel)
80  {
81  out.write (str, size);
82  out.flush();
83 
84  if(mUseColor)
85  {
86  out2 << "\033[0;" << mColors[debugLevel] << "m";
87  out2.write (str, size);
88  out2 << "\033[0;" << Reset << "m";
89  }
90  else
91  {
92  out2.write(str, size);
93  }
94  out2.flush();
95 
96  return size;
97  }
98 
99  virtual ~Tee() {}
100 
101  private:
102 
103  static bool useColoredOutput()
104  {
105  // Note: cmd.exe in Win10 should support ANSI colors, but in its own way.
106 #if defined(_WIN32)
107  return 0;
108 #else
109  char *term = getenv("TERM");
110  bool useColor = term && !getenv("NO_COLOR") && isatty(fileno(stderr));
111 
112  return useColor;
113 #endif
114  }
115 
116  std::ostream &out;
117  std::ostream &out2;
118  bool mUseColor;
119 
120  std::map<Level, int> mColors;
121  };
122 #endif
123 }
124 
125 int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[], const std::string& appName);
126 
127 #endif
Definition: debugging.hpp:19
bool mUseColor
Definition: debugging.hpp:118
Definition: debuglog.hpp:12
Definition: debuglog.hpp:13
Definition: debugging.hpp:20
Definition: debugging.hpp:24
virtual std::streamsize writeImpl(const char *str, std::streamsize size, Level debugLevel)
Definition: debugging.hpp:40
static void fillCurrentDebugLevel()
Definition: debugging.cpp:31
Level
Definition: debuglog.hpp:9
Definition: debugging.hpp:18
Definition: debuglog.hpp:14
virtual std::streamsize write(const char *str, std::streamsize size)
Definition: debugging.cpp:7
DebugOutputBase()
Definition: debugging.hpp:27
std::ostream & out
Definition: debugging.hpp:116
Level CurrentDebugLevel
Definition: debuglog.cpp:5
std::ostream & out2
Definition: debugging.hpp:117
Definition: debuglog.hpp:15
virtual std::streamsize writeImpl(const char *str, std::streamsize size, Level debugLevel)
Definition: debugging.hpp:79
Definition: debuglog.hpp:11
int stderr
Definition: android_main.c:1
Tee(std::ostream &stream, std::ostream &stream2)
Definition: debugging.hpp:66
virtual ~Tee()
Definition: debugging.hpp:99
static Level getLevelMarker(const char *str)
Definition: debugging.cpp:21
static bool useColoredOutput()
Definition: debugging.hpp:103
Definition: debugging.hpp:21
Definition: debugging.hpp:63
Color
Definition: debugging.hpp:16
int wrapApplication(int(*innerApplication)(int argc, char *argv[]), int argc, char *argv[], const std::string &appName)
Definition: debugging.cpp:53
std::map< Level, int > mColors
Definition: debugging.hpp:120