• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kviewshell

debug.cpp

Go to the documentation of this file.
00001 //C-  -*- C++ -*-
00002 //C- -------------------------------------------------------------------
00003 //C- DjVuLibre-3.5
00004 //C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
00005 //C- Copyright (c) 2001  AT&T
00006 //C-
00007 //C- This software is subject to, and may be distributed under, the
00008 //C- GNU General Public License, Version 2. The license should have
00009 //C- accompanied the software or you may obtain a copy of the license
00010 //C- from the Free Software Foundation at http://www.fsf.org .
00011 //C-
00012 //C- This program is distributed in the hope that it will be useful,
00013 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //C- GNU General Public License for more details.
00016 //C- 
00017 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
00018 //C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
00019 //C- Software authorized us to replace the original DjVu(r) Reference 
00020 //C- Library notice by the following text (see doc/lizard2002.djvu):
00021 //C-
00022 //C-  ------------------------------------------------------------------
00023 //C- | DjVu (r) Reference Library (v. 3.5)
00024 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
00025 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
00026 //C- | 6,058,214 and patents pending.
00027 //C- |
00028 //C- | This software is subject to, and may be distributed under, the
00029 //C- | GNU General Public License, Version 2. The license should have
00030 //C- | accompanied the software or you may obtain a copy of the license
00031 //C- | from the Free Software Foundation at http://www.fsf.org .
00032 //C- |
00033 //C- | The computer code originally released by LizardTech under this
00034 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
00035 //C- | ORIGINAL CODE."  Subject to any third party intellectual property
00036 //C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
00037 //C- | non-exclusive license to make, use, sell, or otherwise dispose of 
00038 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
00039 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
00040 //C- | General Public License.   This grant only confers the right to 
00041 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
00042 //C- | the extent such infringement is reasonably necessary to enable 
00043 //C- | recipient to make, have made, practice, sell, or otherwise dispose 
00044 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
00045 //C- | any greater extent that may be necessary to utilize further 
00046 //C- | modifications or combinations.
00047 //C- |
00048 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
00049 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00050 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
00051 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00052 //C- +------------------------------------------------------------------
00053 // 
00054 // $Id: debug.cpp,v 1.14 2005/05/27 14:26:00 leonb Exp $
00055 // $Name: release_3_5_15 $
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   // Find Debug object
00190 #if THREADMODEL==NOTHREADS
00191   // Get no-threads debug object
00192   DjVuDebug &dbg = debug_obj;
00193 #else
00194   // Get per-thread debug object
00195   long threadid = (long) GThread::current();
00196   DjVuDebug &dbg = debug_map()[threadid];
00197   threads_num=debug_map().size();
00198 #endif
00199   // Check level
00200   dbg.block = (lvl > debug_level);
00201   // Output thread id and indentation
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   // Return
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

kviewshell

Skip menu "kviewshell"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kviewshell
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal