OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESLog.cc
Go to the documentation of this file.
1 // BESLog.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "config.h"
34 
35 #include <iostream>
36 #include <time.h>
37 #include <string>
38 
39 #include "BESLog.h"
40 #include "TheBESKeys.h"
41 #include "BESInternalFatalError.h"
42 
43 #if HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 
47 using std::cerr ;
48 using std::endl ;
49 using std::flush ;
50 
51 BESLog *BESLog::_instance = 0 ;
52 
69  : _flushed( 1 ),
70  _file_buffer( 0 ),
71  _suspended( 0 ),
72  _verbose( false )
73 {
74  _suspended = 0 ;
75  bool found = false ;
76  try
77  {
78  TheBESKeys::TheKeys()->get_value( "BES.LogName", _file_name, found ) ;
79  }
80  catch( ... )
81  {
82  string err = (string)"BES Fatal: unable to determine log file name."
83  + " The key BES.LogName has multiple values" ;
84  cerr << err << endl ;
85  throw BESInternalFatalError( err, __FILE__, __LINE__ ) ;
86  }
87  if( _file_name == "" )
88  {
89  string err = (string)"BES Fatal: unable to determine log file name."
90  + " Please set BES.LogName in your initialization file" ;
91  cerr << err << endl ;
92  throw BESInternalFatalError( err, __FILE__, __LINE__ ) ;
93  }
94  _file_buffer = new ofstream( _file_name.c_str(), ios::out | ios::app ) ;
95  if( !(*_file_buffer) )
96  {
97  string err = (string)"BES Fatal; cannot open log file "
98  + _file_name + "." ;
99  cerr << err << endl ;
100  throw BESInternalFatalError( err, __FILE__, __LINE__ ) ;
101  }
102  found = false ;
103  string verbose ;
104  TheBESKeys::TheKeys()->get_value( "BES.LogVerbose", verbose, found ) ;
105  if( verbose == "YES" || verbose == "Yes" || verbose == "yes" )
106  {
107  _verbose = true ;
108  }
109 }
110 
116 {
117  _file_buffer->close();
118  delete _file_buffer;
119  _file_buffer = 0 ;
120 }
121 
128 void
130 {
131  const time_t sctime=time(NULL);
132  const struct tm *sttime=localtime(&sctime);
133  char zone_name[10];
134  strftime(zone_name, sizeof(zone_name), "%Z", sttime);
135  char *b=asctime(sttime);
136  (*_file_buffer)<<"["<<zone_name<<" ";
137  for (register int j=0; b[j]!='\n'; j++)
138  (*_file_buffer)<<b[j];
139  pid_t thepid = getpid() ;
140  (*_file_buffer)<<" id: "<<thepid<<"] ";
141  _flushed = 0 ;
142 }
143 
149 {
150  if (!_suspended)
151  {
152  if (_flushed)
153  dump_time();
154  (*_file_buffer) << s;
155  }
156  return *this;
157 }
158 
163 BESLog& BESLog::operator<<(const string &s)
164 {
165  if (!_suspended)
166  {
167  if (_flushed)
168  dump_time();
169  (*_file_buffer) << s;
170  }
171  return *this;
172 }
173 
179 {
180  if (!_suspended)
181  {
182  if (_flushed)
183  dump_time();
184  if( val )
185  (*_file_buffer) << val;
186  else
187  (*_file_buffer) << "NULL" ;
188  }
189  return *this;
190 }
191 
196 BESLog& BESLog::operator<<(const char *val)
197 {
198  if (!_suspended)
199  {
200  if (_flushed)
201  {
202  dump_time();
203  }
204  if( val )
205  (*_file_buffer) << val;
206  else
207  (*_file_buffer) << "NULL" ;
208  }
209  return *this;
210 }
211 
217 {
218  if (!_suspended)
219  {
220  if (_flushed)
221  dump_time();
222  (*_file_buffer) << val;
223  }
224  return *this;
225 }
226 
232 {
233  if (!_suspended)
234  {
235  if (_flushed)
236  dump_time();
237  (*_file_buffer) << val;
238  }
239  return *this;
240 }
241 
247 {
248  if (!_suspended)
249  {
250  if (_flushed)
251  dump_time();
252  (*_file_buffer) << val;
253  }
254  return *this;
255 }
256 
261 BESLog& BESLog::operator<<(unsigned long val)
262 {
263  if (!_suspended)
264  {
265  if (_flushed)
266  dump_time();
267  (*_file_buffer) << val;
268  }
269  return *this;
270 }
271 
277 {
278  if (!_suspended)
279  {
280  if (_flushed)
281  dump_time();
282  (*_file_buffer) << val;
283  }
284  return *this;
285 }
286 
294 BESLog& BESLog::operator<<(p_ostream_manipulator val)
295 {
296  if (!_suspended)
297  {
298  (*_file_buffer) << val ;
299  if ((val==(p_ostream_manipulator)endl) || (val==(p_ostream_manipulator)flush))
300  _flushed=1;
301  }
302  return *this;
303 }
304 
311 BESLog& BESLog::operator<<(p_ios_manipulator val)
312 {
313  if (!_suspended)
314  (*_file_buffer)<<val;
315  return *this;
316 }
317 
325 void
326 BESLog::dump( ostream &strm ) const
327 {
328  strm << BESIndent::LMarg << "BESLog::dump - ("
329  << (void *)this << ")" << endl ;
331  strm << BESIndent::LMarg << "log file: " << _file_name << endl ;
332  if( _file_buffer && *_file_buffer )
333  {
334  strm << BESIndent::LMarg << "log is valid" << endl ;
335  }
336  else
337  {
338  strm << BESIndent::LMarg << "log is NOT valid" << endl ;
339  }
340  strm << BESIndent::LMarg << "is verbose: " << _verbose << endl ;
341  strm << BESIndent::LMarg << "is flushed: " << _flushed << endl ;
342  strm << BESIndent::LMarg << "is suspended: " << _suspended << endl ;
344 }
345 
346 BESLog *
348 {
349  if( _instance == 0 )
350  {
351  _instance = new BESLog ;
352  }
353  return _instance ;
354 }
355 
exception thrown if an internal error is found and is fatal to the BES
~BESLog()
Cleans up the logging mechanism.
Definition: BESLog.cc:115
ostream &(* p_ostream_manipulator)(ostream &)
Defines a data type p_ostream_manipulator "pointer to function that takes ostream& and returns ostrea...
Definition: BESLog.h:170
BESLog & operator<<(string &)
Overloaded inserter that writes the specified string.
Definition: BESLog.cc:148
static void Indent()
Definition: BESIndent.cc:38
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:453
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESLog.cc:326
static BESLog * TheLog()
Definition: BESLog.cc:347
void dump_time()
Protected method that dumps the date/time to the log file.
Definition: BESLog.cc:129
BESLog()
constructor that sets up logging for the application.
Definition: BESLog.cc:68
Provides a mechanism for applications to log information to an external file.
Definition: BESLog.h:87
static void UnIndent()
Definition: BESIndent.cc:44
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:48