source: trunk/libdjvu/debug.cpp @ 209

Last change on this file since 209 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

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