bes  Updated for version 3.20.10
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 "config.h"
37 
38 #include <fstream>
39 #include <string>
40 
41 // Note that the BESLog::operator<<() methods will prefix output with
42 // the time and PID by checking for the flush and endl stream operators.
43 //
44 // TRACE_LOGGING provides a way to see just where in the code the log info
45 // is written from. jhrg 11/14/17
46 
47 #undef TRACE_LOGGING
48 
49 // #define USE_LOG_TYPES WITH_NEWLOG
50 
51 #if 1 // NEW WAY is better way. ndp - 10/1/2020
52 #ifdef TRACE_LOGGING
53 #define MR_LOG(tag, msg) do { *(BESLog::TheLog()) << "trace-" << tag << BESLog::mark << __FILE__ << BESLog::mark << __LINE__ << BESLog::mark << msg ; BESLog::TheLog()->flush_me() ; } while( 0 )
54 #else
55 #if USE_LOG_CATEGORIES
56 #define MR_LOG(tag, msg) do { *(BESLog::TheLog()) << tag << BESLog::mark << msg ; BESLog::TheLog()->flush_me() ; } while( 0 )
57 #else
58 #define MR_LOG(tag, msg) do { *(BESLog::TheLog()) << msg ; BESLog::TheLog()->flush_me() ; } while( 0 )
59 #endif
60 #endif
61 
62 #define REQUEST_LOG(x) MR_LOG("request", x)
63 #define INFO_LOG(x) MR_LOG("info", x)
64 #define ERROR_LOG(x) MR_LOG("error", x)
65 #define VERBOSE(x) do { if (BESLog::TheLog()->is_verbose()) MR_LOG("verbose", x); } while( 0 )
66 
67 #endif
68 
69 
70 #if 0// OLD WAY
71 #ifdef TRACE_LOGGING
72 #define LOG(x) do { *(BESLog::TheLog()) << __FILE__ << ":" << __LINE__ << " - " << x ; BESLog::TheLog()->flush_me() ; } while( 0 )
73 #define VERBOSE(x) do { if (BESLog::TheLog()->is_verbose()) *(BESLog::TheLog()) << __FILE__ << ":" << __LINE__ << " - " << x ; BESLog::TheLog()->flush_me() ; } while( 0 )
74 #else
75 #define LOG(x) do { *(BESLog::TheLog()) << x ; BESLog::TheLog()->flush_me() ; } while( 0 )
76 #define VERBOSE(x) do { if (BESLog::TheLog()->is_verbose()) *(BESLog::TheLog()) << x ; BESLog::TheLog()->flush_me() ; } while( 0 )
77 #endif
78 
79 // Pretty silly - for now ERROR is the same as LOG, but I suspect that we might
80 // want to treat errors differently in the near future given the special logging
81 // needs of the 'Hyrax in the Cloud' project. jhrg 11/16/17
82 #define ERROR(x) LOG(x)
83 #endif
84 
85 
86 
87 #include "BESObj.h"
88 
130 class BESLog: public BESObj {
131 private:
132  static BESLog * d_instance;
133 
134  int d_flushed;
135  std::ofstream * d_file_buffer;
136  std::string d_file_name;
137 
138  // Flag to indicate the object is not routing data to its associated stream
139  int d_suspended;
140 
141  // Flag to indicate whether to log verbose messages
142  bool d_verbose;
143 
144  bool d_use_local_time; // Use UTC by default
145 
146  bool d_use_unix_time; // Use the UNIX time value as the log time.
147 
148 protected:
149  BESLog();
150 
151  // Dumps the current system time.
152  void dump_time();
153 public:
154  ~BESLog();
155 
156  const static std::string mark;
157 
163  void suspend()
164  {
165  d_suspended = 1;
166  }
167 
173  void resume()
174  {
175  d_suspended = 0;
176  }
177 
184  void verbose_on()
185  {
186  d_verbose = true;
187  }
188 
194  void verbose_off()
195  {
196  d_verbose = false;
197  }
198 
214  bool is_verbose()
215  {
216  return d_verbose;
217  }
218 
220  typedef std::ios& (*p_ios_manipulator)(std::ios&);
222  typedef std::ostream& (*p_ostream_manipulator)(std::ostream&);
223 
224  BESLog& operator <<(std::string&);
225  BESLog& operator <<(const std::string&);
226  BESLog& operator <<(char*);
227  BESLog& operator <<(const char*);
228  BESLog& operator <<(int);
229  BESLog& operator <<(unsigned int);
230  BESLog& operator <<(char);
231  BESLog& operator <<(long);
232  BESLog& operator <<(unsigned long);
233  BESLog& operator <<(double);
234 
237 
238  virtual void dump(std::ostream &strm) const;
239 
240  virtual void flush_me();
241 
242  static BESLog *TheLog();
243 
244  // I added this so that it's easy to route the BESDebug messages to the
245  // log file. This will enable the Admin Interface to display the contents
246  // of those debug messages when it displays the log file. jhrg
247  std::ostream *get_log_ostream()
248  {
249  return d_file_buffer;
250  }
251 };
252 
253 #endif // BESLog_h_
254 
Provides a mechanism for applications to log information to an external file.
Definition: BESLog.h:130
void dump_time()
Protected method that dumps the date/time to the log file.
Definition: BESLog.cc:167
BESLog & operator<<(std::string &)
Overloaded inserter that writes the specified string.
Definition: BESLog.cc:230
void resume()
Resumes logging after being suspended.
Definition: BESLog.h:173
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:220
void suspend()
Suspend logging of any information until resumed.
Definition: BESLog.h:163
void verbose_on()
turn on verbose logging
Definition: BESLog.h:184
~BESLog()
Cleans up the logging mechanism.
Definition: BESLog.cc:153
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:222
void verbose_off()
turns off verbose logging
Definition: BESLog.h:194
BESLog()
constructor that sets up logging for the application.
Definition: BESLog.cc:78
bool is_verbose()
Returns true if verbose logging is requested.
Definition: BESLog.h:214
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESLog.cc:401
top level BES object to house generic methods
Definition: BESObj.h:50