| [5328] | 1 | |
|---|
| [1529] | 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| [120] | 13 | #include <osg/Notify> |
|---|
| [9941] | 14 | #include <osg/ApplicationUsage> |
|---|
| [10229] | 15 | #include <osg/ref_ptr> |
|---|
| [2] | 16 | #include <string> |
|---|
| [7747] | 17 | #include <stdlib.h> |
|---|
| [10229] | 18 | #include <stdio.h> |
|---|
| 19 | #include <sstream> |
|---|
| [2772] | 20 | #include <iostream> |
|---|
| [2] | 21 | |
|---|
| [10764] | 22 | #include <ctype.h> |
|---|
| 23 | |
|---|
| [10229] | 24 | namespace osg |
|---|
| 25 | { |
|---|
| [347] | 26 | |
|---|
| [10229] | 27 | class NullStreamBuffer : public std::streambuf |
|---|
| 28 | { |
|---|
| 29 | private: |
|---|
| 30 | std::streamsize xsputn(const std::streambuf::char_type *str, std::streamsize n) |
|---|
| 31 | { |
|---|
| 32 | return n; |
|---|
| 33 | } |
|---|
| 34 | }; |
|---|
| 35 | |
|---|
| 36 | struct NullStream : public std::ostream |
|---|
| 37 | { |
|---|
| 38 | public: |
|---|
| 39 | NullStream(): |
|---|
| [10263] | 40 | std::ostream(new NullStreamBuffer) |
|---|
| 41 | { _buffer = dynamic_cast<NullStreamBuffer *>(rdbuf()); } |
|---|
| 42 | |
|---|
| 43 | ~NullStream() |
|---|
| 44 | { |
|---|
| 45 | rdbuf(0); |
|---|
| 46 | delete _buffer; |
|---|
| 47 | } |
|---|
| [10229] | 48 | |
|---|
| 49 | protected: |
|---|
| [10263] | 50 | NullStreamBuffer* _buffer; |
|---|
| [10229] | 51 | }; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | struct NotifyStreamBuffer : public std::stringbuf |
|---|
| 57 | { |
|---|
| 58 | NotifyStreamBuffer() : _severity(osg::NOTICE) |
|---|
| 59 | { |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void setNotifyHandler(osg::NotifyHandler *handler) { _handler = handler; } |
|---|
| 63 | osg::NotifyHandler *getNotifyHandler() const { return _handler.get(); } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | void setCurrentSeverity(osg::NotifySeverity severity) { _severity = severity; } |
|---|
| 67 | osg::NotifySeverity getCurrentSeverity() const { return _severity; } |
|---|
| 68 | |
|---|
| 69 | private: |
|---|
| 70 | |
|---|
| 71 | int sync() |
|---|
| 72 | { |
|---|
| 73 | sputc(0); |
|---|
| 74 | if (_handler.valid()) |
|---|
| 75 | _handler->notify(_severity, pbase()); |
|---|
| 76 | pubseekpos(0, std::ios_base::out); |
|---|
| 77 | return 0; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | osg::ref_ptr<osg::NotifyHandler> _handler; |
|---|
| 81 | osg::NotifySeverity _severity; |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | struct NotifyStream : public std::ostream |
|---|
| 85 | { |
|---|
| 86 | public: |
|---|
| 87 | NotifyStream(): |
|---|
| [10263] | 88 | std::ostream(new NotifyStreamBuffer) |
|---|
| 89 | { _buffer = dynamic_cast<NotifyStreamBuffer *>(rdbuf()); } |
|---|
| [10229] | 90 | |
|---|
| 91 | void setCurrentSeverity(osg::NotifySeverity severity) |
|---|
| 92 | { |
|---|
| [10263] | 93 | _buffer->setCurrentSeverity(severity); |
|---|
| [10229] | 94 | } |
|---|
| 95 | |
|---|
| 96 | osg::NotifySeverity getCurrentSeverity() const |
|---|
| 97 | { |
|---|
| [10263] | 98 | return _buffer->getCurrentSeverity(); |
|---|
| [10229] | 99 | } |
|---|
| [10263] | 100 | |
|---|
| 101 | ~NotifyStream() |
|---|
| 102 | { |
|---|
| 103 | rdbuf(0); |
|---|
| 104 | delete _buffer; |
|---|
| 105 | } |
|---|
| [10229] | 106 | |
|---|
| 107 | protected: |
|---|
| [10263] | 108 | NotifyStreamBuffer* _buffer; |
|---|
| [10229] | 109 | }; |
|---|
| 110 | |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | using namespace osg; |
|---|
| 114 | |
|---|
| [9941] | 115 | static osg::ApplicationUsageProxy Notify_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE, "OSG_NOTIFY_LEVEL <mode>", "FATAL | WARN | NOTICE | DEBUG_INFO | DEBUG_FP | DEBUG | INFO | ALWAYS"); |
|---|
| 116 | |
|---|
| [10263] | 117 | static osg::NotifySeverity g_NotifyLevel = osg::NOTICE; |
|---|
| 118 | static osg::NullStream *g_NullStream; |
|---|
| 119 | static osg::NotifyStream *g_NotifyStream; |
|---|
| [2] | 120 | |
|---|
| [8] | 121 | void osg::setNotifyLevel(osg::NotifySeverity severity) |
|---|
| [2] | 122 | { |
|---|
| [8] | 123 | osg::initNotifyLevel(); |
|---|
| 124 | g_NotifyLevel = severity; |
|---|
| [2] | 125 | } |
|---|
| 126 | |
|---|
| [8] | 127 | |
|---|
| 128 | osg::NotifySeverity osg::getNotifyLevel() |
|---|
| [2] | 129 | { |
|---|
| [8] | 130 | osg::initNotifyLevel(); |
|---|
| [2] | 131 | return g_NotifyLevel; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| [10229] | 134 | void osg::setNotifyHandler(osg::NotifyHandler *handler) |
|---|
| 135 | { |
|---|
| [10263] | 136 | osg::NotifyStreamBuffer *buffer = static_cast<osg::NotifyStreamBuffer *>(g_NotifyStream->rdbuf()); |
|---|
| [10229] | 137 | if (buffer) |
|---|
| 138 | buffer->setNotifyHandler(handler); |
|---|
| 139 | } |
|---|
| [2] | 140 | |
|---|
| [10263] | 141 | osg::NotifyHandler* osg::getNotifyHandler() |
|---|
| [10229] | 142 | { |
|---|
| 143 | osg::initNotifyLevel(); |
|---|
| [10263] | 144 | osg::NotifyStreamBuffer *buffer = static_cast<osg::NotifyStreamBuffer *>(g_NotifyStream->rdbuf()); |
|---|
| [10229] | 145 | return buffer ? buffer->getNotifyHandler() : 0; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| [8] | 148 | bool osg::initNotifyLevel() |
|---|
| [2] | 149 | { |
|---|
| [751] | 150 | static bool s_NotifyInit = false; |
|---|
| [10263] | 151 | static osg::NullStream s_NullStream; |
|---|
| 152 | static osg::NotifyStream s_NotifyStream; |
|---|
| [751] | 153 | |
|---|
| 154 | if (s_NotifyInit) return true; |
|---|
| [10263] | 155 | |
|---|
| 156 | g_NullStream = &s_NullStream; |
|---|
| 157 | g_NotifyStream = &s_NotifyStream; |
|---|
| [8] | 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| [2] | 161 | |
|---|
| [8] | 162 | g_NotifyLevel = osg::NOTICE; |
|---|
| [2] | 163 | |
|---|
| [638] | 164 | char* OSGNOTIFYLEVEL=getenv("OSG_NOTIFY_LEVEL"); |
|---|
| 165 | if (!OSGNOTIFYLEVEL) OSGNOTIFYLEVEL=getenv("OSGNOTIFYLEVEL"); |
|---|
| [8] | 166 | if(OSGNOTIFYLEVEL) |
|---|
| 167 | { |
|---|
| [2] | 168 | |
|---|
| [8] | 169 | std::string stringOSGNOTIFYLEVEL(OSGNOTIFYLEVEL); |
|---|
| [2] | 170 | |
|---|
| [8] | 171 | |
|---|
| 172 | for(std::string::iterator i=stringOSGNOTIFYLEVEL.begin(); |
|---|
| 173 | i!=stringOSGNOTIFYLEVEL.end(); |
|---|
| 174 | ++i) |
|---|
| [2] | 175 | { |
|---|
| [8] | 176 | *i=toupper(*i); |
|---|
| [2] | 177 | } |
|---|
| 178 | |
|---|
| [8] | 179 | if(stringOSGNOTIFYLEVEL.find("ALWAYS")!=std::string::npos) g_NotifyLevel=osg::ALWAYS; |
|---|
| 180 | else if(stringOSGNOTIFYLEVEL.find("FATAL")!=std::string::npos) g_NotifyLevel=osg::FATAL; |
|---|
| 181 | else if(stringOSGNOTIFYLEVEL.find("WARN")!=std::string::npos) g_NotifyLevel=osg::WARN; |
|---|
| 182 | else if(stringOSGNOTIFYLEVEL.find("NOTICE")!=std::string::npos) g_NotifyLevel=osg::NOTICE; |
|---|
| 183 | else if(stringOSGNOTIFYLEVEL.find("DEBUG_INFO")!=std::string::npos) g_NotifyLevel=osg::DEBUG_INFO; |
|---|
| 184 | else if(stringOSGNOTIFYLEVEL.find("DEBUG_FP")!=std::string::npos) g_NotifyLevel=osg::DEBUG_FP; |
|---|
| 185 | else if(stringOSGNOTIFYLEVEL.find("DEBUG")!=std::string::npos) g_NotifyLevel=osg::DEBUG_INFO; |
|---|
| [1057] | 186 | else if(stringOSGNOTIFYLEVEL.find("INFO")!=std::string::npos) g_NotifyLevel=osg::INFO; |
|---|
| [1060] | 187 | else std::cout << "Warning: invalid OSG_NOTIFY_LEVEL set ("<<stringOSGNOTIFYLEVEL<<")"<<std::endl; |
|---|
| [8] | 188 | |
|---|
| [2] | 189 | } |
|---|
| [8] | 190 | |
|---|
| [10229] | 191 | |
|---|
| [10263] | 192 | osg::NotifyStreamBuffer *buffer = dynamic_cast<osg::NotifyStreamBuffer *>(g_NotifyStream->rdbuf()); |
|---|
| [10229] | 193 | if (buffer && !buffer->getNotifyHandler()) |
|---|
| 194 | buffer->setNotifyHandler(new StandardNotifyHandler); |
|---|
| 195 | |
|---|
| [4991] | 196 | s_NotifyInit = true; |
|---|
| 197 | |
|---|
| [8] | 198 | return true; |
|---|
| 199 | |
|---|
| [2] | 200 | } |
|---|
| [751] | 201 | |
|---|
| [4381] | 202 | bool osg::isNotifyEnabled( osg::NotifySeverity severity ) |
|---|
| 203 | { |
|---|
| 204 | return severity<=g_NotifyLevel; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| [771] | 207 | std::ostream& osg::notify(const osg::NotifySeverity severity) |
|---|
| 208 | { |
|---|
| 209 | static bool initialized = false; |
|---|
| 210 | if (!initialized) |
|---|
| 211 | { |
|---|
| 212 | initialized = osg::initNotifyLevel(); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| [751] | 215 | if (severity<=g_NotifyLevel) |
|---|
| 216 | { |
|---|
| [10263] | 217 | g_NotifyStream->setCurrentSeverity(severity); |
|---|
| 218 | return *g_NotifyStream; |
|---|
| [751] | 219 | } |
|---|
| [10263] | 220 | return *g_NullStream; |
|---|
| [751] | 221 | } |
|---|
| [10229] | 222 | |
|---|
| 223 | void osg::StandardNotifyHandler::notify(osg::NotifySeverity severity, const char *message) |
|---|
| 224 | { |
|---|
| [10715] | 225 | #if 0 |
|---|
| [10229] | 226 | if (severity <= osg::WARN) |
|---|
| 227 | fputs(message, stderr); |
|---|
| 228 | else |
|---|
| 229 | fputs(message, stdout); |
|---|
| [10715] | 230 | #else |
|---|
| 231 | fputs(message, stdout); |
|---|
| 232 | #endif |
|---|
| [10229] | 233 | } |
|---|
| 234 | |
|---|
| 235 | #if defined(WIN32) && !defined(__CYGWIN__) |
|---|
| 236 | |
|---|
| 237 | #define WIN32_LEAN_AND_MEAN |
|---|
| 238 | #include <windows.h> |
|---|
| 239 | |
|---|
| 240 | void osg::WinDebugNotifyHandler::notify(osg::NotifySeverity severity, const char *message) |
|---|
| 241 | { |
|---|
| 242 | OutputDebugStringA(message); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | #endif |
|---|