-
Notifications
You must be signed in to change notification settings - Fork 4
/
Debug.cpp
133 lines (103 loc) · 2.9 KB
/
Debug.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
** \file Debug.cpp
** \date 2011-08-16
** \author grymse@alhem.net
**/
/*
Copyright (C) 2011 Anders Hedstrom
This library is made available under the terms of the GNU GPL.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "Debug.h"
#include <stdarg.h>
#include <cstring>
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
std::map<unsigned long, int> Debug::m_level;
const char *Debug::colors[] = {
"\x1B[0;0m", // &n */
"\x1B[0;0m\x1B[31m", // &r */
"\x1B[0;0m\x1B[32m", // &g */
"\x1B[0;0m\x1B[33m", // &y */
"\x1B[0;0m\x1B[34m", // &b */
"\x1B[0;0m\x1B[35m", // &m */
"\x1B[0;0m\x1B[36m", // &c */
"\x1B[0;0m\x1B[37m", // &w */
"\x1B[1;31m", // &R */
"\x1B[1;32m", // &G */
"\x1B[1;33m", // &Y */
"\x1B[1;34m", // &B */
"\x1B[1;35m", // &M */
"\x1B[1;36m", // &C */
"\x1B[1;37m" }; // &W */
void Debug::Print(const char *format, ...)
{
char slask[5000]; // temporary for vsprintf / vsnprintf
va_list ap;
va_start(ap, format);
vsnprintf(slask, sizeof(slask), format, ap);
va_end(ap);
fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
fprintf(stderr, " ");
if (slask[strlen(slask) - 1] == '\n')
slask[strlen(slask) - 1] = 0;
fprintf(stderr, "%s%s\n", slask, colors[0]);
}
Debug& Debug::operator<<(const char * str)
{
m_line += str;
return *this;
}
Debug& Debug::operator<<(const std::string& str)
{
m_line += str;
return *this;
}
Debug& Debug::operator<<(short l)
{
m_line += Utility::l2string(l);
return *this;
}
Debug& Debug::operator<<(int l)
{
m_line += Utility::l2string(l);
return *this;
}
Debug& Debug::operator<<(long l)
{
m_line += Utility::l2string(l);
return *this;
}
Debug& Debug::operator<<(double d)
{
char slask[100];
snprintf(slask, sizeof(slask), "%f", d);
m_line += slask;
return *this;
}
Debug& Debug::operator<<(endl)
{
Print("%s", m_line.c_str());
m_line = "";
return *this;
}
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif