AGX Dynamics  2.35.0.1
Notify.h
Go to the documentation of this file.
1 /* Copyright 2007-2022. Algoryx Simulation AB.
2 
3 All AGX source code, intellectual property, documentation, sample code,
4 tutorials, scene files and technical white papers, are copyrighted, proprietary
5 and confidential material of Algoryx Simulation AB. You may not download, read,
6 store, distribute, publish, copy or otherwise disseminate, use or expose this
7 material unless having a written signed agreement with Algoryx Simulation AB, or having been
8 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
9 valid commercial license from Algoryx Simulation AB.
10 
11 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
12 from using this software, unless otherwise stated in written agreements with
13 Algoryx Simulation AB.
14 */
15 
16 #pragma once
17 
18 
19 #ifdef _MSC_VER
20 # pragma warning(push)
21 # pragma warning(disable: 4355) // No warnings about using this in member initializer list
22 #endif
23 
24 #include <agx/agx.h>
25 #include <iostream>
26 #include <agx/Vector.h>
27 #include <sstream>
28 #include <agx/HashTable.h>
29 #include <agx/Singleton.h>
30 #include <agx/LogStream.h>
31 
32 #define THREAD_SAFE_NOTIFY 1
33 
34 #if THREAD_SAFE_NOTIFY
36 #endif
37 
38 namespace agx
39 {
40 
42  class Thread;
43  class Notify;
44  class ThreadDataHolder;
46 
47 
49  {
50  public:
52  virtual ~NotifyCallback();
53 
55  virtual void message( const agx::String& msg, int notifyLevel) = 0;
56 
57  protected:
58 
59  };
60 
63  {
64  public:
65 
66  enum NotifyLevel { NOTIFY_DEBUG = 1 << 0,
67  NOTIFY_INFO = 1 << 1,
68  NOTIFY_WARNING = 1 << 2,
69  NOTIFY_ERROR = 1 << 3,
70  NOTIFY_END = 1 << 4,
71  NOTIFY_CLEAR = 1 << 5,
72  NOTIFY_PUSH = 1 << 6,
73  NOTIFY_LOGONLY = 1 << 7
74  };
75 
77  PRINT_NONE = 0, // Print nothing
78  PRINT_FILE = 1, // Print the filename
79  PRINT_TAG = 2, // Print the tag (ERROR, WARNING, INFO, DEBUG
80  PRINT_LINE = 4, // Print the linenumber
81  PRINT_FUNCTION = 8, // Print the functionname
82  PRINT_SELECTION_END = 8,
83  PRINT_DEFAULT = PRINT_TAG | PRINT_FUNCTION,
84  PRINT_ALL = PRINT_FILE | PRINT_TAG | PRINT_LINE | PRINT_FUNCTION
85  };
86 
91  Notify( std::ostream& out_stream = std::cerr);
92 
94  static Notify* instance();
95 
98 
100  const agx::LogStream& logStream() const;
101 
104 
107 
109  bool openLog( const agx::String& filename );
110 
113 
115  bool getThrowOnError() const;
116 
118  void setThrowOnError( bool v );
119 
129  void setBreakOnError( bool f );
130 
132  bool getBreakOnError() const;
133 
134 
136  void addCallback( NotifyCallback *callback, NotifyLevel l );
137 
142  bool removeCallback( NotifyCallback *callback );
143 
149 
151  void addStream( std::ostream* stream, NotifyLevel l );
152 
157  bool removeStream( std::ostream* stream );
158 
163  bool setNotifyLevel( std::ostream* stream, NotifyLevel l );
164 
167 
170 
172  bool getToLogOnly() const;
173 
175  void setToLogOnly( bool f );
176 
177  std::ostream& outStream();
178 
179  private:
180 
181  template <class A, A val >
182  class Mode
183  {
184  public:
185 #ifdef __APPLE__
186  // Hm, some weird bug in Apples gcc doesn't like the original constructor
187  Mode( Notify* notify ) : m_notify( notify ), m_level( val ) {}
188 #else
189  Mode( Notify* notify, A l = val ) : m_notify( notify ), m_level( l ) {}
190 #endif
191  Mode( const Mode& m ) : m_notify( m.m_notify ), m_level( m.m_level ) {}
192 
193  Notify::NotifyLevel getLevel() const {
194  return m_level;
195  }
196  Notify* getNotify() {
197  return m_notify;
198  }
199  const Notify* getNotify() const {
200  return m_notify;
201  }
202  private:
203  Notify* m_notify;
204  Notify::NotifyLevel m_level;
205  Mode() {}
206  };
207 
209 
210 
211  template <class A>
212  class Modifier
213  {
214  public:
215  Modifier( Notify* notify ) : m_notify( notify ), m_value( A( 0 ) ) {}
216  Modifier( const Modifier& m ) : m_notify( m.m_notify ), m_value( m.m_value ) {}
217  A getValue() {
218  return m_value;
219  }
220  Modifier& setValue( A val ) {
221  m_value = val;
222  return *this;
223  }
224 
225  Notify* getNotify() {
226  return m_notify;
227  }
228  const Notify* getNotify() const {
229  return m_notify;
230  }
231 
232  private:
233  Notify* m_notify;
234  A m_value;
235  Modifier() {}
236  };
238 
239 
240  class AGXCORE_EXPORT PrintSelection
241  {
242  public:
243  PrintSelection( int mask );
244 
245  PrintSelection();
246 
247  PrintSelection( const PrintSelection& p );
248  PrintSelection& operator=( const PrintSelection& p );
249 
250  ~PrintSelection();
251 
252  void clear();
253 
255  void setMask( int f );
256 
258  void setMask( int f, bool val );
259 
260  bool isSet( int s ) const;
261 
262  bool isNoneSet() const;
263 
264  private:
265  int m_selection_mask;
266  };
267  public:
268 
270 
271  typedef Mode<Notify::NotifyLevel, Notify::NOTIFY_END> End;
272  typedef Mode<Notify::NotifyLevel, Notify::NOTIFY_CLEAR> Clear;
273  typedef Mode<Notify::NotifyLevel, Notify::NOTIFY_LOGONLY> LogOnly;
274  typedef Modifier<int> Push;
275 
276  SINGLETON_CLASSNAME_METHOD();
277 
278  template<typename T>
279  Notify& operator<<(const T& rhs) {
280  getThreadStringStream() << rhs;
281  return *this;
282  }
283 
284  typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
285  typedef CoutType& (*StandardEndLine)(CoutType&);
286 
288  Notify& operator<<(StandardEndLine manip);
289 
290  operator std::ostream&() { return getThreadStringStream(); }
291 
292  Notify& operator<<( Notify::Clear& /*mode*/ ) {
293  std::ostringstream& str = getThreadStringStream();
294  str.str( "" );
295  str.clear();
296  return *this;
297  }
298 
299  Notify& operator<<( Notify::End& modifier );
300  Notify& operator<<( Notify::Push& modifier );
301  Notify& operator<<( Notify::LogOnly& modifier );
302 
303  std::ios_base::fmtflags setf(std::ios_base::fmtflags newFormatFlags);
304 
305 
306  Clear& clearString() ;
307 
308  End& end();
309 
310  Push& push();
311 
312  LogOnly& logOnly();
313 
314 
315  void debug( const char* function, int line, const char* file, const agx::String& msg );
316  void info( const char* function, int line, const char* file, const agx::String& msg );
317  void warning( const char* function, int line, const char* file, const agx::String& msg );
318  void error( const char* function, int line, const char* file, const agx::String& msg );
319 
320  void debug( const agx::String& msg );
321  void info( const agx::String& msg );
322  void warning( const agx::String& msg );
323  void error( const agx::String& msg );
324 
326  void pushPrintSelection( const PrintSelection& p, NotifyLevel l );
327 
328  void pushPrintSelection( const PrintSelection& p );
329 
330  // Get the current print selection stack
331  PrintSelection& getPrintSelection( NotifyLevel l );
332 
333  PrintSelection& getPrintSelection();
334 
336  bool popPrintSelection( NotifyLevel l );
337 
338  bool popPrintSelection();
339 
341 
342 
343  agx::String buildString( );
344  agx::String setDebugStrings( NotifyLevel l, const char* function, int line, const char* file );
345 
346  virtual ~Notify();
347 
348  void shutdown() override;
349 
350  protected:
351 
352  friend void agxCore::init();
353  friend class agx::Thread;
354  friend void agx::setNumThreads(size_t numThreads);
355  friend class ThreadDataHolder;
356 
357  std::string str() const;
358 
359  void str( const std::string& text );
360 
361  void clear();
362 
363  End m_end;
364  Clear m_clear;
365  Push m_push;
366  LogOnly m_log;
367 
368  private:
369 
370  typedef agx::HashTable<std::ostream*, NotifyLevel > StreamMap;
371  StreamMap m_streams;
372 
374  CallbackMap m_callbacks;
375 
376  std::ostringstream& getThreadStringStream();
377  const std::ostringstream& getThreadStringStream() const;
378 
379  typedef Vector<PrintSelection> PrintSelectionStack;
380  AGX_DECLARE_POINTER_TYPES(ThreadData);
381  class ThreadData : public agx::Referenced {
382  public:
383  ThreadData();
384  NotifyLevel output_mode;
385  agx::String tag;
386  int line;
387  agx::String function;
388  agx::String file;
389  std::ostringstream stream;
390  NotifyLevel notify_level;
391 
392  Vector<PrintSelectionStack> selection_stack;
393 
394  protected:
395  virtual ~ThreadData();
396  };
397 
398  ThreadData* getThreadData();
399  const ThreadData* getThreadData() const;
400 
401  agx::LogStream m_logStream;
402 
403  bool m_to_log_only;
404  bool m_throw_on_error;
405  bool m_break_on_error;
406 
407  static Notify* s_instance;
408  std::ostream& m_outStream;
409  mutable agx::ReentrantMutex m_mutex;
410 
411  };
412 
413 
414 } // agx
415 
416 
417 #define ADD_NOTIFY_FUNCTIONALITY_TO_CLASS() \
418  public:\
419  void debug( const agx::String& msg ) { Notify::instance()->debug(msg); };\
420  void info( const agx::String& msg ) { Notify::instance()->info(msg); };\
421  void warning( const agx::String& msg ) { Notify::instance()->warning(msg); };\
422  void error( const agx::String& msg ) { Notify::instance()->error(msg); };\
423  protected:\
424  void debug( const char* function, int line, const char* file, const agx::String& msg ) { Notify::instance()->debug(function,line,file,msg); };\
425  void info( const char* function, int line, const char* file, const agx::String& msg ) { Notify::instance()->info(function,line,file,msg); };\
426  void warning( const char* function, int line, const char* file, const agx::String& msg ) { Notify::instance()->warning(function,line,file,msg); };\
427  void error( const char* function, int line, const char* file, const agx::String& msg ) { Notify::instance()->error(function,line,file,msg); };\
428  ::agx::Notify::Push push() { return Notify::instance()->push(); };\
429  ::agx::Notify::End end() { return Notify::instance()->end(); };\
430  agx::String setDebugStrings( ::agx::Notify::NotifyLevel l, const char* function, int line, const char* file ) { return Notify::instance()->setDebugStrings(l,function,line,file); };\
431  public:\
432  ::agx::Notify& getNotify() {\
433  ::agx::Notify* n = Notify::instance();\
434  return *n;\
435  } \
436  const ::agx::Notify& getNotify() const {\
437  ::agx::Notify* n = Notify::instance();\
438  return *n;\
439 }
440 
441 
442 #ifdef AGX_DEBUG
443 #define NOTIFY_POS_ARGS __FUNCTION__,__LINE__,__FILE__
444 #else
445 #define NOTIFY_POS_ARGS "",-1,""
446 #endif
447 
448 #define NOTIFY() getNotify()
449 #define NOTIFY_DEBUG() setDebugStrings(::agx::Notify::NOTIFY_DEBUG, NOTIFY_POS_ARGS)
450 #define NOTIFY_INFO() setDebugStrings(::agx::Notify::NOTIFY_INFO, NOTIFY_POS_ARGS)
451 #define NOTIFY_WARNING() setDebugStrings(::agx::Notify::NOTIFY_WARNING, NOTIFY_POS_ARGS)
452 #define NOTIFY_ERROR() setDebugStrings(::agx::Notify::NOTIFY_ERROR, NOTIFY_POS_ARGS)
453 #define NOTIFY_LOG() logOnly()
454 #define NOTIFY_END() end()
455 #define NOTIFY_CLEAR() clear()
456 #define NOTIFY_STATE(X) push().setValue(X)
457 
458 
459 #define NOTIFY_WARNING_IF_nullptr(X,Y) if ( Y == nullptr ) { X << X.NOTIFY_WARNING() << " nullptr pointer: " << #Y << std::endl << X.NOTIFY_END(); }
460 #define NOTIFY_WARNING_IF_FALSE(X,Y) if ( !Y ) { X << X.NOTIFY_WARNING() << " Test false: " << #Y <<std::endl << X.NOTIFY_END(); }
461 #define NOTIFY_WARNING_IF_TRUE(X,Y) if ( Y ) { X << X.NOTIFY_WARNING() << " Test true: " << #Y <<std::endl << X.NOTIFY_END(); }
462 
463 #define NOTIFY_ERROR_IF_nullptr(X,Y) if ( Y == nullptr ) { X << X.NOTIFY_STATE(::agx::Notify::PRINT_ALL) << X.NOTIFY_ERROR() << " nullptr pointer: " << #Y <<std::endl << X.NOTIFY_END(); }
464 #define NOTIFY_ERROR_IF_FALSE(X,Y) if ( !Y ) { X << X.NOTIFY_STATE(::agx::Notify::PRINT_ALL) << X.NOTIFY_ERROR() << " Test false: " << #Y <<std::endl << X.NOTIFY_END(); }
465 #define NOTIFY_ERROR_IF_TRUE(X,Y) if ( Y ) { X << X.NOTIFY_STATE(::agx::Notify::PRINT_ALL) << X.NOTIFY_ERROR() << " Test true: " << #Y <<std::endl << X.NOTIFY_END(); }
466 
467 
468 #define NOTIFY_INSTANCE() ::agx::Notify::instance()
469 
470 
471 
472 #ifdef _MSC_VER
473 # pragma warning(pop)
474 #endif
475 
#define NOTIFY_INFO()
Definition: Notify.h:450
#define NOTIFY_ERROR()
Definition: Notify.h:452
#define NOTIFY_CLEAR()
Definition: Notify.h:455
#define NOTIFY_DEBUG()
Definition: Notify.h:449
#define NOTIFY_WARNING()
Definition: Notify.h:451
#define NOTIFY_END()
Definition: Notify.h:454
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXCORE_EXPORT
Inheritance with partial specialization due to bug with ref_ptr containers.
Class for printing out messages to console, file, virtual method and or a callback function.
Definition: LogStream.h:45
Callback class, derive from this and Register to the notify singleton to get callbacks.
Definition: Notify.h:49
virtual ~NotifyCallback()
virtual void message(const agx::String &msg, int notifyLevel)=0
Virtual method called whenever there is a message ready.
Class for handling logging of messages.
Definition: Notify.h:63
bool getToLogOnly() const
bool setNotifyLevel(NotifyCallback *callback, NotifyLevel l)
Set the notify level for a specific callback.
void addStream(std::ostream *stream, NotifyLevel l)
Add a stream that will be printed to if the notify level less or equal to l, the memory will NOT be m...
const agx::LogStream & logStream() const
std::ostream & outStream()
void setLogNotifyLevel(NotifyLevel l)
Set the notify level for the LogStream.
NotifyLevel getNotifyLevel() const
NotifySelection
Definition: Notify.h:76
void setBreakOnError(bool f)
WIN32 specific: If set to true, a breakpoint will be set in debug build.
bool getBreakOnError() const
bool removeCallback(NotifyCallback *callback)
Remove a callback from list of callbacks to report messages to.
bool setNotifyLevel(std::ostream *stream, NotifyLevel l)
Set the notify level for a specific stream.
agx::LogStream & logStream()
bool openLog(const agx::String &filename)
Open a log file (in the LogStream)
void setToLogOnly(bool f)
If f==true, logging will occur only to the LogStream, not to any of the associated ostreams.
NotifyLevel getOutputMode() const
void addCallback(NotifyCallback *callback, NotifyLevel l)
Add a callback class that will be printed to if the notify level less or equal to l.
void setNotifyLevel(NotifyLevel level)
Set the current notify level.
static Notify * instance()
bool removeStream(std::ostream *stream)
Remove a stream from list of streams to report messages to.
void setThrowOnError(bool v)
Specify whether we should Throw an exception upon calling error?
Notify(std::ostream &out_stream=std::cerr)
Constructor.
bool getThrowOnError() const
void setOutputMode(NotifyLevel l)
Set the current output mode, do not use! (used internally)
Base class providing referencing counted objects.
Definition: Referenced.h:114
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
Definition: Singleton.h:31
agx::Thread is a representation of an OS specific implementation of a computational thread.
Definition: Thread.h:198
Templated vector class.
Definition: agx/Vector.h:53
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
Contains function for initialization and shutdown of agxCore library when agxPhysics is not used.
AGXTERRAIN_EXPORT void clear()
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
void AGXCORE_EXPORT setNumThreads(size_t numThreads)
Set the number of threads to use (including the main thread).
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
void AGXPHYSICS_EXPORT shutdown()
Shutdown agx.
std::recursive_mutex ReentrantMutex
Definition: Object.h:49
std::ostream & operator<<(std::ostream &stream, const AtomicValueT< T > &value)
Definition: AtomicValue.h:109
void AGXPHYSICS_EXPORT init()
Initialize agx.