kviewshell
debug.cpp
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
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063
00064 #include "debug.h"
00065
00066 #if ( DEBUGLVL > 0 )
00067
00068 #include "GThreads.h"
00069 #include "GContainer.h"
00070 #include "GString.h"
00071 #include "GString.h"
00072 #include "ByteStream.h"
00073 #include "GURL.h"
00074
00075 #include <stdarg.h>
00076 #include <stdio.h>
00077 #include <errno.h>
00078
00079 #ifdef HAVE_NAMESPACES
00080 namespace DJVU {
00081 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00082 }
00083 #endif
00084 #endif
00085
00086
00087 #ifndef UNIX
00088 #ifndef WIN32
00089 #ifndef macintosh
00090 #define UNIX
00091 #endif
00092 #endif
00093 #endif
00094
00095 static GCriticalSection debug_lock;
00096 #ifdef RUNTIME_DEBUG_ONLY
00097 static int debug_level = 0;
00098 #else
00099 static int debug_level = DEBUGLVL;
00100 #endif
00101 static int debug_id;
00102 static FILE *debug_file;
00103 static int debug_file_count;
00104
00105 #if THREADMODEL==NOTHREADS
00106 static DjVuDebug debug_obj;
00107 #else
00108 static GMap<long, DjVuDebug> &
00109 debug_map(void)
00110 {
00111 static GMap<long, DjVuDebug> xmap;
00112 return xmap;
00113 }
00114 #endif
00115
00116 DjVuDebug::DjVuDebug()
00117 : block(0), indent(0)
00118 {
00119 id = debug_id++;
00120 #ifdef UNIX
00121 if (debug_file_count++ == 0 && !debug_file)
00122 set_debug_file(stderr);
00123 #endif
00124 }
00125
00126 DjVuDebug::~DjVuDebug()
00127 {
00128 #ifdef UNIX
00129 if (--debug_file_count == 0)
00130 {
00131 if (debug_file && (debug_file != stderr))
00132 fclose(debug_file);
00133 debug_file = 0;
00134 }
00135 #endif
00136 }
00137
00138 void
00139 DjVuDebug::format(const char *fmt, ... )
00140 {
00141 if (! block)
00142 {
00143 va_list ap;
00144 va_start(ap, fmt);
00145 GUTF8String buffer(fmt,ap);
00146 va_end(ap);
00147 GCriticalSectionLock glock(&debug_lock);
00148 if (debug_file)
00149 {
00150 fprintf(debug_file,"%s", (const char*)buffer);
00151 fflush(debug_file);
00152 }
00153 #ifdef WIN32
00154 else
00155 {
00156 USES_CONVERSION;
00157 OutputDebugString(A2CT((const char *)buffer));
00158 }
00159 #endif
00160 }
00161 }
00162
00163 void
00164 DjVuDebug::set_debug_level(int lvl)
00165 {
00166 debug_level = lvl;
00167 }
00168
00169 void
00170 DjVuDebug::set_debug_file(FILE * file)
00171 {
00172 GCriticalSectionLock glock(&debug_lock);
00173 if (debug_file && (debug_file != stderr))
00174 fclose(debug_file);
00175 debug_file = file;
00176 }
00177
00178 void
00179 DjVuDebug::modify_indent(int rindent)
00180 {
00181 indent += rindent;
00182 }
00183
00184 DjVuDebug&
00185 DjVuDebug::lock(int lvl, int noindent)
00186 {
00187 int threads_num=1;
00188 debug_lock.lock();
00189
00190 #if THREADMODEL==NOTHREADS
00191
00192 DjVuDebug &dbg = debug_obj;
00193 #else
00194
00195 long threadid = (long) GThread::current();
00196 DjVuDebug &dbg = debug_map()[threadid];
00197 threads_num=debug_map().size();
00198 #endif
00199
00200 dbg.block = (lvl > debug_level);
00201
00202 if (! noindent)
00203 {
00204 if (threads_num>1)
00205 dbg.format("[T%d] ", dbg.id);
00206 int ind = dbg.indent;
00207 char buffer[257];
00208 memset(buffer,' ', sizeof(buffer)-1);
00209 buffer[sizeof(buffer)-1] = 0;
00210 while (ind > (int)sizeof(buffer)-1)
00211 {
00212 dbg.format("%s", buffer);
00213 ind -= sizeof(buffer)-1;
00214 }
00215 if (ind > 0)
00216 {
00217 buffer[ind] = 0;
00218 dbg.format("%s", buffer);
00219 }
00220 }
00221
00222 return dbg;
00223 }
00224
00225 void
00226 DjVuDebug::unlock()
00227 {
00228 debug_lock.unlock();
00229 }
00230
00231 #define OP(type, fmt) \
00232 DjVuDebug& DjVuDebug::operator<<(type arg)\
00233 { format(fmt, arg); return *this; }
00234
00235 DjVuDebug& DjVuDebug::operator<<(bool arg)
00236 {
00237 format("%s", arg ? "TRUE" : "FALSE"); return *this;
00238 }
00239
00240 OP(char, "%c")
00241 OP(unsigned char, "%c")
00242 OP(int, "%d")
00243 OP(unsigned int, "%u")
00244 OP(short int, "%hd")
00245 OP(unsigned short int, "%hu")
00246 OP(long, "%ld")
00247 OP(unsigned long, "%lu")
00248 OP(float, "%g")
00249 OP(double, "%g")
00250 OP(const void * const, "0x%08x")
00251
00252 DjVuDebug& DjVuDebug::operator<<(const char * const ptr)
00253 {
00254 GUTF8String buffer(ptr?ptr:"(null)");
00255 if(buffer.length() > 255)
00256 {
00257 buffer=buffer.substr(0,252)+"...";
00258 }
00259 format("%s", (const char *)buffer);
00260 return *this;
00261 }
00262
00263 DjVuDebug& DjVuDebug::operator<<(const unsigned char * const ptr)
00264 {
00265 return operator<<( (const char *) ptr );
00266 }
00267
00268 DjVuDebug& DjVuDebug::operator<<(const GUTF8String &ptr)
00269 {
00270 GUTF8String buffer(ptr);
00271 if(buffer.length() > 255)
00272 buffer=buffer.substr(0,252)+"...";
00273 format("%s", (const char *)buffer);
00274 return *this;
00275 }
00276
00277 DjVuDebugIndent::DjVuDebugIndent(int inc)
00278 : inc(inc)
00279 {
00280 DjVuDebug &dbg = DjVuDebug::lock(0,1);
00281 dbg.modify_indent(inc);
00282 dbg.unlock();
00283 }
00284
00285 DjVuDebugIndent::~DjVuDebugIndent()
00286 {
00287 DjVuDebug &dbg = DjVuDebug::lock(0,1);
00288 dbg.modify_indent(-inc);
00289 dbg.unlock();
00290 }
00291
00292
00293 #ifdef HAVE_NAMESPACES
00294 }
00295 # ifndef NOT_USING_DJVU_NAMESPACE
00296 using namespace DJVU;
00297 # endif
00298 #endif
00299 #endif