| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/Notify> |
|---|
| 14 | #include <osg/ApplicationUsage> |
|---|
| 15 | #include <osg/ref_ptr> |
|---|
| 16 | #include <string> |
|---|
| 17 | #include <stdlib.h> |
|---|
| 18 | #include <stdio.h> |
|---|
| 19 | #include <sstream> |
|---|
| 20 | #include <iostream> |
|---|
| 21 | |
|---|
| 22 | #include <ctype.h> |
|---|
| 23 | |
|---|
| 24 | namespace osg |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 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(): |
|---|
| 40 | std::ostream(new NullStreamBuffer) |
|---|
| 41 | { _buffer = dynamic_cast<NullStreamBuffer *>(rdbuf()); } |
|---|
| 42 | |
|---|
| 43 | ~NullStream() |
|---|
| 44 | { |
|---|
| 45 | rdbuf(0); |
|---|
| 46 | delete _buffer; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | protected: |
|---|
| 50 | NullStreamBuffer* _buffer; |
|---|
| 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(): |
|---|
| 88 | std::ostream(new NotifyStreamBuffer) |
|---|
| 89 | { _buffer = dynamic_cast<NotifyStreamBuffer *>(rdbuf()); } |
|---|
| 90 | |
|---|
| 91 | void setCurrentSeverity(osg::NotifySeverity severity) |
|---|
| 92 | { |
|---|
| 93 | _buffer->setCurrentSeverity(severity); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | osg::NotifySeverity getCurrentSeverity() const |
|---|
| 97 | { |
|---|
| 98 | return _buffer->getCurrentSeverity(); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | ~NotifyStream() |
|---|
| 102 | { |
|---|
| 103 | rdbuf(0); |
|---|
| 104 | delete _buffer; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | protected: |
|---|
| 108 | NotifyStreamBuffer* _buffer; |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | using namespace osg; |
|---|
| 114 | |
|---|
| 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 | |
|---|
| 117 | static osg::NotifySeverity g_NotifyLevel = osg::NOTICE; |
|---|
| 118 | static osg::NullStream *g_NullStream; |
|---|
| 119 | static osg::NotifyStream *g_NotifyStream; |
|---|
| 120 | |
|---|
| 121 | void osg::setNotifyLevel(osg::NotifySeverity severity) |
|---|
| 122 | { |
|---|
| 123 | osg::initNotifyLevel(); |
|---|
| 124 | g_NotifyLevel = severity; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | osg::NotifySeverity osg::getNotifyLevel() |
|---|
| 129 | { |
|---|
| 130 | osg::initNotifyLevel(); |
|---|
| 131 | return g_NotifyLevel; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | void osg::setNotifyHandler(osg::NotifyHandler *handler) |
|---|
| 135 | { |
|---|
| 136 | osg::NotifyStreamBuffer *buffer = static_cast<osg::NotifyStreamBuffer *>(g_NotifyStream->rdbuf()); |
|---|
| 137 | if (buffer) |
|---|
| 138 | buffer->setNotifyHandler(handler); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | osg::NotifyHandler* osg::getNotifyHandler() |
|---|
| 142 | { |
|---|
| 143 | osg::initNotifyLevel(); |
|---|
| 144 | osg::NotifyStreamBuffer *buffer = static_cast<osg::NotifyStreamBuffer *>(g_NotifyStream->rdbuf()); |
|---|
| 145 | return buffer ? buffer->getNotifyHandler() : 0; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | bool osg::initNotifyLevel() |
|---|
| 149 | { |
|---|
| 150 | static bool s_NotifyInit = false; |
|---|
| 151 | static osg::NullStream s_NullStream; |
|---|
| 152 | static osg::NotifyStream s_NotifyStream; |
|---|
| 153 | |
|---|
| 154 | if (s_NotifyInit) return true; |
|---|
| 155 | |
|---|
| 156 | g_NullStream = &s_NullStream; |
|---|
| 157 | g_NotifyStream = &s_NotifyStream; |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | g_NotifyLevel = osg::NOTICE; |
|---|
| 163 | |
|---|
| 164 | char* OSGNOTIFYLEVEL=getenv("OSG_NOTIFY_LEVEL"); |
|---|
| 165 | if (!OSGNOTIFYLEVEL) OSGNOTIFYLEVEL=getenv("OSGNOTIFYLEVEL"); |
|---|
| 166 | if(OSGNOTIFYLEVEL) |
|---|
| 167 | { |
|---|
| 168 | |
|---|
| 169 | std::string stringOSGNOTIFYLEVEL(OSGNOTIFYLEVEL); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | for(std::string::iterator i=stringOSGNOTIFYLEVEL.begin(); |
|---|
| 173 | i!=stringOSGNOTIFYLEVEL.end(); |
|---|
| 174 | ++i) |
|---|
| 175 | { |
|---|
| 176 | *i=toupper(*i); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 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; |
|---|
| 186 | else if(stringOSGNOTIFYLEVEL.find("INFO")!=std::string::npos) g_NotifyLevel=osg::INFO; |
|---|
| 187 | else std::cout << "Warning: invalid OSG_NOTIFY_LEVEL set ("<<stringOSGNOTIFYLEVEL<<")"<<std::endl; |
|---|
| 188 | |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | osg::NotifyStreamBuffer *buffer = dynamic_cast<osg::NotifyStreamBuffer *>(g_NotifyStream->rdbuf()); |
|---|
| 193 | if (buffer && !buffer->getNotifyHandler()) |
|---|
| 194 | buffer->setNotifyHandler(new StandardNotifyHandler); |
|---|
| 195 | |
|---|
| 196 | s_NotifyInit = true; |
|---|
| 197 | |
|---|
| 198 | return true; |
|---|
| 199 | |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | bool osg::isNotifyEnabled( osg::NotifySeverity severity ) |
|---|
| 203 | { |
|---|
| 204 | return severity<=g_NotifyLevel; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 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 | |
|---|
| 215 | if (severity<=g_NotifyLevel) |
|---|
| 216 | { |
|---|
| 217 | g_NotifyStream->setCurrentSeverity(severity); |
|---|
| 218 | return *g_NotifyStream; |
|---|
| 219 | } |
|---|
| 220 | return *g_NullStream; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | void osg::StandardNotifyHandler::notify(osg::NotifySeverity severity, const char *message) |
|---|
| 224 | { |
|---|
| 225 | #if 0 |
|---|
| 226 | if (severity <= osg::WARN) |
|---|
| 227 | fputs(message, stderr); |
|---|
| 228 | else |
|---|
| 229 | fputs(message, stdout); |
|---|
| 230 | #else |
|---|
| 231 | fputs(message, stdout); |
|---|
| 232 | #endif |
|---|
| 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 |
|---|