00001 #ifndef _SOCKETS_Debug_H
00002 #define _SOCKETS_Debug_H
00003
00004 #include "sockets-config.h"
00005 #include <string>
00006 #include "Utility.h"
00007 #include <map>
00008
00009
00010 #ifdef SOCKETS_NAMESPACE
00011 namespace SOCKETS_NAMESPACE {
00012 #endif
00013
00014
00015 class Debug
00016 {
00017 static const char *colors[];
00018 public:
00019 class endl {
00020 public:
00021 endl() {}
00022 virtual ~endl() {}
00023 };
00024
00025 public:
00026 Debug() {}
00027 Debug(const std::string& x) : m_id(0), m_text(x) {
00028 fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
00029 for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
00030 fprintf(stderr, " ");
00031 fprintf(stderr, "%s%s\n", x.c_str(), colors[0]);
00032 m_level[Utility::ThreadID()]++;
00033 }
00034 Debug(int id, const std::string& x) : m_id(id), m_text(x) {
00035 fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
00036 for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
00037 fprintf(stderr, " ");
00038 fprintf(stderr, "%d> %s%s\n", m_id, x.c_str(), colors[0]);
00039 m_level[Utility::ThreadID()]++;
00040 }
00041 ~Debug() {
00042 if (!m_text.empty())
00043 {
00044 if (m_level[Utility::ThreadID()])
00045 m_level[Utility::ThreadID()]--;
00046 fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
00047 for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
00048 fprintf(stderr, " ");
00049 if (m_id)
00050 fprintf(stderr, "%d> /%s%s\n", m_id, m_text.c_str(), colors[0]);
00051 else
00052 fprintf(stderr, "/%s%s\n", m_text.c_str(), colors[0]);
00053 fflush(stderr);
00054 }
00055 }
00056 static void Print(const char *format, ...);
00057
00058 Debug& operator<<(const std::string& );
00059 Debug& operator<<(long);
00060 Debug& operator<<(endl);
00061
00062 private:
00063 int m_id;
00064 std::string m_text;
00065 static std::map<unsigned long, int> m_level;
00066 std::string m_line;
00067 };
00068
00069
00070 #ifdef SOCKETS_NAMESPACE
00071 }
00072 #endif
00073
00074 #endif // _SOCKETS_Debug_H
00075