osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/Notify.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003  * Copyright 2008-2010 Pelican Mapping
00004  * http://osgearth.org
00005  *
00006  * osgEarth is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 #include <osgEarth/Notify>
00020 
00021 using namespace osgEarth;
00022 
00023 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
00024  *
00025  * This library is open source and may be redistributed and/or modified under  
00026  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
00027  * (at your option) any later version.  The full license is in LICENSE file
00028  * included with this distribution, and on the openscenegraph.org website.
00029  * 
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00033  * OpenSceneGraph Public License for more details.
00034 */
00035 #include <osgEarth/Notify>
00036 #include <string>
00037 #include <stdlib.h>
00038 #include <iostream>
00039 #include <fstream>
00040 #include <cctype>
00041 
00042 using namespace std;
00043 
00044 osg::NotifySeverity osgearth_g_NotifyLevel = osg::NOTICE;
00045 
00046 void
00047 osgEarth::setNotifyLevel(osg::NotifySeverity severity)
00048 {
00049     osgEarth::initNotifyLevel();
00050     osgearth_g_NotifyLevel = severity;
00051 }
00052 
00053 osg::NotifySeverity
00054 osgEarth::getNotifyLevel()
00055 {
00056     osgEarth::initNotifyLevel();
00057     return osgearth_g_NotifyLevel;
00058 }
00059 
00060 bool
00061 osgEarth::initNotifyLevel()
00062 {
00063     static bool s_NotifyInit = false;
00064 
00065     if (s_NotifyInit) return true;
00066     
00067     // g_NotifyLevel
00068     // =============
00069 
00070     osgearth_g_NotifyLevel = osg::NOTICE; // Default value
00071 
00072     char* OSGNOTIFYLEVEL=getenv("OSGEARTH_NOTIFY_LEVEL");
00073     if (!OSGNOTIFYLEVEL) OSGNOTIFYLEVEL=getenv("OSGEARTHNOTIFYLEVEL");
00074     if(OSGNOTIFYLEVEL)
00075     {
00076 
00077         std::string stringOSGNOTIFYLEVEL(OSGNOTIFYLEVEL);
00078 
00079         // Convert to upper case
00080         for(std::string::iterator i=stringOSGNOTIFYLEVEL.begin();
00081             i!=stringOSGNOTIFYLEVEL.end();
00082             ++i)
00083         {
00084             *i=toupper(*i);
00085         }
00086 
00087         if(stringOSGNOTIFYLEVEL.find("ALWAYS")!=std::string::npos)          osgearth_g_NotifyLevel=osg::ALWAYS;
00088         else if(stringOSGNOTIFYLEVEL.find("FATAL")!=std::string::npos)      osgearth_g_NotifyLevel=osg::FATAL;
00089         else if(stringOSGNOTIFYLEVEL.find("WARN")!=std::string::npos)       osgearth_g_NotifyLevel=osg::WARN;
00090         else if(stringOSGNOTIFYLEVEL.find("NOTICE")!=std::string::npos)     osgearth_g_NotifyLevel=osg::NOTICE;
00091         else if(stringOSGNOTIFYLEVEL.find("DEBUG_INFO")!=std::string::npos) osgearth_g_NotifyLevel=osg::DEBUG_INFO;
00092         else if(stringOSGNOTIFYLEVEL.find("DEBUG_FP")!=std::string::npos)   osgearth_g_NotifyLevel=osg::DEBUG_FP;
00093         else if(stringOSGNOTIFYLEVEL.find("DEBUG")!=std::string::npos)      osgearth_g_NotifyLevel=osg::DEBUG_INFO;
00094         else if(stringOSGNOTIFYLEVEL.find("INFO")!=std::string::npos)       osgearth_g_NotifyLevel=osg::INFO;
00095         else std::cout << "Warning: invalid OSG_NOTIFY_LEVEL set ("<<stringOSGNOTIFYLEVEL<<")"<<std::endl;
00096  
00097     }
00098 
00099     s_NotifyInit = true;
00100 
00101     return true;
00102 
00103 }
00104 
00105 bool
00106 osgEarth::isNotifyEnabled( osg::NotifySeverity severity )
00107 {
00108     return severity<=getNotifyLevel();
00109 }
00110 
00111 class NullStreamBuffer : public std::streambuf
00112 {
00113     private:
00114     
00115         virtual streamsize xsputn (const char_type*, streamsize n)
00116         {
00117             return n;
00118         }
00119 };
00120 
00121 struct NullStream : public std::ostream
00122 {
00123     NullStream():
00124         std::ostream(new NullStreamBuffer) {}
00125         
00126     virtual ~NullStream()
00127     {
00128         delete rdbuf();
00129         rdbuf(0);
00130     }
00131 };
00132 
00133 std::ostream&
00134 osgEarth::notify(const osg::NotifySeverity severity)
00135 {
00136     // set up global notify null stream for inline notify
00137     static NullStream s_NotifyNulStream;
00138 
00139     static bool initialized = false;
00140     if (!initialized) 
00141     {
00142         std::cerr<<""; // dummy op to force construction of cerr, before a reference is passed back to calling code.
00143         std::cout<<""; // dummy op to force construction of cout, before a reference is passed back to calling code.
00144         initialized = osgEarth::initNotifyLevel();
00145     }
00146 
00147     if (severity<=osgearth_g_NotifyLevel)
00148     {
00149         if (severity<=osg::WARN) return std::cerr;
00150         else return std::cout;
00151     }
00152     return s_NotifyNulStream;
00153 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines