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

Revision 10764, 6.7 kB (checked in by robert, 5 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
RevLine 
[5328]1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
[1529]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*/
[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]24namespace osg
25{
[347]26
[10229]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():
[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
49protected:
[10263]50    NullStreamBuffer* _buffer;
[10229]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():
[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
107protected:
[10263]108    NotifyStreamBuffer* _buffer;
[10229]109};
110
111}
112
113using namespace osg;
114
[9941]115static osg::ApplicationUsageProxy Notify_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE, "OSG_NOTIFY_LEVEL <mode>", "FATAL | WARN | NOTICE | DEBUG_INFO | DEBUG_FP | DEBUG | INFO | ALWAYS");
116
[10263]117static osg::NotifySeverity g_NotifyLevel = osg::NOTICE;
118static osg::NullStream *g_NullStream;
119static osg::NotifyStream *g_NotifyStream;
[2]120
[8]121void osg::setNotifyLevel(osg::NotifySeverity severity)
[2]122{
[8]123    osg::initNotifyLevel();
124    g_NotifyLevel = severity;
[2]125}
126
[8]127
128osg::NotifySeverity osg::getNotifyLevel()
[2]129{
[8]130    osg::initNotifyLevel();
[2]131    return g_NotifyLevel;
132}
133
[10229]134void 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]141osg::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]148bool 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    // g_NotifyLevel
160    // =============
[2]161
[8]162    g_NotifyLevel = osg::NOTICE; // Default value
[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        // Convert to upper case
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    // Setup standard notify handler
[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]202bool osg::isNotifyEnabled( osg::NotifySeverity severity )
203{
204    return severity<=g_NotifyLevel;
205}
206
[771]207std::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
223void 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
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.