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

Revision 13041, 6.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • 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 bool s_NeedNotifyInit = true;
118static osg::NotifySeverity g_NotifyLevel = osg::NOTICE;
119static osg::NullStream *g_NullStream;
120static osg::NotifyStream *g_NotifyStream;
121
122void osg::setNotifyLevel(osg::NotifySeverity severity)
123{
124    if (s_NeedNotifyInit) osg::initNotifyLevel();
125    g_NotifyLevel = severity;
126}
127
128
129osg::NotifySeverity osg::getNotifyLevel()
130{
131    if (s_NeedNotifyInit) osg::initNotifyLevel();
132    return g_NotifyLevel;
133}
134
135void osg::setNotifyHandler(osg::NotifyHandler *handler)
136{
137    osg::NotifyStreamBuffer *buffer = static_cast<osg::NotifyStreamBuffer *>(g_NotifyStream->rdbuf());
138    if (buffer)
139        buffer->setNotifyHandler(handler);
140}
141
142osg::NotifyHandler* osg::getNotifyHandler()
143{
144    if (s_NeedNotifyInit) osg::initNotifyLevel();
145    osg::NotifyStreamBuffer *buffer = static_cast<osg::NotifyStreamBuffer *>(g_NotifyStream->rdbuf());
146    return buffer ? buffer->getNotifyHandler() : 0;
147}
148
149bool osg::initNotifyLevel()
150{
151    static osg::NullStream s_NullStream;
152    static osg::NotifyStream s_NotifyStream;
153
154    g_NullStream = &s_NullStream;
155    g_NotifyStream = &s_NotifyStream;
156
157    // g_NotifyLevel
158    // =============
159
160    g_NotifyLevel = osg::NOTICE; // Default value
161
162    char* OSGNOTIFYLEVEL=getenv("OSG_NOTIFY_LEVEL");
163    if (!OSGNOTIFYLEVEL) OSGNOTIFYLEVEL=getenv("OSGNOTIFYLEVEL");
164    if(OSGNOTIFYLEVEL)
165    {
166
167        std::string stringOSGNOTIFYLEVEL(OSGNOTIFYLEVEL);
168
169        // Convert to upper case
170        for(std::string::iterator i=stringOSGNOTIFYLEVEL.begin();
171            i!=stringOSGNOTIFYLEVEL.end();
172            ++i)
173        {
174            *i=toupper(*i);
175        }
176
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;
184        else if(stringOSGNOTIFYLEVEL.find("INFO")!=std::string::npos)       g_NotifyLevel=osg::INFO;
185        else std::cout << "Warning: invalid OSG_NOTIFY_LEVEL set ("<<stringOSGNOTIFYLEVEL<<")"<<std::endl;
186
187    }
188
189    // Setup standard notify handler
190    osg::NotifyStreamBuffer *buffer = dynamic_cast<osg::NotifyStreamBuffer *>(g_NotifyStream->rdbuf());
191    if (buffer && !buffer->getNotifyHandler())
192        buffer->setNotifyHandler(new StandardNotifyHandler);
193
194    s_NeedNotifyInit = false;
195
196    return true;
197
198}
199
200#ifndef OSG_NOTIFY_DISABLED
201bool osg::isNotifyEnabled( osg::NotifySeverity severity )
202{
203    if (s_NeedNotifyInit) osg::initNotifyLevel();
204    return severity<=g_NotifyLevel;
205}
206#endif
207
208std::ostream& osg::notify(const osg::NotifySeverity severity)
209{
210    if (s_NeedNotifyInit) osg::initNotifyLevel();
211
212    if (osg::isNotifyEnabled(severity))
213    {
214        g_NotifyStream->setCurrentSeverity(severity);
215        return *g_NotifyStream;
216    }
217    return *g_NullStream;
218}
219
220void osg::StandardNotifyHandler::notify(osg::NotifySeverity severity, const char *message)
221{
222#if 1
223    if (severity <= osg::WARN)
224        fputs(message, stderr);
225    else
226        fputs(message, stdout);
227#else
228   fputs(message, stdout);
229#endif
230}
231
232#if defined(WIN32) && !defined(__CYGWIN__)
233
234#ifndef WIN32_LEAN_AND_MEAN
235    #define WIN32_LEAN_AND_MEAN
236#endif
237#include <windows.h>
238
239void osg::WinDebugNotifyHandler::notify(osg::NotifySeverity severity, const char *message)
240{
241    OutputDebugStringA(message);
242}
243
244#endif
Note: See TracBrowser for help on using the browser.