OPeNDAP Hyrax Back End Server (BES) Updated for version 3.8.3

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-2009 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 "config.h"
00034 
00035 #include <time.h>
00036 
00037 #include <fstream>
00038 #include <iostream>
00039 #include <sstream>
00040 
00041 using std::ofstream ;
00042 using std::ios ;
00043 using std::cout ;
00044 using std::endl ;
00045 using std::ostringstream ;
00046 
00047 #include "BESDebug.h"
00048 #include "BESInternalError.h"
00049 
00050 ostream *BESDebug::_debug_strm = NULL ;
00051 bool BESDebug::_debug_strm_created = false ;
00052 map<string,bool> BESDebug::_debug_map ;
00053 
00066 void
00067 BESDebug::SetUp( const string &values )
00068 {
00069     if( values.empty() )
00070     {
00071         string err = "Empty debug options" ;
00072         throw BESInternalError( err, __FILE__, __LINE__ ) ;
00073     }
00074     string::size_type comma = 0 ;
00075     comma = values.find( ',' ) ;
00076     if( comma == string::npos )
00077     {
00078         string err = "Missing comma in debug options: " + values ;
00079         throw BESInternalError( err, __FILE__, __LINE__ ) ;
00080     }
00081     ostream *strm = 0 ;
00082     bool created = false ;
00083     string s_strm = values.substr( 0, comma ) ;
00084     if( s_strm == "cerr" )
00085     {
00086         strm = &cerr ;
00087     }
00088     else
00089     {
00090         strm = new ofstream( s_strm.c_str(), ios::out ) ;
00091         if( strm && !(*strm) )
00092         {
00093                 delete strm ;
00094                 strm = 0 ;
00095             string err = "Unable to open the debug file: " + s_strm ;
00096             throw BESInternalError( err, __FILE__, __LINE__ ) ;
00097         }
00098         created = true ;
00099     }
00100 
00101     BESDebug::SetStrm( strm, created ) ;
00102 
00103     string::size_type new_comma = 0 ;
00104     while( ( new_comma = values.find( ',', comma+1 ) ) != string::npos )
00105     {
00106         string flagName = values.substr( comma+1, new_comma-comma-1 ) ;
00107         if( flagName[0] == '-' )
00108         {
00109             string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
00110             BESDebug::Set( newflag, false ) ;
00111         }
00112         else
00113         {
00114             BESDebug::Set( flagName, true ) ;
00115         }
00116         comma = new_comma ;
00117     }
00118     string flagName = values.substr( comma+1, values.length()-comma-1 ) ;
00119     if( flagName[0] == '-' )
00120     {
00121         string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
00122         BESDebug::Set( newflag, false ) ;
00123     }
00124     else
00125     {
00126         BESDebug::Set( flagName, true ) ;
00127     }
00128 }
00129 
00134 string
00135 BESDebug::GetPidStr()
00136 {
00137     ostringstream strm ;
00138     const time_t sctime = time( NULL ) ;
00139     const struct tm *sttime = localtime( &sctime ) ; 
00140     char zone_name[10] ;
00141     strftime( zone_name, sizeof( zone_name ), "%Z", sttime ) ;
00142     char *b = asctime( sttime ) ;
00143     strm << zone_name << " " ;
00144     for( register int j = 0; b[j] != '\n'; j++ )
00145         strm << b[j] ;
00146     pid_t thepid = getpid() ;
00147     strm << " id: " << thepid ;
00148     return strm.str() ;
00149 }
00150 
00159 void
00160 BESDebug::Help( ostream &strm )
00161 {
00162     strm << "Debug help:" << endl
00163          << "  Set on the command line with "
00164          << "-d \"file_name|cerr,[-]context1,...,[-]contextn\"" << endl
00165          << "  context with dash (-) in front will be turned off" << endl
00166          << "  context of all will turn on debugging for all context" << endl
00167          << endl
00168          << "Possible context:" << endl ;
00169 
00170     if( _debug_map.size() )
00171     {
00172         BESDebug::_debug_citer i = _debug_map.begin() ;
00173         BESDebug::_debug_citer e = _debug_map.end() ;
00174         for( ; i != e; i++ )
00175         {
00176             strm << "  " << (*i).first << ": " ;
00177             if( (*i).second )
00178                 strm << "on" << endl ;
00179             else
00180                 strm << "off" << endl ;
00181         }
00182     }
00183     else
00184     {
00185         strm << "  none specified" << endl ;
00186     }
00187 }
00188