00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _SOCKETS_Debug_H
00032 #define _SOCKETS_Debug_H
00033
00034 #include "sockets-config.h"
00035 #include "Utility.h"
00036 #include <stdio.h>
00037
00038
00039 #ifdef SOCKETS_NAMESPACE
00040 namespace SOCKETS_NAMESPACE {
00041 #endif
00042
00043
00044 class Debug
00045 {
00046 static const char *colors[];
00047 public:
00048 class endl {
00049 public:
00050 endl() {}
00051 virtual ~endl() {}
00052 };
00053
00054 public:
00055 Debug() {}
00056 Debug(const std::string& x) : m_id(0), m_text(x) {
00057 fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
00058 for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
00059 fprintf(stderr, " ");
00060 fprintf(stderr, "%s%s\n", x.c_str(), colors[0]);
00061 m_level[Utility::ThreadID()]++;
00062 }
00063 Debug(int id, const std::string& x) : m_id(id), m_text(x) {
00064 fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
00065 for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
00066 fprintf(stderr, " ");
00067 fprintf(stderr, "%d> %s%s\n", m_id, x.c_str(), colors[0]);
00068 m_level[Utility::ThreadID()]++;
00069 }
00070 ~Debug() {
00071 if (!m_text.empty())
00072 {
00073 if (m_level[Utility::ThreadID()])
00074 m_level[Utility::ThreadID()]--;
00075 fprintf(stderr, "%s", colors[Utility::ThreadID() % 14 + 1]);
00076 for (int i = 0; i < m_level[Utility::ThreadID()]; i++)
00077 fprintf(stderr, " ");
00078 if (m_id)
00079 fprintf(stderr, "%d> /%s%s\n", m_id, m_text.c_str(), colors[0]);
00080 else
00081 fprintf(stderr, "/%s%s\n", m_text.c_str(), colors[0]);
00082 fflush(stderr);
00083 }
00084 }
00085 static void Print(const char *format, ...);
00086
00087 Debug& operator<<(const char *);
00088 Debug& operator<<(const std::string& );
00089 Debug& operator<<(short);
00090 Debug& operator<<(int);
00091 Debug& operator<<(long);
00092 Debug& operator<<(double);
00093 Debug& operator<<(endl);
00094
00095 private:
00096 int m_id;
00097 std::string m_text;
00098 static std::map<unsigned long, int> m_level;
00099 std::string m_line;
00100 };
00101
00102
00103 #ifdef SOCKETS_NAMESPACE
00104 }
00105 #endif
00106
00107 #endif // _SOCKETS_Debug_H
00108