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

Revision 10715, 6.6 kB (checked in by robert, 5 years ago)

Changed notify to output warnings and normally messages to std::out

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