OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESDebug.cc
Go to the documentation of this file.
1 // BESDebug.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <unistd.h>
34 #include "config.h"
35 
36 #include <time.h>
37 
38 #include <fstream>
39 #include <iostream>
40 #include <sstream>
41 
42 using std::ofstream ;
43 using std::ios ;
44 using std::cout ;
45 using std::endl ;
46 using std::ostringstream ;
47 
48 #include "BESDebug.h"
49 #include "BESInternalError.h"
50 
51 ostream *BESDebug::_debug_strm = NULL ;
52 bool BESDebug::_debug_strm_created = false ;
53 map<string,bool> BESDebug::_debug_map ;
54 
67 void
68 BESDebug::SetUp( const string &values )
69 {
70  if( values.empty() )
71  {
72  string err = "Empty debug options" ;
73  throw BESInternalError( err, __FILE__, __LINE__ ) ;
74  }
75  string::size_type comma = 0 ;
76  comma = values.find( ',' ) ;
77  if( comma == string::npos )
78  {
79  string err = "Missing comma in debug options: " + values ;
80  throw BESInternalError( err, __FILE__, __LINE__ ) ;
81  }
82  ostream *strm = 0 ;
83  bool created = false ;
84  string s_strm = values.substr( 0, comma ) ;
85  if( s_strm == "cerr" )
86  {
87  strm = &cerr ;
88  }
89  else
90  {
91  strm = new ofstream( s_strm.c_str(), ios::out ) ;
92  if( strm && !(*strm) )
93  {
94  delete strm ;
95  strm = 0 ;
96  string err = "Unable to open the debug file: " + s_strm ;
97  throw BESInternalError( err, __FILE__, __LINE__ ) ;
98  }
99  created = true ;
100  }
101 
102  BESDebug::SetStrm( strm, created ) ;
103 
104  string::size_type new_comma = 0 ;
105  while( ( new_comma = values.find( ',', comma+1 ) ) != string::npos )
106  {
107  string flagName = values.substr( comma+1, new_comma-comma-1 ) ;
108  if( flagName[0] == '-' )
109  {
110  string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
111  BESDebug::Set( newflag, false ) ;
112  }
113  else
114  {
115  BESDebug::Set( flagName, true ) ;
116  }
117  comma = new_comma ;
118  }
119  string flagName = values.substr( comma+1, values.length()-comma-1 ) ;
120  if( flagName[0] == '-' )
121  {
122  string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
123  BESDebug::Set( newflag, false ) ;
124  }
125  else
126  {
127  BESDebug::Set( flagName, true ) ;
128  }
129 }
130 
135 string
137 {
138  ostringstream strm ;
139  const time_t sctime = time( NULL ) ;
140  const struct tm *sttime = localtime( &sctime ) ;
141  char zone_name[10] ;
142  strftime( zone_name, sizeof( zone_name ), "%Z", sttime ) ;
143  char *b = asctime( sttime ) ;
144  strm << zone_name << " " ;
145  for( register int j = 0; b[j] != '\n'; j++ )
146  strm << b[j] ;
147  pid_t thepid = getpid() ;
148  strm << " id: " << thepid ;
149  return strm.str() ;
150 }
151 
160 void
161 BESDebug::Help( ostream &strm )
162 {
163  strm << "Debug help:" << endl
164  << " Set on the command line with "
165  << "-d \"file_name|cerr,[-]context1,...,[-]contextn\"" << endl
166  << " context with dash (-) in front will be turned off" << endl
167  << " context of all will turn on debugging for all context" << endl
168  << endl
169  << "Possible context:" << endl ;
170 
171  if( _debug_map.size() )
172  {
173  BESDebug::_debug_citer i = _debug_map.begin() ;
174  BESDebug::_debug_citer e = _debug_map.end() ;
175  for( ; i != e; i++ )
176  {
177  strm << " " << (*i).first << ": " ;
178  if( (*i).second )
179  strm << "on" << endl ;
180  else
181  strm << "off" << endl ;
182  }
183  }
184  else
185  {
186  strm << " none specified" << endl ;
187  }
188 }
189 
exception thrown if inernal error encountered
static void SetUp(const string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:68
static void Set(const string &flagName, bool value)
set the debug context to the specified value
Definition: BESDebug.h:104
static string GetPidStr()
return the pid as a string
Definition: BESDebug.cc:136
static void Help(ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition: BESDebug.cc:161
static void SetStrm(ostream *strm, bool created)
set the debug output stream to the specified stream
Definition: BESDebug.h:185