root/OpenSceneGraph/trunk/src/osg/Notify.cpp @ 10764

Revision 10764, 6.7 kB (checked in by robert, 4 years ago)

<iterator>, <stdlib.h> and <ctype.h> includes required for QNX compiler

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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#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
24namespace osg
25{
26
27class NullStreamBuffer : public std::streambuf
28{
29private:
30    std::streamsize xsputn(const std::streambuf::char_type *str, std::streamsize n)
31    {
32        return n;
33    }
34};
35
36struct NullStream : public std::ostream
37{
38public:
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
49protected:
50    NullStreamBuffer* _buffer;
51};
52
53/** Stream buffer calling notify handler when buffer is synchronized (usually on std::endl).
54 * Stream stores last notification severity to pass it to handler call.
55 */
56struct 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    /** Sets severity for next call of notify handler */
66    void setCurrentSeverity(osg::NotifySeverity severity) { _severity = severity; }
67    osg::NotifySeverity getCurrentSeverity() const { return _severity; }
68
69private:
70
71    int sync()
72    {
73        sputc(0); // string termination
74        if (_handler.valid())
75            _handler->notify(_severity, pbase());
76        pubseekpos(0, std::ios_base::out); // or str(std::string())
77        return 0;
78    }
79
80    osg::ref_ptr<osg::NotifyHandler> _handler;
81    osg::NotifySeverity _severity;
82};
83
84struct NotifyStream : public std::ostream
85{
86public:
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
107protected:
108    NotifyStreamBuffer* _buffer;
109};
110
111}
112
113using namespace osg;
114
115static osg::ApplicationUsageProxy Notify_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE, "OSG_NOTIFY_LEVEL <mode>", "FATAL | WARN | NOTICE | DEBUG_INFO | DEBUG_FP | DEBUG | INFO | ALWAYS");
116
117static osg::NotifySeverity g_NotifyLevel = osg::NOTICE;
118static osg::NullStream *g_NullStream;
119static osg::NotifyStream *g_NotifyStream;
120
121void osg::setNotifyLevel(osg::NotifySeverity severity)
122{
123    osg::initNotifyLevel();
124    g_NotifyLevel = severity;
125}
126
127
128osg::NotifySeverity osg::getNotifyLevel()
129{
130    osg::initNotifyLevel();
131    return g_NotifyLevel;
132}
133
134void 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
141osg::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
148bool 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    // g_NotifyLevel
160    // =============
161
162    g_NotifyLevel = osg::NOTICE; // Default value
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        // Convert to upper case
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    // Setup standard notify handler
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
202bool osg::isNotifyEnabled( osg::NotifySeverity severity )
203{
204    return severity<=g_NotifyLevel;
205}
206
207std::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
223void 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
240void osg::WinDebugNotifyHandler::notify(osg::NotifySeverity severity, const char *message)
241{
242    OutputDebugStringA(message);
243}
244
245#endif
Note: See TracBrowser for help on using the browser.