BESLog.cc

Go to the documentation of this file.
00001 // BESLog.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 Atmostpheric 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 <iostream>
00034 #include <time.h>
00035 #include <string>
00036 #include <unistd.h>
00037 #include "BESLog.h"
00038 #include "TheBESKeys.h"
00039 #include "BESLogException.h"
00040 
00041 using std::cerr ;
00042 using std::endl ;
00043 using std::flush ;
00044 
00045 BESLog *BESLog::_instance = 0 ;
00046 
00062 BESLog::BESLog()
00063     : _flushed( 1 ),
00064       _file_buffer( 0 ),
00065       _suspended( 0 ),
00066       _verbose( false )
00067 {
00068     _suspended = 0 ;
00069     bool found = false ;
00070     _file_name = TheBESKeys::TheKeys()->get_key( "BES.LogName", found ) ;
00071     if( _file_name == "" )
00072     {
00073         string err = (string)"BES Fatal: unable to determine log file name."
00074                      + " Please set BES.LogName in your initialization file" ;
00075         cerr << err << endl ;
00076         throw BESLogException( err, __FILE__, __LINE__ ) ;
00077     }
00078     else
00079     {
00080         _file_buffer = new ofstream( _file_name.c_str(), ios::out | ios::app ) ;
00081         if( !(*_file_buffer) )
00082         {
00083             string err = (string)"BES Fatal; can not open log file "
00084                          + _file_name + "." ;
00085             cerr << err << endl ;
00086             throw BESLogException( err, __FILE__, __LINE__ ) ;
00087         } 
00088         /*
00089         if (_flushed)
00090         {
00091             dump_time();
00092             _flushed=0;
00093         }
00094         */
00095     }
00096     string verbose = TheBESKeys::TheKeys()->get_key( "BES.LogVerbose", found ) ;
00097     if( verbose == "YES" || verbose == "Yes" || verbose == "yes" )
00098     {
00099         _verbose = true ;
00100     }
00101 }
00102 
00107 BESLog:: ~BESLog()
00108 {
00109     _file_buffer->close();
00110     delete _file_buffer;
00111     _file_buffer = 0 ;
00112 }
00113 
00120 void
00121 BESLog::dump_time()
00122 {
00123     const time_t sctime=time(NULL);
00124     const struct tm *sttime=localtime(&sctime); 
00125     char zone_name[10];
00126     strftime(zone_name, sizeof(zone_name), "%Z", sttime);
00127     char *b=asctime(sttime);
00128     (*_file_buffer)<<"["<<zone_name<<" ";
00129     for (register int j=0; b[j]!='\n'; j++)
00130         (*_file_buffer)<<b[j];
00131     pid_t thepid = getpid() ;
00132     (*_file_buffer)<<" id: "<<thepid<<"] ";
00133     _flushed = 0 ;
00134 }
00135 
00140 BESLog& BESLog::operator<<(string &s) 
00141 {
00142     if (!_suspended)
00143     {
00144         if (_flushed)
00145             dump_time();
00146         (*_file_buffer) << s;
00147     }
00148     return *this;
00149 }
00150 
00155 BESLog& BESLog::operator<<(const string &s) 
00156 {
00157     if (!_suspended)
00158     {
00159         if (_flushed)
00160             dump_time();
00161         (*_file_buffer) << s;
00162     }
00163     return *this;
00164 }
00165 
00170 BESLog& BESLog::operator<<(char *val) 
00171 {
00172     if (!_suspended)
00173     {
00174         if (_flushed)
00175             dump_time();
00176         if( val )
00177             (*_file_buffer) << val;
00178         else
00179             (*_file_buffer) << "NULL" ;
00180     }
00181     return *this;
00182 }
00183 
00188 BESLog& BESLog::operator<<(const char *val) 
00189 {
00190     if (!_suspended)
00191     {
00192         if (_flushed)
00193         {
00194             dump_time();
00195         }
00196         if( val )
00197             (*_file_buffer) << val;
00198         else
00199             (*_file_buffer) << "NULL" ;
00200     }
00201     return *this;
00202 }
00203 
00208 BESLog& BESLog::operator<<(int val) 
00209 {
00210     if (!_suspended)
00211     {
00212         if (_flushed)
00213             dump_time();
00214         (*_file_buffer) << val;
00215     }
00216     return *this;
00217 }
00218 
00223 BESLog& BESLog::operator<<(char val) 
00224 { 
00225     if (!_suspended)
00226     {
00227         if (_flushed)
00228             dump_time();
00229         (*_file_buffer) << val;
00230     }
00231     return *this;
00232 }
00233 
00238 BESLog& BESLog::operator<<(long val) 
00239 {
00240     if (!_suspended)
00241     {
00242         if (_flushed)
00243             dump_time();
00244         (*_file_buffer) << val;
00245     }
00246     return *this;
00247 }
00248 
00253 BESLog& BESLog::operator<<(unsigned long val) 
00254 {
00255     if (!_suspended)
00256     {
00257         if (_flushed)
00258             dump_time();
00259         (*_file_buffer) << val;
00260     }
00261     return *this;
00262 }
00263 
00268 BESLog& BESLog::operator<<(double val) 
00269 {
00270     if (!_suspended)
00271     {
00272         if (_flushed)
00273             dump_time();
00274         (*_file_buffer) << val;
00275     }
00276     return *this;
00277 }
00278 
00286 BESLog& BESLog::operator<<(p_ostream_manipulator val) 
00287 {
00288     if (!_suspended)
00289     {
00290         (*_file_buffer) << val ;
00291         if ((val==(p_ostream_manipulator)endl) || (val==(p_ostream_manipulator)flush))
00292             _flushed=1;
00293     }
00294     return *this;
00295 }
00296 
00303 BESLog& BESLog::operator<<(p_ios_manipulator val) 
00304 { 
00305     if (!_suspended)
00306         (*_file_buffer)<<val;
00307     return *this;
00308 }
00309 
00317 void
00318 BESLog::dump( ostream &strm ) const
00319 {
00320     strm << BESIndent::LMarg << "BESLog::dump - ("
00321                              << (void *)this << ")" << endl ;
00322     BESIndent::Indent() ;
00323     strm << BESIndent::LMarg << "log file: " << _file_name << endl ;
00324     if( _file_buffer && *_file_buffer )
00325     {
00326         strm << BESIndent::LMarg << "log is valid" << endl ;
00327     }
00328     else
00329     {
00330         strm << BESIndent::LMarg << "log is NOT valid" << endl ;
00331     }
00332     strm << BESIndent::LMarg << "is verbose: " << _verbose << endl ;
00333     strm << BESIndent::LMarg << "is flushed: " << _flushed << endl ;
00334     strm << BESIndent::LMarg << "is suspended: " << _suspended << endl ;
00335     BESIndent::UnIndent() ;
00336 }
00337 
00338 BESLog *
00339 BESLog::TheLog()
00340 {
00341     if( _instance == 0 )
00342     {
00343         _instance = new BESLog ;
00344     }
00345     return _instance ;
00346 }
00347 

Generated on Wed Aug 29 02:59:01 2007 for OPeNDAP Back End Server (BES) by  doxygen 1.5.2