source: trunk/libdjvu/debug.h @ 426

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

DJVU plugin: djvulibre updated to version 3.5.22

File size: 9.7 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.h,v 1.14 2007/05/12 18:53:31 leonb Exp $
57// $Name: release_3_5_22 $
58
59#ifndef _DEBUG_H_
60#define _DEBUG_H_
61
62#ifdef HAVE_CONFIG_H
63# include "config.h"
64#endif
65#if NEED_GNUG_PRAGMAS
66# pragma interface
67#endif
68
69#include <stdio.h>
70
71
72#ifdef HAVE_NAMESPACES
73namespace DJVU {
74# ifdef NOT_DEFINED // Just to fool emacs c++ mode
75}
76# endif
77#endif
78
79/** @name debug.h
80
81    Files #"debug.h"# and #"debug.cpp"# implement means to print debug
82    messages in a multithread safe way.  Message are also marked with a thread
83    identifier.  Under Windows, debug messages are directly sent to the
84    debugger using the Win32 function #OutputDebugString#.  Under Unix, debug
85    messages are printed on the controlling terminal, preferably using device
86    #/dev/tty#.
87
88    The preprocessor variable #DEBUGLVL# defines which debug code is going to
89    be compiled.  Selecting #-DDEBUGLVL=0# (the default) disables all
90    debugging code.  Selecting a positive values (e.g. #-DDEBUGLVL=4#) enables
91    more and more debugging code.
92
93    Message output is controlled by the current debugging level (an integer
94    between #0# and #DEBUGLVL#). Greater values enable more messages.  The
95    initial debugging level is set to the maximum value.  The debugging level
96    can be changed using macro \Ref{DEBUG_SET_LEVEL}.
97
98    Message indentation can be modified using macro \Ref{DEBUG_MAKE_INDENT}.
99    Messages are generated by macro \Ref{DEBUG_MSG} or its variants.  The
100    argument of the macro can contain several components separated by operator
101    #<<#, as demonstrated in the example below:
102    \begin{verbatim}
103    DEBUG_MSG("The value of a[" << n << "] is " << a[n] << '\n');
104    \end{verbatim}
105
106    One more preprocessor variable #RUNTIME_DEBUG_ONLY# enables compilation
107    of debug code, but does not enable the debug messages automatically.
108    In order to see them the program should use \Ref{DEBUG_SET_LEVEL} to
109    change the level to anything greater than 0. Normally this happens when
110    user specifies option #-debug# in the command line. Usage of
111    #RUNTIME_DEBUG_ONLY# implies #DEBUGLVL=1# if not specified otherwise.
112
113    Finally, #-DNO_DEBUG# or #-DNDEBUG# can be used instead of #-DDEBUGLVL=0#,
114    and #-D_DEBUG# can be used instead of #-DDEBUGLVL=0#.
115
116    {\bf Historical Comment} --- Debug macros are rarely used in the reference
117    DjVu library because Leon thinks that debugging messages unnecessarily
118    clutter the code.  Debug macros are used everywhere in the plugin code
119    because Andrew thinks that code without debugging messages is close to
120    useless.  No agreement could be reached. Neither could they agree on
121    if cluttering header files with huge documentation chunks helps to
122    improve code readability.
123
124    @memo
125    Macros for printing debug messages.
126    @version
127    #$Id: debug.h,v 1.14 2007/05/12 18:53:31 leonb Exp $#
128    @author
129    Andrew Erofeev <eaf@geocities.com> -- initial implementation \\
130    Leon Bottou <leonb@research.att.com> -- cleanups */
131//@{
132
133#ifndef DEBUGLVL
134# ifdef NDEBUG
135#  define DEBUGLVL 0
136# endif
137#endif
138#ifndef DEBUGLVL
139# ifdef NO_DEBUG
140#  define DEBUGLVL 0
141# endif
142#endif
143#ifndef DEBUGLVL
144# ifdef RUNTIME_DEBUG_ONLY
145#  define DEBUGLVL 1
146# endif
147#endif
148#ifndef DEBUGLVL
149# ifdef _DEBUG
150#  define DEBUGLVL 1
151# endif
152#endif
153#ifndef DEBUGLVL
154#  define DEBUGLVL 0
155#endif
156
157#if DEBUGLVL <= 0
158
159# ifndef NO_DEBUG
160#  define NO_DEBUG
161# endif
162# ifndef NDEBUG
163#  define NDEBUG
164# endif
165# ifdef _DEBUG
166#  undef _DEBUG
167# endif
168
169# define DEBUG_MAKE_INDENT(x)
170# define DEBUG_SET_LEVEL(level)
171# define DEBUG_MSG_LVL(level,x)
172# define DEBUG_MSGN_LVL(level,x)
173
174#else
175
176# ifdef NO_DEBUG
177#  undef NO_DEBUG
178# endif
179# ifdef NDEBUG
180#  undef NDEBUG
181# endif
182# ifndef _DEBUG
183#  define _DEBUG
184# endif
185
186class GUTF8String;
187
188// ------------ SUPPORT
189
190class DjVuDebug // DJVU_CLASS
191{
192private:
193  int    id;
194  int    block;
195  int    indent;
196  void   format(const char *fmt, ... );
197public:
198  // construction
199  DjVuDebug();
200  ~DjVuDebug();
201  // access
202  static void   set_debug_level(int lvl);
203  static void   set_debug_file(FILE * file);
204  void          modify_indent(int rindent);
205  static DjVuDebug& lock(int lvl, int noindent);
206  void          unlock();
207  // printing
208  DjVuDebug &   operator<<(bool b);
209  DjVuDebug &   operator<<(char c);
210  DjVuDebug &   operator<<(unsigned char c);
211  DjVuDebug &   operator<<(int i);
212  DjVuDebug &   operator<<(unsigned int i);
213  DjVuDebug &   operator<<(short int i);
214  DjVuDebug &   operator<<(unsigned short int i);
215  DjVuDebug &   operator<<(long i);
216  DjVuDebug &   operator<<(unsigned long i);
217  DjVuDebug &   operator<<(const char * const ptr);
218  DjVuDebug &   operator<<(const unsigned char * const ptr);
219  DjVuDebug&    operator<<(const GUTF8String &ptr);
220  DjVuDebug &   operator<<(float f);
221  DjVuDebug &   operator<<(double d);
222  DjVuDebug &   operator<<(const void * const p);
223};
224
225class DjVuDebugIndent // DJVU_CLASS
226{
227private:
228  int inc;
229public:
230  DjVuDebugIndent(int inc=2);
231  ~DjVuDebugIndent();
232//#define DEBUG_MAKE_INDENT_2(x, y) DjVuDebugIndent debug_indent ## y (x)
233//#define DEBUG_MAKE_INDENT_1(x, y) DEBUG_MAKE_INDENT_2(x, y)
234#define DEBUG_MAKE_INDENT_1(x, y) DjVuDebugIndent debug_indent ## y (x)
235};
236
237// ------------ MAIN MACROS
238
239# define DEBUG_MAKE_INDENT(x)     DEBUG_MAKE_INDENT_1(x, __LINE__)
240# define DEBUG_SET_LEVEL(level)   DjVuDebug::set_debug_level(level)
241# define DEBUG_MSG_LVL(level,x)   { ( DjVuDebug::lock(level,0) << x ).unlock(); }
242# define DEBUG_MSGN_LVL(level,x)  { ( DjVuDebug::lock(level,1) << x ).unlock(); }
243# define DEBUG_MSGF_LVL(level,x)  { ( DjVuDebug::lock(level,1) << x ).unlock(); }
244
245#endif
246
247
248// ------------ EAF MACROS
249
250#if ( DEBUGLVL >= 1 )
251/** Generates a level 1 message */
252# define DEBUG1_MSG(x)  DEBUG_MSG_LVL(1,x)
253# define DEBUG1_MSGF(x) DEBUG_MSGF_LVL(1,x)
254#else
255# define DEBUG1_MSG(x)
256# define DEBUG1_MSGF(x)
257#endif
258#if ( DEBUGLVL >= 2 )
259/** Generates a level 2 message */
260# define DEBUG2_MSG(x)  DEBUG_MSG_LVL(2,x)
261# define DEBUG2_MSGF(x) DEBUG_MSGF_LVL(2,x)
262#else
263# define DEBUG2_MSG(x)
264# define DEBUG2_MSGF(x)
265#endif
266#if ( DEBUGLVL >= 3 )
267/** Generates a level 3 message */
268# define DEBUG3_MSG(x)  DEBUG_MSG_LVL(3,x)
269# define DEBUG3_MSGF(x) DEBUG_MSGF_LVL(3,x)
270#else
271# define DEBUG3_MSG(x)
272# define DEBUG3_MSGF(x)
273#endif
274#if ( DEBUGLVL >= 4 )
275/** Generates a level 4 message */
276# define DEBUG4_MSG(x)  DEBUG_MSG_LVL(4,x)
277# define DEBUG4_MSGF(x) DEBUG_MSGF_LVL(4,x)
278#else
279# define DEBUG4_MSG(x)
280# define DEBUG4_MSGF(x)
281#endif
282
283#define DEBUG_RUNTIME_SET_LEVEL(level) DEBUG_SET_LEVEL(level)
284/** Generates a level 1 message. */
285#define DEBUG_MSG(x)  DEBUG1_MSG(x)
286/** Generates a level 1 message without indentation. */
287#define DEBUG_MSGF(x) DEBUG1_MSGF(x)
288/** Generates a level 1 message terminated with a newline. */
289#define DEBUG_MSGN(x) DEBUG_MSG(x<<'\n')
290
291//@}
292
293// ------------ THE END
294
295#ifdef HAVE_NAMESPACES
296}
297# ifndef NOT_USING_DJVU_NAMESPACE
298using namespace DJVU;
299# endif
300#endif
301
302#endif // DEBUG_H
Note: See TracBrowser for help on using the repository browser.