summaryrefslogtreecommitdiffstats
path: root/kviewshell/plugins/djvu/libdjvu/debug.h
blob: c39390a28235769902d5b6097d4f9f459d831217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//C-  -*- C++ -*-
//C- -------------------------------------------------------------------
//C- DjVuLibre-3.5
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
//C- Copyright (c) 2001  AT&T
//C-
//C- This software is subject to, and may be distributed under, the
//C- GNU General Public License, Version 2. The license should have
//C- accompanied the software or you may obtain a copy of the license
//C- from the Free Software Foundation at http://www.fsf.org .
//C-
//C- This program is distributed in the hope that it will be useful,
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//C- GNU General Public License for more details.
//C- 
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
//C- Software authorized us to replace the original DjVu(r) Reference 
//C- Library notice by the following text (see doc/lizard2002.djvu):
//C-
//C-  ------------------------------------------------------------------
//C- | DjVu (r) Reference Library (v. 3.5)
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
//C- | 6,058,214 and patents pending.
//C- |
//C- | This software is subject to, and may be distributed under, the
//C- | GNU General Public License, Version 2. The license should have
//C- | accompanied the software or you may obtain a copy of the license
//C- | from the Free Software Foundation at http://www.fsf.org .
//C- |
//C- | The computer code originally released by LizardTech under this
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
//C- | General Public License.   This grant only confers the right to 
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
//C- | the extent such infringement is reasonably necessary to enable 
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
//C- | any greater extent that may be necessary to utilize further 
//C- | modifications or combinations.
//C- |
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//C- +------------------------------------------------------------------
// 
// $Id: debug.h,v 1.12 2005/05/27 14:26:01 leonb Exp $
// $Name: release_3_5_15 $

#ifndef _DEBUG_H_
#define _DEBUG_H_

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if NEED_GNUG_PRAGMAS
# pragma interface
#endif

#include <stdio.h>
#ifdef WIN32
# include <atlbase.h> // USES_CONVERSION, A2CT macro
# include <windows.h> // OutputDebugString
#endif 


#ifdef HAVE_NAMESPACES
namespace DJVU {
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
}
# endif
#endif

/** @name debug.h

    Files #"debug.h"# and #"debug.cpp"# implement means to print debug
    messages in a multithread safe way.  Message are also marked with a thread
    identifier.  Under Windows, debug messages are directly sent to the
    debugger using the Win32 function #OutputDebugString#.  Under Unix, debug
    messages are printed on the controlling terminal, preferably using device
    #/dev/tty#.

    The preprocessor variable #DEBUGLVL# defines which debug code is going to
    be compiled.  Selecting #-DDEBUGLVL=0# (the default) disables all
    debugging code.  Selecting a positive values (e.g. #-DDEBUGLVL=4#) enables
    more and more debugging code.

    Message output is controlled by the current debugging level (an integer
    between #0# and #DEBUGLVL#). Greater values enable more messages.  The
    initial debugging level is set to the maximum value.  The debugging level
    can be changed using macro \Ref{DEBUG_SET_LEVEL}.

    Message indentation can be modified using macro \Ref{DEBUG_MAKE_INDENT}.
    Messages are generated by macro \Ref{DEBUG_MSG} or its variants.  The
    argument of the macro can contain several components separated by operator
    #<<#, as demonstrated in the example below:
    \begin{verbatim}
    DEBUG_MSG("The value of a[" << n << "] is " << a[n] << '\n');
    \end{verbatim}

    One more preprocessor variable #RUNTIME_DEBUG_ONLY# enables compilation
    of debug code, but does not enable the debug messages automatically.
    In order to see them the program should use \Ref{DEBUG_SET_LEVEL} to
    change the level to anything greater than 0. Normally this happens when
    user specifies option #-debug# in the command line. Usage of
    #RUNTIME_DEBUG_ONLY# implies #DEBUGLVL=1# if not specified otherwise.

    Finally, #-DNO_DEBUG# or #-DNDEBUG# can be used instead of #-DDEBUGLVL=0#,
    and #-D_DEBUG# can be used instead of #-DDEBUGLVL=0#.

    {\bf Historical Comment} --- Debug macros are rarely used in the reference
    DjVu library because Leon thinks that debugging messages unnecessarily
    clutter the code.  Debug macros are used everywhere in the plugin code
    because Andrew thinks that code without debugging messages is close to
    useless.  No agreement could be reached. Neither could they agree on
    if cluttering header files with huge documentation chunks helps to
    improve code readability.

    @memo 
    Macros for printing debug messages.
    @version 
    #$Id: debug.h,v 1.12 2005/05/27 14:26:01 leonb Exp $#
    @author
    Andrew Erofeev <eaf@geocities.com> -- initial implementation \\
    Leon Bottou <leonb@research.att.com> -- cleanups */
//@{

#ifndef DEBUGLVL
# ifdef NDEBUG
#  define DEBUGLVL 0
# endif 
#endif
#ifndef DEBUGLVL
# ifdef NO_DEBUG
#  define DEBUGLVL 0
# endif 
#endif
#ifndef DEBUGLVL
# ifdef RUNTIME_DEBUG_ONLY
#  define DEBUGLVL 1
# endif 
#endif
#ifndef DEBUGLVL
# ifdef _DEBUG
#  define DEBUGLVL 1
# endif 
#endif
#ifndef DEBUGLVL
#  define DEBUGLVL 0
#endif

