source: trunk/libdjvu/debug.cpp @ 15

Last change on this file since 15 was 15, checked in by Eugene Romanenko, 15 years ago

needed libs update

File size: 7.4 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: debug.cpp,v 1.14 2005/05/27 14:26:00 leonb Exp $
55// $Name: release_3_5_16 $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64#include "debug.h"
65
66#if ( DEBUGLVL > 0 )
67
68#include "GThreads.h"
69#include "GContainer.h"
70#include "GString.h"
71#include "GString.h"
72#include "ByteStream.h"
73#include "GURL.h"
74
75#include <stdarg.h>
76#include <stdio.h>
77#include <errno.h>
78
79#ifdef HAVE_NAMESPACES
80namespace DJVU {
81# ifdef NOT_DEFINED // Just to fool emacs c++ mode
82}
83#endif
84#endif
85
86
87#ifndef UNIX
88#ifndef WIN32
89#ifndef macintosh
90#define UNIX
91#endif
92#endif
93#endif
94
95static GCriticalSection debug_lock;
96#ifdef RUNTIME_DEBUG_ONLY
97static int              debug_level = 0;
98#else
99static int              debug_level = DEBUGLVL;
100#endif
101static int              debug_id;
102static FILE            *debug_file;
103static int              debug_file_count;
104
105#if THREADMODEL==NOTHREADS
106static DjVuDebug debug_obj;
107#else
108static GMap<long, DjVuDebug> &
109debug_map(void)
110{
111  static GMap<long, DjVuDebug> xmap;
112  return xmap;
113}
114#endif
115
116DjVuDebug::DjVuDebug()
117  : block(0), indent(0)
118{
119  id = debug_id++;
120#ifdef UNIX
121  if (debug_file_count++ == 0 && !debug_file)
122    set_debug_file(stderr);
123#endif
124}
125
126DjVuDebug::~DjVuDebug()
127{
128#ifdef UNIX
129  if (--debug_file_count == 0)
130    {
131      if (debug_file && (debug_file != stderr))
132        fclose(debug_file);
133      debug_file = 0;
134    }
135#endif
136}
137
138void   
139DjVuDebug::format(const char *fmt, ... )
140{
141  if (! block)
142    {
143      va_list ap;
144      va_start(ap, fmt);
145      GUTF8String buffer(fmt,ap);
146      va_end(ap);
147      GCriticalSectionLock glock(&debug_lock);
148      if (debug_file)
149        {
150          fprintf(debug_file,"%s", (const char*)buffer);
151          fflush(debug_file);
152        }
153#ifdef WIN32
154      else
155        {
156          USES_CONVERSION;
157          OutputDebugString(A2CT((const char *)buffer));
158        }
159#endif
160    }
161}
162
163void   
164DjVuDebug::set_debug_level(int lvl)
165{
166  debug_level = lvl;
167}
168
169void
170DjVuDebug::set_debug_file(FILE * file)
171{
172  GCriticalSectionLock glock(&debug_lock);
173  if (debug_file && (debug_file != stderr))
174    fclose(debug_file);
175  debug_file = file;
176}
177
178void
179DjVuDebug::modify_indent(int rindent)
180{
181  indent += rindent;
182}
183
184DjVuDebug& 
185DjVuDebug::lock(int lvl, int noindent)
186{
187  int threads_num=1;
188  debug_lock.lock();
189  // Find Debug object
190#if THREADMODEL==NOTHREADS
191  // Get no-threads debug object
192  DjVuDebug &dbg = debug_obj;
193#else
194  // Get per-thread debug object
195  long threadid = (long) GThread::current();
196  DjVuDebug &dbg = debug_map()[threadid];
197  threads_num=debug_map().size();
198#endif
199  // Check level
200  dbg.block = (lvl > debug_level);
201  // Output thread id and indentation
202  if (! noindent)
203    {
204      if (threads_num>1)
205        dbg.format("[T%d] ", dbg.id);
206      int ind = dbg.indent;
207      char buffer[257];
208      memset(buffer,' ', sizeof(buffer)-1);
209      buffer[sizeof(buffer)-1] = 0;
210      while (ind > (int)sizeof(buffer)-1)
211        {
212          dbg.format("%s", buffer);
213          ind -= sizeof(buffer)-1;
214        }
215      if (ind > 0)
216        {
217          buffer[ind] = 0;
218          dbg.format("%s", buffer);
219        }
220    }
221  // Return
222  return dbg;
223}
224
225void
226DjVuDebug::unlock()
227{
228  debug_lock.unlock();
229}
230
231#define OP(type, fmt) \
232DjVuDebug& DjVuDebug::operator<<(type arg)\
233{ format(fmt, arg); return *this; }
234
235DjVuDebug& DjVuDebug::operator<<(bool arg)
236{
237   format("%s", arg ? "TRUE" : "FALSE"); return *this;
238}
239
240//OP(char, "%c")
241OP(unsigned char, "%c")
242OP(int, "%d")
243OP(unsigned int, "%u")
244OP(short int, "%hd")
245OP(unsigned short int, "%hu")
246OP(long, "%ld")
247OP(unsigned long, "%lu")
248OP(float, "%g")
249OP(double, "%g")
250OP(const void * const, "0x%08x")
251
252DjVuDebug& DjVuDebug::operator<<(const char * const ptr) 
253{
254  GUTF8String buffer(ptr?ptr:"(null)");
255  if(buffer.length() > 255)
256  {
257    buffer=buffer.substr(0,252)+"...";
258  }
259  format("%s", (const char *)buffer);
260  return *this; 
261}
262
263DjVuDebug& DjVuDebug::operator<<(const unsigned char * const ptr) 
264{ 
265  return operator<<( (const char *) ptr );
266}
267
268DjVuDebug& DjVuDebug::operator<<(const GUTF8String &ptr)
269{
270  GUTF8String buffer(ptr);
271  if(buffer.length() > 255)
272    buffer=buffer.substr(0,252)+"...";
273  format("%s", (const char *)buffer);
274  return *this; 
275}
276
277DjVuDebugIndent::DjVuDebugIndent(int inc)
278  : inc(inc)
279{
280  DjVuDebug &dbg = DjVuDebug::lock(0,1);
281  dbg.modify_indent(inc);
282  dbg.unlock();
283}
284
285DjVuDebugIndent::~DjVuDebugIndent()
286{
287  DjVuDebug &dbg = DjVuDebug::lock(0,1);
288  dbg.modify_indent(-inc);
289  dbg.unlock();
290}
291
292
293#ifdef HAVE_NAMESPACES
294}
295# ifndef NOT_USING_DJVU_NAMESPACE
296using namespace DJVU;
297# endif
298#endif
299#endif
Note: See TracBrowser for help on using the repository browser.