BESDebug.cc

Go to the documentation of this file.
00001 // BESDebug.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #include <fstream>
00034 #include <iostream>
00035 
00036 using std::ofstream ;
00037 using std::ios ;
00038 using std::cout ;
00039 using std::endl ;
00040 
00041 #include "BESDebug.h"
00042 #include "BESInternalError.h"
00043 
00044 ostream *BESDebug::_debug_strm = NULL ;
00045 bool BESDebug::_debug_strm_created = false ;
00046 map<string,bool> BESDebug::_debug_map ;
00047 
00060 void
00061 BESDebug::SetUp( const string &values )
00062 {
00063     if( values.empty() )
00064     {
00065         string err = "Empty debug options" ;
00066         throw BESInternalError( err, __FILE__, __LINE__ ) ;
00067     }
00068     string::size_type comma = 0 ;
00069     comma = values.find( ',' ) ;
00070     if( comma == string::npos )
00071     {
00072         string err = "Missing comma in debug options: " + values ;
00073         throw BESInternalError( err, __FILE__, __LINE__ ) ;
00074     }
00075     ostream *strm = 0 ;
00076     bool created = false ;
00077     string s_strm = values.substr( 0, comma ) ;
00078     if( s_strm == "cerr" )
00079     {
00080         strm = &cerr ;
00081     }
00082     else
00083     {
00084         strm = new ofstream( s_strm.c_str(), ios::out ) ;
00085         if( !(*strm) )
00086         {
00087             if( strm )
00088             {
00089                 delete strm ;
00090                 strm = 0 ;
00091             }
00092             string err = "Unable to open the debug file: " + s_strm ;
00093             throw BESInternalError( err, __FILE__, __LINE__ ) ;
00094         }
00095         created = true ;
00096     }
00097 
00098     BESDebug::SetStrm( strm, created ) ;
00099 
00100     string::size_type new_comma = 0 ;
00101     while( ( new_comma = values.find( ',', comma+1 ) ) != string::npos )
00102     {
00103         string flagName = values.substr( comma+1, new_comma-comma-1 ) ;
00104         if( flagName[0] == '-' )
00105         {
00106             string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
00107             BESDebug::Set( newflag, false ) ;
00108         }
00109         else
00110             BESDebug::Set( flagName, true ) ;
00111         comma = new_comma ;
00112     }
00113     string flagName = values.substr( comma+1, values.length()-comma-1 ) ;
00114     if( flagName[0] == '-' )
00115     {
00116         string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
00117         BESDebug::Set( newflag, false ) ;
00118     }
00119     else
00120         BESDebug::Set( flagName, true ) ;
00121 }
00122 
00130 void
00131 BESDebug::Help( ostream &strm )
00132 {
00133     strm << "Debug help:" << endl
00134          << endl
00135          << "Set on the command line with "
00136          << "-d \"file_name|cerr,[-]context1,[-]context2,...,[-]contextn\"" << endl
00137          << "  context with dash (-) in front will be turned off" << endl
00138          << endl
00139          << "Possible context:" << endl ;
00140 
00141     if( _debug_map.size() )
00142     {
00143         BESDebug::_debug_citer i = _debug_map.begin() ;
00144         BESDebug::_debug_citer e = _debug_map.end() ;
00145         for( ; i != e; i++ )
00146         {
00147             strm << "  " << (*i).first << ": " ;
00148             if( (*i).second )
00149                 strm << "on" << endl ;
00150             else
00151                 strm << "off" << endl ;
00152         }
00153     }
00154     else
00155     {
00156         strm << "  none specified" << endl ;
00157     }
00158 }
00159 

Generated on Tue Mar 4 23:13:34 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.1