bes  Updated for version 3.17.0
BESLog.cc
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 namespace std;
48 
49 BESLog *BESLog::_instance = 0;
50 
67  _flushed(1), _file_buffer(0), _suspended(0), _verbose(false)
68 {
69  _suspended = 0;
70  bool found = false;
71  try {
72  TheBESKeys::TheKeys()->get_value("BES.LogName", _file_name, found);
73  }
74  catch (...) {
75  string err = (string) "BES Fatal: unable to determine log file name."
76  + " The key BES.LogName has multiple values";
77  cerr << err << endl;
78  throw BESInternalFatalError(err, __FILE__, __LINE__);
79  }
80  if (_file_name == "") {
81  string err = (string) "BES Fatal: unable to determine log file name."
82  + " Please set BES.LogName in your initialization file";
83  cerr << err << endl;
84  throw BESInternalFatalError(err, __FILE__, __LINE__);
85  }
86  _file_buffer = new ofstream(_file_name.c_str(), ios::out | ios::app);
87  if (!(*_file_buffer)) {
88  string err = (string) "BES Fatal; cannot open log file " + _file_name + ".";
89  cerr << err << endl;
90  throw BESInternalFatalError(err, __FILE__, __LINE__);
91  }
92  found = false;
93  string verbose;
94  TheBESKeys::TheKeys()->get_value("BES.LogVerbose", verbose, found);
95  if (verbose == "YES" || verbose == "Yes" || verbose == "yes") {
96  _verbose = true;
97  }
98 }
99 
105 {
106  _file_buffer->close();
107  delete _file_buffer;
108  _file_buffer = 0;
109 }
110 
118 {
119  const time_t sctime = time(NULL);
120  const struct tm *sttime = localtime(&sctime);
121  char zone_name[10];
122  strftime(zone_name, sizeof(zone_name), "%Z", sttime);
123  char *b = asctime(sttime);
124  (*_file_buffer) << "[" << zone_name << " ";
125  for (register int j = 0; b[j] != '\n'; j++)
126  (*_file_buffer) << b[j];
127  pid_t thepid = getpid();
128  (*_file_buffer) << " id: " << thepid << "] ";
129  _flushed = 0;
130 }
131 
139 {
140  if (!_suspended) {
141  if (_flushed) dump_time();
142  (*_file_buffer) << s;
143  }
144  return *this;
145 }
146 
151 BESLog& BESLog::operator<<(const string &s)
152 {
153  if (!_suspended) {
154  if (_flushed) dump_time();
155  (*_file_buffer) << s;
156  }
157  return *this;
158 }
159 
165 {
166  if (!_suspended) {
167  if (_flushed) dump_time();
168  if (val)
169  (*_file_buffer) << val;
170  else
171  (*_file_buffer) << "NULL";
172  }
173  return *this;
174 }
175 
180 BESLog& BESLog::operator<<(const char *val)
181 {
182  if (!_suspended) {
183  if (_flushed) {
184  dump_time();
185  }
186  if (val)
187  (*_file_buffer) << val;
188  else
189  (*_file_buffer) << "NULL";
190  }
191  return *this;
192 }
193 
199 {
200  if (!_suspended) {
201  if (_flushed) dump_time();
202  (*_file_buffer) << val;
203  }
204  return *this;
205 }
206 
212 {
213  if (!_suspended) {
214  if (_flushed) dump_time();
215  (*_file_buffer) << val;
216  }
217  return *this;
218 }
219 
225 {
226  if (!_suspended) {
227  if (_flushed) dump_time();
228  (*_file_buffer) << val;
229  }
230  return *this;
231 }
232 
237 BESLog& BESLog::operator<<(unsigned long val)
238 {
239  if (!_suspended) {
240  if (_flushed) dump_time();
241  (*_file_buffer) << val;
242  }
243  return *this;
244 }
245 
251 {
252  if (!_suspended) {
253  if (_flushed) dump_time();
254  (*_file_buffer) << val;
255  }
256  return *this;
257 }
258 
267 {
268  if (!_suspended) {
269  (*_file_buffer) << val;
270  if ((val == (p_ostream_manipulator) endl) || (val == (p_ostream_manipulator) flush)) _flushed = 1;
271  }
272  return *this;
273 }
274 
282 {
283  if (!_suspended) (*_file_buffer) << val;
284  return *this;
285 }
286 
294 void BESLog::dump(ostream &strm) const
295 {
296  strm << BESIndent::LMarg << "BESLog::dump - (" << (void *) this << ")" << endl;
297  BESIndent::Indent();
298  strm << BESIndent::LMarg << "log file: " << _file_name << endl;
299  if (_file_buffer && *_file_buffer) {
300  strm << BESIndent::LMarg << "log is valid" << endl;
301  }
302  else {
303  strm << BESIndent::LMarg << "log is NOT valid" << endl;
304  }
305  strm << BESIndent::LMarg << "is verbose: " << _verbose << endl;
306  strm << BESIndent::LMarg << "is flushed: " << _flushed << endl;
307  strm << BESIndent::LMarg << "is suspended: " << _suspended << endl;
308  BESIndent::UnIndent();
309 }
310 
311 BESLog *
312 BESLog::TheLog()
313 {
314  if (_instance == 0) {
315  _instance = new BESLog;
316  }
317  return _instance;
318 }
319 
exception thrown if an internal error is found and is fatal to the BES
~BESLog()
Cleans up the logging mechanism.
Definition: BESLog.cc:104
BESLog & operator<<(std::string &)
Overloaded inserter that writes the specified string.
Definition: BESLog.cc:138
STL namespace.
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESLog.cc:294
std::ostream &(* p_ostream_manipulator)(std::ostream &)
Defines a data type p_std::ostream_manipulator "pointer to function that takes std::ostream& and retu...
Definition: BESLog.h:169
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:481
void dump_time()
Protected method that dumps the date/time to the log file.
Definition: BESLog.cc:117
BESLog()
constructor that sets up logging for the application.
Definition: BESLog.cc:66
Provides a mechanism for applications to log information to an external file.
Definition: BESLog.h:87
std::ios &(* p_ios_manipulator)(std::ios &)
Defines a data type p_ios_manipulator "pointer to function that takes ios& and returns ios&"...
Definition: BESLog.h:167
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:43