Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_UTIL_CALLSTACK_H__
00021 #define __CS_UTIL_CALLSTACK_H__
00022
00027 #include "csextern.h"
00028 #include "csutil/csstring.h"
00029
00030
00032 class CS_CRYSTALSPACE_EXPORT csCallStack
00033 {
00034 protected:
00035 int ref_count;
00036
00037 virtual ~csCallStack() {}
00038 public:
00039 csCallStack () : ref_count (1) { }
00040
00041 void IncRef () { ref_count++; }
00042 void DecRef ()
00043 {
00044 ref_count--;
00045 if (ref_count <= 0)
00046 Free ();
00047 }
00048 int GetRefCount () const { return ref_count; }
00049
00053 virtual void Free() = 0;
00054
00056 virtual size_t GetEntryCount () = 0;
00057
00063 virtual bool GetFunctionName (size_t num, char*& str) = 0;
00064 bool GetFunctionName (size_t num, csString& str)
00065 {
00066 char* cstr;
00067 if (GetFunctionName (num, cstr))
00068 {
00069 str = cstr;
00070 free (cstr);
00071 return true;
00072 }
00073 return false;
00074 }
00076
00082 virtual bool GetLineNumber (size_t num, char*& str) = 0;
00083 bool GetLineNumber (size_t num, csString& str)
00084 {
00085 char* cstr;
00086 if (GetLineNumber (num, cstr))
00087 {
00088 str = cstr;
00089 free (cstr);
00090 return true;
00091 }
00092 return false;
00093 }
00095
00100 virtual bool GetParameters (size_t num, char*& str) = 0;
00101 bool GetParameters (size_t num, csString& str)
00102 {
00103 char* cstr;
00104 if (GetParameters (num, cstr))
00105 {
00106 str = cstr;
00107 free (cstr);
00108 return true;
00109 }
00110 return false;
00111 }
00113
00118 void Print (FILE* f = stdout, bool brief = false)
00119 {
00120 for (size_t i = 0; i < GetEntryCount(); i++)
00121 {
00122 char* s;
00123 bool hasFunc = GetFunctionName (i, s);
00124 fprintf (f, "%s", hasFunc ? (const char*)s : "<unknown>");
00125 if (hasFunc) free (s);
00126 if (!brief && (GetLineNumber (i, s)))
00127 {
00128 fprintf (f, " @%s", (const char*)s);
00129 free (s);
00130 }
00131 if (!brief && (GetParameters (i, s)))
00132 {
00133 fprintf (f, " (%s)", (const char*)s);
00134 free (s);
00135 }
00136 fprintf (f, "\n");
00137 }
00138 fflush (f);
00139 }
00145 csString GetEntryAll (size_t i, bool brief = false)
00146 {
00147 csString line;
00148 char* s;
00149 bool hasFunc = GetFunctionName (i, s);
00150 line << (hasFunc ? (const char*)s : "<unknown>");
00151 if (hasFunc) free (s);
00152 if (!brief && GetLineNumber (i, s))
00153 {
00154 line << " @" << s;
00155 free (s);
00156 }
00157 if (!brief && GetParameters (i, s))
00158 {
00159 line << " (" << s << ")";
00160 free (s);
00161 }
00162 return line;
00163 }
00164 };
00165
00167 class CS_CRYSTALSPACE_EXPORT csCallStackHelper
00168 {
00169 public:
00181 static csCallStack* CreateCallStack (int skip = 0, bool fast = false);
00182 };
00183
00184 #endif // __CS_UTIL_CALLSTACK_H__