#if DEBUGLVL <= 0

# ifndef NO_DEBUG
#  define NO_DEBUG
# endif
# ifndef NDEBUG
#  define NDEBUG
# endif
# ifdef _DEBUG
#  undef _DEBUG
# endif

# define DEBUG_MAKE_INDENT(x)
# define DEBUG_SET_LEVEL(level)
# define DEBUG_MSG_LVL(level,x)
# define DEBUG_MSGN_LVL(level,x)

#else

# ifdef NO_DEBUG
#  undef NO_DEBUG
# endif
# ifdef NDEBUG
#  undef NDEBUG
# endif
# ifndef _DEBUG
#  define _DEBUG
# endif

class GUTF8String;

// ------------ SUPPORT

class DjVuDebug // DJVU_CLASS
{
private:
  int    id;
  int    block;
  int    indent;
  void   format(const char *fmt, ... );
public:
  // construction
  DjVuDebug();
  ~DjVuDebug();
  // access
  static void   set_debug_level(int lvl);
  static void	set_debug_file(FILE * file);
  void          modify_indent(int rindent);
  static DjVuDebug& lock(int lvl, int noindent);
  void          unlock();
  // printing
  DjVuDebug &	operator<<(bool b);
  DjVuDebug &	operator<<(char c);
  DjVuDebug &	operator<<(unsigned char c);
  DjVuDebug &	operator<<(int i);
  DjVuDebug &	operator<<(unsigned int i);
  DjVuDebug &	operator<<(short int i);
  DjVuDebug &	operator<<(unsigned short int i);
  DjVuDebug &	operator<<(long i);
  DjVuDebug &	operator<<(unsigned long i);
  DjVuDebug &	operator<<(const char * const ptr);
  DjVuDebug &	operator<<(const unsigned char * const ptr);
  DjVuDebug&    operator<<(const GUTF8String &ptr);
  DjVuDebug &	operator<<(float f);
  DjVuDebug &	operator<<(double d);
  DjVuDebug &	operator<<(const void * const p);
};

class DjVuDebugIndent // DJVU_CLASS
{
private:
  int inc;
public:
  DjVuDebugIndent(int inc=2);
  ~DjVuDebugIndent();
//#define DEBUG_MAKE_INDENT_2(x, y) DjVuDebugIndent debug_indent ## y (x)
//#define DEBUG_MAKE_INDENT_1(x, y) DEBUG_MAKE_INDENT_2(x, y)
#define DEBUG_MAKE_INDENT_1(x, y) DjVuDebugIndent debug_indent ## y (x)
};

// ------------ MAIN MACROS

# define DEBUG_MAKE_INDENT(x)     DEBUG_MAKE_INDENT_1(x, __LINE__)
# define DEBUG_SET_LEVEL(level)   DjVuDebug::set_debug_level(level)
# define DEBUG_MSG_LVL(level,x)   { ( DjVuDebug::lock(level,0) << x ).unlock(); }
# define DEBUG_MSGN_LVL(level,x)  { ( DjVuDebug::lock(level,1) << x ).unlock(); }
# define DEBUG_MSGF_LVL(level,x)  { ( DjVuDebug::lock(level,1) << x ).unlock(); }

#endif


// ------------ EAF MACROS

#if ( DEBUGLVL >= 1 )
/** Generates a level 1 message */
# define DEBUG1_MSG(x)  DEBUG_MSG_LVL(1,x)
# define DEBUG1_MSGF(x) DEBUG_MSGF_LVL(1,x)
#else
# define DEBUG1_MSG(x)
# define DEBUG1_MSGF(x)
#endif
#if ( DEBUGLVL >= 2 )
/** Generates a level 2 message */
# define DEBUG2_MSG(x)  DEBUG_MSG_LVL(2,x)
# define DEBUG2_MSGF(x) DEBUG_MSGF_LVL(2,x)
#else
# define DEBUG2_MSG(x)
# define DEBUG2_MSGF(x)
#endif
#if ( DEBUGLVL >= 3 )
/** Generates a level 3 message */
# define DEBUG3_MSG(x)  DEBUG_MSG_LVL(3,x)
# define DEBUG3_MSGF(x) DEBUG_MSGF_LVL(3,x)
#else
# define DEBUG3_MSG(x)
# define DEBUG3_MSGF(x)
#endif
#if ( DEBUGLVL >= 4 )
/** Generates a level 4 message */
# define DEBUG4_MSG(x)  DEBUG_MSG_LVL(4,x)
# define DEBUG4_MSGF(x) DEBUG_MSGF_LVL(4,x)
#else
# define DEBUG4_MSG(x)
# define DEBUG4_MSGF(x)
#endif

#define DEBUG_RUNTIME_SET_LEVEL(level) DEBUG_SET_LEVEL(level)
/** Generates a level 1 message. */
#define DEBUG_MSG(x)  DEBUG1_MSG(x)
/** Generates a level 1 message without indentation. */
#define DEBUG_MSGF(x) DEBUG1_MSGF(x)
/** Generates a level 1 message terminated with a newline. */
#define DEBUG_MSGN(x) DEBUG_MSG(x<<'\n')

//@}

// ------------ THE END

#ifdef HAVE_NAMESPACES
}
# ifndef NOT_USING_DJVU_NAMESPACE
using namespace DJVU;
# endif
#endif

#endif // DEBUG_H