source: trunk/libdjvu/debug.cpp @ 280

Last change on this file since 280 was 280, checked in by rbri, 11 years ago

DJVU plugin: djvulibre updated to version 3.5.22

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.17 2008/01/27 09:24:35 leonb Exp $
57// $Name: release_3_5_22 $
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 WIN32
82# include <windows.h>  // OutputDebugString
83#endif
84
85
86#ifdef HAVE_NAMESPACES
87namespace DJVU {
88# ifdef NOT_DEFINED // Just to fool emacs c++ mode
89}
90#endif
91#endif
92
93
94#ifndef UNIX
95#ifndef WIN32
96#ifndef macintosh
97#define UNIX
98#endif
99#endif
100#endif
101
102static GCriticalSection debug_lock;
103#ifdef RUNTIME_DEBUG_ONLY
104static int              debug_level = 0;
105#else
106static int              debug_level = DEBUGLVL;
107#endif
108static int              debug_id;
109static FILE            *debug_file;
110static int              debug_file_count;
111
112#if THREADMODEL==NOTHREADS
113static DjVuDebug debug_obj;
114#else
115static GMap<long, DjVuDebug> &
116debug_map(void)
117{
118  static GMap<long, DjVuDebug> xmap;
119  return xmap;
120}
121#endif
122
123DjVuDebug::DjVuDebug()
124  : block(0), indent(0)
125{
126  id = debug_id++;
127#ifdef UNIX
128  if (debug_file_count++ == 0 && !debug_file)
129    set_debug_file(stderr);
130#endif
131}
132
133DjVuDebug::~DjVuDebug()
134{
135#ifdef UNIX
136  if (--debug_file_count == 0)
137    {
138      if (debug_file && (debug_file != stderr))
139        fclose(debug_file);
140      debug_file = 0;
141    }
142#endif
143}
144
145void   
146DjVuDebug::format(const char *fmt, ... )
147{
148  if (! block)
149    {
150      va_list ap;
151      va_start(ap, fmt);
152      GUTF8String buffer(fmt,ap);
153      va_end(ap);
154      GCriticalSectionLock glock(&debug_lock);
155      if (debug_file)
156        {
157          fprintf(debug_file,"%s", (const char*)buffer);
158          fflush(debug_file);
159        }
160#ifdef WIN32
161      else
162        {
163          OutputDebugStringA((const char *)buffer);
164        }
165#endif
166    }
167}
168
169void   
170DjVuDebug::set_debug_level(int lvl)
171{
172  debug_level = lvl;
173}
174
175void
176DjVuDebug::set_debug_file(FILE * file)
177{
178  GCriticalSectionLock glock(&debug_lock);
179  if (debug_file && (debug_file != stderr))
180    fclose(debug_file);
181  debug_file = file;
182}
183
184void
185DjVuDebug::modify_indent(int rindent)
186{
187  indent += rindent;
188}
189
190DjVuDebug& 
191DjVuDebug::lock(int lvl, int noindent)
192{
193  int threads_num=1;
194  debug_lock.lock();
195  // Find Debug object
196#if THREADMODEL==NOTHREADS
197  // Get no-threads debug object
198  DjVuDebug &dbg = debug_obj;
199#else
200  // Get per-thread debug object
201  long threadid = (long) GThread::current();
202  DjVuDebug &dbg = debug_map()[threadid];
203  threads_num=debug_map().size();
204#endif
205  // Check level
206  dbg.block = (lvl > debug_level);
207  // Output thread id and indentation
208  if (! noindent)
209    {
210      if (threads_num>1)
211        dbg.format("[T%d] ", dbg.id);
212      int ind = dbg.indent;
213      char buffer[257];
214      memset(buffer,' ', sizeof(buffer)-1);
215      buffer[sizeof(buffer)-1] = 0;
216      while (ind > (int)sizeof(buffer)-1)
217        {
218          dbg.format("%s", buffer);
219          ind -= sizeof(buffer)-1;
220        }
221      if (ind > 0)
222        {
223          buffer[ind] = 0;
224          dbg.format("%s", buffer);
225        }
226    }
227  // Return
228  return dbg;
229}
230
231void
232DjVuDebug::unlock()
233{
234  debug_lock.unlock();
235}
236
237#define OP(type, fmt) \
238DjVuDebug& DjVuDebug::operator<<(type arg)\
239{ format(fmt, arg); return *this; }
240
241DjVuDebug& DjVuDebug::operator<<(bool arg)
242{
243   format("%s", arg ? "TRUE" : "FALSE"); return *this;
244}
245
246OP(char, "%c")
247OP(unsigned char, "%c")
248OP(int, "%d")
249OP(unsigned int, "%u")
250OP(short int, "%hd")
251OP(unsigned short int, "%hu")
252OP(long, "%ld")
253OP(unsigned long, "%lu")
254OP(float, "%g")
255OP(double, "%g")
256OP(const void * const, "0x%08x")
257
258DjVuDebug& DjVuDebug::operator<<(const char * const ptr) 
259{
260  GUTF8String buffer(ptr?ptr:"(null)");
261  if(buffer.length() > 255)
262  {
263    buffer=buffer.substr(0,252)+"...";
264  }
265  format("%s", (const char *)buffer);
266  return *this; 
267}
268
269DjVuDebug& DjVuDebug::operator<<(const unsigned char * const ptr) 
270{ 
271  return operator<<( (const char *) ptr );
272}
273
274DjVuDebug& DjVuDebug::operator<<(const GUTF8String &ptr)
275{
276  GUTF8String buffer(ptr);
277  if(buffer.length() > 255)
278    buffer=buffer.substr(0,252)+"...";
279  format("%s", (const char *)buffer);
280  return *this; 
281}
282
283DjVuDebugIndent::DjVuDebugIndent(int inc)
284  : inc(inc)
285{
286  DjVuDebug &dbg = DjVuDebug::lock(0,1);
287  dbg.modify_indent(inc);
288  dbg.unlock();
289}
290
291DjVuDebugIndent::~DjVuDebugIndent()
292{
293  DjVuDebug &dbg = DjVuDebug::lock(0,1);
294  dbg.modify_indent(-inc);
295  dbg.unlock();
296}
297
298
299#ifdef HAVE_NAMESPACES
300}
301# ifndef NOT_USING_DJVU_NAMESPACE
302using namespace DJVU;
303# endif
304#endif
305#endif
Note: See TracBrowser for help on using the repository browser.