Notify 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_NOTIFY_H
  14. #define OSG_NOTIFY_H 1
  15. #include <osg/Export>
  16. #include <osg/Referenced> // for NotifyHandler
  17. #include <ostream>
  18. namespace osg {
  19. /** Range of notify levels from DEBUG_FP through to FATAL, ALWAYS
  20. * is reserved for forcing the absorption of all messages. The
  21. * keywords are also used verbatim when specified by the environmental
  22. * variable OSGNOTIFYLEVEL or OSG_NOTIFY_LEVEL.
  23. * See documentation on osg::notify() for further details.
  24. */
  25. enum NotifySeverity {
  26. ALWAYS=0,
  27. FATAL=1,
  28. WARN=2,
  29. NOTICE=3,
  30. INFO=4,
  31. DEBUG_INFO=5,
  32. DEBUG_FP=6
  33. };
  34. /** set the notify level, overriding the default or the value set by
  35. * the environmental variable OSGNOTIFYLEVEL or OSG_NOTIFY_LEVEL.
  36. */
  37. extern OSG_EXPORT void setNotifyLevel(NotifySeverity severity);
  38. /** get the notify level. */
  39. extern OSG_EXPORT NotifySeverity getNotifyLevel();
  40. /** initialize notify level. */
  41. extern OSG_EXPORT bool initNotifyLevel();
  42. #ifdef OSG_NOTIFY_DISABLED
  43. inline bool isNotifyEnabled(NotifySeverity) { return false; }
  44. #else
  45. /** is notification enabled, given the current setNotifyLevel() setting? */
  46. extern OSG_EXPORT bool isNotifyEnabled(NotifySeverity severity);
  47. #endif
  48. /** notify messaging function for providing fatal through to verbose
  49. * debugging messages. Level of messages sent to the console can
  50. * be controlled by setting the NotifyLevel either within your
  51. * application or via the an environmental variable i.e.
  52. * - setenv OSGNOTIFYLEVEL DEBUG (for tsh)
  53. * - export OSGNOTIFYLEVEL=DEBUG (for bourne shell)
  54. * - set OSGNOTIFYLEVEL=DEBUG (for Windows)
  55. *
  56. * All tell the osg to redirect all debugging and more important messages
  57. * to the notification stream (useful for debugging) setting ALWAYS will force
  58. * all messages to be absorbed, which might be appropriate for final
  59. * applications. Default NotifyLevel is NOTICE. Check the enum
  60. * #NotifySeverity for full range of possibilities. To use the notify
  61. * with your code simply use the notify function as a normal file
  62. * stream (like std::cout) i.e
  63. * @code
  64. * osg::notify(osg::DEBUG) << "Hello Bugs!" << std::endl;
  65. * @endcode
  66. * @see setNotifyLevel, setNotifyHandler
  67. */
  68. extern OSG_EXPORT std::ostream& notify(const NotifySeverity severity);
  69. inline std::ostream& notify(void) { return notify(osg::INFO); }
  70. #define OSG_NOTIFY(level) if (osg::isNotifyEnabled(level)) osg::notify(level)
  71. #define OSG_ALWAYS OSG_NOTIFY(osg::ALWAYS)
  72. #define OSG_FATAL OSG_NOTIFY(osg::FATAL)
  73. #define OSG_WARN OSG_NOTIFY(osg::WARN)
  74. #define OSG_NOTICE OSG_NOTIFY(osg::NOTICE)
  75. #define OSG_INFO OSG_NOTIFY(osg::INFO)
  76. #define OSG_DEBUG OSG_NOTIFY(osg::DEBUG_INFO)
  77. #define OSG_DEBUG_FP OSG_NOTIFY(osg::DEBUG_FP)
  78. /** Handler processing output of notification stream. It acts as a sink to
  79. * notification messages. It is called when notification stream needs to be
  80. * synchronized (i.e. after osg::notify() << std::endl).
  81. * StandardNotifyHandler is used by default, it writes notifications to stderr
  82. * (severity <= WARN) or stdout (severity > WARN).
  83. * Notifications can be redirected to other sinks such as GUI widgets or
  84. * windows debugger (WinDebugNotifyHandler) with custom handlers.
  85. * Use setNotifyHandler to set custom handler.
  86. * Note that osg notification API is not thread safe although notification
  87. * handler is called from many threads. When incorporating handlers into GUI
  88. * widgets you must take care of thread safety on your own.
  89. * @see setNotifyHandler
  90. */
  91. class OSG_EXPORT NotifyHandler : public osg::Referenced
  92. {
  93. public:
  94. virtual void notify(osg::NotifySeverity severity, const char *message) = 0;
  95. };
  96. /** Set notification handler, by default StandardNotifyHandler is used.
  97. * @see NotifyHandler
  98. */
  99. extern OSG_EXPORT void setNotifyHandler(NotifyHandler *handler);
  100. /** Get currrent notification handler. */
  101. extern OSG_EXPORT NotifyHandler *getNotifyHandler();
  102. /** Redirects notification stream to stderr (severity <= WARN) or stdout (severity > WARN).
  103. * The fputs() function is used to write messages to standard files. Note that
  104. * std::out and std::cerr streams are not used.
  105. * @see setNotifyHandler
  106. */
  107. class OSG_EXPORT StandardNotifyHandler : public NotifyHandler
  108. {
  109. public:
  110. void notify(osg::NotifySeverity severity, const char *message);
  111. };
  112. #if defined(WIN32) && !defined(__CYGWIN__)
  113. /** Redirects notification stream to windows debugger with use of
  114. * OuputDebugString functions.
  115. * @see setNotifyHandler
  116. */
  117. class OSG_EXPORT WinDebugNotifyHandler : public NotifyHandler
  118. {
  119. public:
  120. void notify(osg::NotifySeverity severity, const char *message);
  121. };
  122. #endif
  123. }
  124. #endif