bes  Updated for version 3.20.6
BESLog.h
1 // BESLog.h
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 #ifndef BESLog_h_
34 #define BESLog_h_ 1
35 
36 #include <fstream>
37 #include <string>
38 
39 // Note that the BESLog::operator<<() methods will prefix output with
40 // the time and PID by checking for the flush and endl stream operators.
41 //
42 // TRACE_LOGGING provides a way to see just where in the code the log info
43 // is written from. jhrg 11/14/17
44 
45 #undef TRACE_LOGGING
46 
47 #ifdef TRACE_LOGGING
48 #define LOG(x) do { *(BESLog::TheLog()) << __FILE__ << ":" << __LINE__ << " - " << x ; BESLog::TheLog()->flush_me() ; } while( 0 )
49 #define VERBOSE(x) do { if (BESLog::TheLog()->is_verbose()) *(BESLog::TheLog()) << __FILE__ << ":" << __LINE__ << " - " << x ; BESLog::TheLog()->flush_me() ; } while( 0 )
50 #else
51 #define LOG(x) do { *(BESLog::TheLog()) << x ; BESLog::TheLog()->flush_me() ; } while( 0 )
52 #define VERBOSE(x) do { if (BESLog::TheLog()->is_verbose()) *(BESLog::TheLog()) << x ; BESLog::TheLog()->flush_me() ; } while( 0 )
53 #endif
54 
55 // Pretty silly - for now ERROR is the same as LOG, but I suspect that we might
56 // want to treat errors differently in the near future given the special logging
57 // needs of the 'Hyrax in the Cloud' project. jhrg 11/16/17
58 #define ERROR(x) LOG(x)
59 
60 #include "BESObj.h"
61 
103 class BESLog: public BESObj {
104 private:
105  static BESLog * d_instance;
106 
107  int d_flushed;
108  std::ofstream * d_file_buffer;
109  std::string d_file_name;
110 
111  // Flag to indicate the object is not routing data to its associated stream
112  int d_suspended;
113 
114  // Flag to indicate whether to log verbose messages
115  bool d_verbose;
116 
117  bool d_use_local_time;
118 
119 protected:
120  BESLog();
121 
122  // Dumps the current system time.
123  void dump_time();
124 public:
125  ~BESLog();
126 
127  const static std::string mark;
128 
134  void suspend()
135  {
136  d_suspended = 1;
137  }
138 
144  void resume()
145  {
146  d_suspended = 0;
147  }
148 
155  void verbose_on()
156  {
157  d_verbose = true;
158  }
159 
165  void verbose_off()
166  {
167  d_verbose = false;
168  }
169 
185  bool is_verbose()
186  {
187  return d_verbose;
188  }
189 
191  typedef std::ios& (*p_ios_manipulator)(std::ios&);
193  typedef std::ostream& (*p_ostream_manipulator)(std::ostream&);
194 
195  BESLog& operator <<(std::string&);
196  BESLog& operator <<(const std::string&);
197  BESLog& operator <<(char*);
198  BESLog& operator <<(const char*);
199  BESLog& operator <<(int);
200  BESLog& operator <<(unsigned int);
201  BESLog& operator <<(char);
202  BESLog& operator <<(long);
203  BESLog& operator <<(unsigned long);
204  BESLog& operator <<(double);
205 
208 
209  virtual void dump(std::ostream &strm) const;
210 
211  virtual void flush_me();
212 
213  static BESLog *TheLog();
214 
215  // I added this so that it's easy to route the BESDebug messages to the
216  // log file. This will enable the Admin Interface to display the contents
217  // of those debug messages when it displays the log file. jhrg
218  std::ostream *get_log_ostream()
219  {
220  return d_file_buffer;
221  }
222 };
223 
224 #endif // BESLog_h_
225 
BESLog::is_verbose
bool is_verbose()
Returns true if verbose logging is requested.
Definition: BESLog.h:185
BESLog::verbose_off
void verbose_off()
turns off verbose logging
Definition: BESLog.h:165
BESLog::dump
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESLog.cc:360
BESLog::operator<<
BESLog & operator<<(std::string &)
Overloaded inserter that writes the specified string.
Definition: BESLog.cc:189
BESLog::~BESLog
~BESLog()
Cleans up the logging mechanism.
Definition: BESLog.cc:131
BESLog::suspend
void suspend()
Suspend logging of any information until resumed.
Definition: BESLog.h:134
BESLog::resume
void resume()
Resumes logging after being suspended.
Definition: BESLog.h:144
BESLog::p_ostream_manipulator
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:193
BESObj
top level BES object to house generic methods
Definition: BESObj.h:49
BESLog::verbose_on
void verbose_on()
turn on verbose logging
Definition: BESLog.h:155
BESLog::p_ios_manipulator
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:191
BESLog
Provides a mechanism for applications to log information to an external file.
Definition: BESLog.h:103
BESLog::BESLog
BESLog()
constructor that sets up logging for the application.
Definition: BESLog.cc:72
BESLog::dump_time
void dump_time()
Protected method that dumps the date/time to the log file.
Definition: BESLog.cc:145