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