source: trunk/libdjvu/debug.h @ 269

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

DJVU plugin: djvulibre updated to version 3.5.19

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