InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Trace.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: EricM
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //========================================================================================
23 
24 #ifndef __TRACE_H__
25 #define __TRACE_H__
26 
27 // Trace takes the same arguments as printf and uses vsprintf
28 // to create a buffer that is then sent to the trace log,
29 // eventually to appear in the log or the window.
30 
31 
32 #include "PMTypes.h"
33 
34 #if defined(DEBUG)
35 #ifndef ID_ENABLE_DEBUGGING
36 #error ID_ENABLE_DEBUGGING is not defined in DEBUG.
37 #endif
38 #endif
39 
40 #if defined(ID_ENABLE_DEBUGGING)
41  #include <string>
42  #include <boost/mpl/or.hpp>
43  #include <boost/utility/enable_if.hpp>
44  #include <boost/type_traits.hpp>
45  #include <utility> // For std::pair
46  #include <vector>
47 
48  namespace Trace
49  {
55  RUNTIME_DECL void TraceImpl(bool16 checkCategory, const char* category, const char* filename, const int32 lineNo, const char* format, ...);
56 
57  // get_return_type helpers deduce the return type from the argument
58  template<typename A, typename Enabler = void>
59  struct get_return_type;
60 
61  // Generic implementation for built-in types.
62  template<typename A>
63  struct get_return_type<A,
64  typename boost::enable_if<boost::mpl::or_<boost::is_arithmetic<A>, boost::is_pointer<A>, boost::is_enum<A> > >::type >
65  {
66  typedef A type;
67  };
68 
69  // Specialization for std::string
70  template<>
71  struct get_return_type<std::string>
72  {
73  typedef const char* type;
74  };
75 
76  // Specialization for UID
77  template<>
78  struct get_return_type<UID>
79  {
80  typedef uint32 type;
81  };
82 
83 
84 
85  // format_arg helpers: extract the correct value from the argument
86 
87  // Generic form for built in types
88  template<typename A>
89  inline typename get_return_type<A>::type format_arg(A const& a)
90  {
91  return a;
92  }
93 
94  // Specialization for std::string
95  template<>
96  inline const char* format_arg<std::string>(std::string const& a)
97  {
98  return a.c_str();
99  }
100 
101  // Specialization for UID
102  template<>
103  inline uint32 format_arg<UID>(UID const& a)
104  {
105  return a.Get();
106  }
107 
108  //-----------------------
109 
112  class RUNTIME_DECL TraceCategory
113  {
114  public:
115  ~TraceCategory();
116 
117  TraceCategory();
118  explicit TraceCategory(const char* str);
119  TraceCategory(const TraceCategory& rhs);
120 
121  TraceCategory& operator=(TraceCategory rhs);
122 
123  public:
124  inline const char* c_str() const
125  {
126  return fText;
127  }
128 
129  inline friend void swap(TraceCategory& left, TraceCategory& right){
130  using std::swap;
131  swap(left.fText, right.fText);
132  swap(left.fLen, right.fLen);
133  }
134 
135  bool16 operator==(const TraceCategory& rhs) const;
136  bool16 operator<(const TraceCategory& rhs) const;
137 
138  protected:
139  char* fText;
140  size_t fLen;
141  };
142 
143 
146  class RUNTIME_DECL TraceFlow
147  {
148  public:
149  typedef std::pair<TraceCategory, bool16> SEntry;
150  typedef std::vector<SEntry> SEntryArray;
151 
152  enum {kMaxCategories = 512};
153 
154  // API
155  public:
156  static void EnableCategory(const char* category, bool16 enable = true);
157  static bool16 IsCategoryEnabled(const char* category);
158  static void ClearAll();
159  static void RemoveAllDisabled();
160 
161  // Retrieves an alphabetically sorted copy of the registered entries with the Traceflow
162  static void GetEntries(SEntryArray& entries);
163 
164  // Internal API
165  protected:
166 
167  typedef SEntry* iterator;
168 
169  static void AddCategory(TraceCategory const& newCategory, bool16 enable);
170  static void RemoveCategory(iterator category);
171 
172  static iterator __begin();
173  static iterator __end();
174  };
175 
176 
177  } // End namespace Trace
178 
179  // This line kicks off the Trace overloads generator
180 // #include "TraceGenerator.h"
181 
182  // To shorten the compilation time, we include the generated (pre-processed) file here
183  #include "TraceOverloads.h"
184 
185  //Function to validate predicate datatype in Macro usage
186  inline bool16 CheckPredicateInTrace(bool16 predicate)
187  {
188  return predicate;
189  }
190 
191  #define TRACE(...) TRACE_INTERNAL(kFalse, "No_Category", __FILE__, __LINE__, __VA_ARGS__ )
192  #define TRACEFLOW_WITH_FILE(category, file, lineno, ...) \
193  TRACE_INTERNAL(kTrue, category, file, lineno, __VA_ARGS__ )
194 
195  #define TRACEFLOW(category, ...) TRACE_INTERNAL(kTrue, category, __FILE__, __LINE__, __VA_ARGS__ )
196 
197  #define TRACE_IF(predicate, ...) CheckPredicateInTrace(predicate) ? TRACE_INTERNAL(kFalse, "No_Category", __FILE__, __LINE__, __VA_ARGS__ ) : (void)0
198 
199  #define TRACEFLOW_IF(predicate, category, ...) CheckPredicateInTrace(predicate) ? TRACE_INTERNAL(kTrue, category, __FILE__, __LINE__, __VA_ARGS__ ) : (void)0
200 
201  // Same definition as release(does nothing)
202 #define TRACEFLOW_OBSOLETE(...) ((void) 0)
203 
204 #ifdef MACINTOSH
205  #ifndef DEBUG_LAYER_LOG
206  // If we want to enable logs in console then enable below if.
207  #if 0
208  #define DEBUG_LAYER_LOG(category, ...) NSLog((@""), ##__VA_ARGS__)
209  #else
210  #define DEBUG_LAYER_LOG(category, ...) TRACE_INTERNAL(kTrue, category, __FILE__, __LINE__, __VA_ARGS__ )
211  #endif
212  #endif
213 #else
214 // We don't want layer logs for windows.
215  #ifndef DEBUG_LAYER_LOG
216  #define DEBUG_LAYER_LOG(...) ((void) 0)
217  #endif
218 #endif
219 
220 #else // !defined(ID_ENABLE_DEBUGGING)
221 #define TRACE(...) ((void) 0)
222 #define TRACEFLOW(...) ((void) 0)
223 #define TRACEFLOW_OBSOLETE(...) ((void) 0)
224 #define TRACE_IF(...) ((void) 0)
225 #define TRACEFLOW_IF(...) ((void) 0)
226 
227  #define DEBUG_LAYER_LOG(...) ((void) 0)
228 
229 #endif // defined(ID_ENABLE_DEBUGGING)
230 
231 #endif // __TRACE_H__
232