1 // Copyright (C) 2004, 2007 International Business Machines and others.
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // $Id$
6 //
7 // Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13
8 
9 #ifndef __IPDEBUG_HPP__
10 #define __IPDEBUG_HPP__
11 
12 #include "IpoptConfig.h"
13 #include "IpTypes.hpp"
14 
15 #ifdef COIN_IPOPT_CHECKLEVEL
16 #ifdef HAVE_CASSERT
17 # include <cassert>
18 #else
19 # ifdef HAVE_ASSERT_H
20 #  include <assert.h>
21 # else
22 #  error "don't have header file for assert"
23 # endif
24 #endif
25 #else
26 #define COIN_IPOPT_CHECKLEVEL 0
27 #endif
28 
29 #if COIN_IPOPT_CHECKLEVEL > 0
30 # ifdef NDEBUG
31 #  undef NDEBUG
32 # endif
33 # define DBG_ASSERT(test) assert(test)
34 # define DBG_ASSERT_EXCEPTION(__condition, __except_type, __msg) \
35    ASSERT_EXCEPTION( (__condition), __except_type, __msg);
36 # define DBG_DO(__cmd) __cmd
37 #else
38 # define DBG_ASSERT(test)
39 # define DBG_ASSERT_EXCEPTION(__condition, __except_type, __msg)
40 # define DBG_DO(__cmd)
41 #endif
42 
43 #ifndef COIN_IPOPT_VERBOSITY
44 #define COIN_IPOPT_VERBOSITY 0
45 #endif
46 
47 #if COIN_IPOPT_VERBOSITY < 1
48 # define DBG_START_FUN(__func_name, __verbose_level)
49 # define DBG_START_METH(__func_name, __verbose_level)
50 # define DBG_PRINT(__printf_args)
51 # define DBG_PRINT_VECTOR(__verbose_level, __vec_name, __vec)
52 # define DBG_PRINT_MATRIX(__verbose_level, __mat_name, __mat)
53 # define DBG_EXEC(__verbosity, __cmd)
54 # define DBG_VERBOSITY() 0
55 #else
56 #include <string>
57 
58 namespace Ipopt
59 {
60   // forward definition
61   class Journalist;
62 
63   /** Class that lives throughout the execution of a method or
64   *  function for which debug output is to be generated.  The output
65   *  is sent to the unique debug journalist that is set with
66   *  SetJournalist at the beginning of program execution. */
67   class DebugJournalistWrapper
68   {
69   public:
70     /** @name Constructors/Destructors. */
71     //@{
72     DebugJournalistWrapper(std::string func_name, Index verbose_level);
73     DebugJournalistWrapper(std::string func_name, Index verbose_level,
74                            const void* const method_owner);
75     ~DebugJournalistWrapper();
76     //@}
77 
78     /** @name accessor methods */
79     //@{
Verbosity()80     Index Verbosity()
81     {
82       return verbose_level_;
83     }
Jnlst()84     const Journalist* Jnlst()
85     {
86       return jrnl_;
87     }
IndentationLevel()88     Index IndentationLevel()
89     {
90       return indentation_level_;
91     }
92     //@}
93 
94     /** Printing */
95     void DebugPrintf(Index verbosity, const char* pformat, ...);
96 
97     /* Method for initialization of the static GLOBAL journalist,
98     * through with all debug printout is to be written.  This needs
99     * to be set before any debug printout can be done. */
100     static void SetJournalist(Journalist* jrnl);
101 
102   private:
103     /**@name Default Compiler Generated Methods
104     * (Hidden to avoid implicit creation/calling).
105     * These methods are not implemented and
106     * we do not want the compiler to implement
107     * them for us, so we declare them private
108     * and do not define them. This ensures that
109     * they will not be implicitly created/called. */
110     //@{
111     /** default constructor */
112     DebugJournalistWrapper();
113 
114     /** copy contructor */
115     DebugJournalistWrapper(const DebugJournalistWrapper&);
116 
117     /** Overloaded Equals Operator */
118     DebugJournalistWrapper& operator=(const DebugJournalistWrapper&);
119     //@}
120 
121     static Index indentation_level_;
122     std::string func_name_;
123     Index verbose_level_;
124     const void* method_owner_;
125 
126     static Journalist* jrnl_;
127   };
128 }
129 
130 # define DBG_START_FUN(__func_name, __verbose_level) \
131   DebugJournalistWrapper dbg_jrnl((__func_name), (__verbose_level)); \
132 
133 # define DBG_START_METH(__func_name, __verbose_level) \
134   DebugJournalistWrapper dbg_jrnl((__func_name), (__verbose_level), this);
135 
136 # define DBG_PRINT(__args) \
137   dbg_jrnl.DebugPrintf __args;
138 
139 # define DBG_EXEC(__verbose_level, __cmd) \
140   if (dbg_jrnl.Verbosity() >= (__verbose_level)) { \
141     (__cmd); \
142   }
143 
144 # define DBG_VERBOSITY() \
145   dbg_jrnl.Verbosity()
146 
147 #endif
148 
149 
150 #endif
151