summaryrefslogtreecommitdiff
path: root/src/Sockets/Debug.h
blob: 9bc06b9364c156c4ffcf069cc407b0b911da803c (plain) (blame)
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
#ifndef _SOCKETS_Debug_H
#define _SOCKETS_Debug_H

#include "sockets-config.h"
#include <string>
#include "Utility.h"
#include <map>


#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif


class Debug
{
static	const char *colors[];
public:
	class endl {
	public:
		endl() {}
		virtual ~endl() {}
	};

public:
	Debug() {}
	Debug(const std::string& x) : m_id(0), m_text(x) {
		fprintf(stderr, "%s", colors[Utility::ThreadID() % 16 + 1]);
		for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
			fprintf(stderr, "  ");
		fprintf(stderr, "%s%s\n", x.c_str(), colors[0]);
		m_level[Utility::ThreadID()]++;
	}
	Debug(int id, const std::string& x) : m_id(id), m_text(x) {
		fprintf(stderr, "%s", colors[Utility::ThreadID() % 16 + 1]);
		for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
			fprintf(stderr, "  ");
		fprintf(stderr, "%d> %s%s\n", m_id, x.c_str(), colors[0]);
		m_level[Utility::ThreadID()]++;
	}
	~Debug() {
		if (!m_text.empty())
		{
			if (m_level[Utility::ThreadID()])
				m_level[Utility::ThreadID()]--;
			fprintf(stderr, "%s", colors[Utility::ThreadID() % 16 + 1]);
			for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
				fprintf(stderr, "  ");
			if (m_id)
				fprintf(stderr, "%d> /%s%s\n", m_id, m_text.c_str(), colors[0]);
			else
				fprintf(stderr, "/%s%s\n", m_text.c_str(), colors[0]);
			fflush(stderr);
		}
	}
static	void Print(const char *format, ...);

	Debug& operator<<(const std::string& );
	Debug& operator<<(long);
	Debug& operator<<(endl);

private:
	int m_id;
	std::string m_text;
static	std::map<unsigned long, int> m_level;
	std::string m_line;
};


#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif

#endif // _SOCKETS_Debug_H