source: trunk/libdjvu/debug.h @ 15

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

needed libs update

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