bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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#include "BESObj.h"
42
43// Note that the BESLog::operator<<() methods will prefix output with
44// the time and PID by checking for the flush and endl stream operators.
45//
46// TRACE_LOGGING provides a way to see just where in the code the log info
47// is written from. jhrg 11/14/17
48
49#undef TRACE_LOGGING
50
51#ifdef TRACE_LOGGING
52#define REQUEST_LOG(x) do { BESLog::TheLog()->trace_request(x, __FILE__ ,__LINE__); } while(0)
53#define INFO_LOG(x) do { BESLog::TheLog()->trace_info(x, __FILE__ ,__LINE__); } while(0)
54#define ERROR_LOG(x) do { BESLog::TheLog()->trace_error(x, __FILE__ ,__LINE__); } while(0)
55#define VERBOSE(x) do {if (BESLog::TheLog()->is_verbose()) BESLog::TheLog()->trace_verbose(x, __FILE__, __LINE__; } while(0)
56#define TIMING_LOG(x) do { BESLog::TheLog()->trace_timing(x, __FILE__ ,__LINE__); } while(0)
57#else
58#define REQUEST_LOG(x) do { BESLog::TheLog()->request(x); } while(0)
59#define INFO_LOG(x) do { BESLog::TheLog()->info(x); } while(0)
60#define ERROR_LOG(x) do { BESLog::TheLog()->error(x); } while(0)
61#define VERBOSE(x) do {if (BESLog::TheLog()->is_verbose()) BESLog::TheLog()->verbose(x); } while(0)
62#define TIMING_LOG(x) do { BESLog::TheLog()->timing(x); } while(0)
63#endif
64
65
114class BESLog: public BESObj {
115private:
116 static BESLog * d_instance;
117
118 std::ofstream *d_file_buffer = nullptr;
119 std::string d_file_name;
120 std::string d_instance_id = "-";
121 std::string d_pid = "-";
122 std::string request_id{"BESLog-NotYetSet"};
123 std::string d_log_record_prolog_base;
124
125 // Flag to indicate whether to log verbose messages
126 bool d_verbose = false;
127
128 // Use UTC by default
129 bool d_use_local_time = false;
130
131 // Use the UNIX time value as the log time.
132 bool d_use_unix_time = false;
133
134 const char* REQUEST_LOG_TYPE_KEY = "request";
135 const char* INFO_LOG_TYPE_KEY = "info";
136 const char* ERROR_LOG_TYPE_KEY = "error";
137 const char* VERBOSE_LOG_TYPE_KEY = "verbose";
138 const char* TIMING_LOG_TYPE_KEY = "timing";
139
140protected:
141 BESLog();
142
143 // Starts a log record with time and PID.
144 std::string log_record_begin() const;
145
146 void log_record(const std::string &record_type, const std::string &msg) const;
147 void trace_log_record(const std::string &record_type, const std::string &msg, const std::string &file, int line) const;
148
149public:
150 ~BESLog() override;
151
152 const static std::string mark;
153
160 void verbose_on(){ d_verbose = true; }
161
167 void verbose_off() { d_verbose = false; }
168
184 bool is_verbose() const { return d_verbose; }
185
186 pid_t update_pid();
187
191 void request(const std::string &msg) const {
192 log_record(REQUEST_LOG_TYPE_KEY, msg);
193 }
194
198 void info(const std::string &msg) const {
199 log_record(INFO_LOG_TYPE_KEY, msg);
200 }
201
205 void error(const std::string &msg) const {
206 log_record(ERROR_LOG_TYPE_KEY, msg);
207 }
208
212 void verbose(const std::string &msg) const {
213 if(d_verbose) {
214 log_record(VERBOSE_LOG_TYPE_KEY, msg);
215 }
216 }
217
221 void timing(const std::string &msg) const {
222 log_record(TIMING_LOG_TYPE_KEY, msg);
223 }
224
228 void trace_request(const std::string &msg, const std::string &file, int line) const {
229 trace_log_record(REQUEST_LOG_TYPE_KEY, msg, file, line);
230 }
231
235 void trace_info(const std::string &msg, const std::string &file, int line) const {
236 trace_log_record(INFO_LOG_TYPE_KEY, msg, file, line);
237 }
238
242 void trace_error(const std::string &msg, const std::string &file, int line) const {
243 trace_log_record(ERROR_LOG_TYPE_KEY, msg, file, line);
244 }
245
249 void trace_verbose(const std::string &msg, const std::string &file, int line) const {
250 if(d_verbose) {
251 trace_log_record(VERBOSE_LOG_TYPE_KEY, msg, file, line);
252 }
253 }
254
258 void trace_timing(const std::string &msg, const std::string &file, int line) const {
259 trace_log_record(TIMING_LOG_TYPE_KEY, msg, file, line);
260 }
261
262
263 void set_request_id(const std::string &id);
264 std::string get_request_id() const {return request_id;}
265
266 void dump(std::ostream &strm) const override;
267
268 static BESLog *TheLog();
269
270 // I added this so that it's easy to route the BESDebug messages to the
271 // log file. This will enable the Admin Interface to display the contents
272 // of those debug messages when it displays the log file. jhrg
273 std::ostream *get_log_ostream() const {
274 return d_file_buffer;
275 }
276};
277
278#endif // BESLog_h_
279
bool is_verbose() const
Returns true if verbose logging is requested.
Definition BESLog.h:184
void set_request_id(const std::string &id)
Sets the current request id (cached in BESLog) to id.
Definition BESLog.cc:282
pid_t update_pid()
Update the d_pid and the d_log_record_prolog_base values.
Definition BESLog.cc:182
void log_record(const std::string &record_type, const std::string &msg) const
Writes msg to a log record with type lrt.
Definition BESLog.cc:231
void trace_info(const std::string &msg, const std::string &file, int line) const
Writes info msg to the log stream with FILE and LINE.
Definition BESLog.h:235
void trace_verbose(const std::string &msg, const std::string &file, int line) const
Writes verbose msg to the log stream with FILE and LINE, if verbose logging is enabled.
Definition BESLog.h:249
void trace_log_record(const std::string &record_type, const std::string &msg, const std::string &file, int line) const
Writes msg, file, and line to a trace log record with type lrt.
Definition BESLog.cc:245
void verbose_on()
turn on verbose logging
Definition BESLog.h:160
void timing(const std::string &msg) const
Writes timing msg to the log stream.
Definition BESLog.h:221
std::string log_record_begin() const
Protected method that returns a string with the first fields of a log record.
Definition BESLog.cc:200
void trace_request(const std::string &msg, const std::string &file, int line) const
Writes request msg to the log stream with FILE and LINE.
Definition BESLog.h:228
void error(const std::string &msg) const
Writes error msg to the log stream.
Definition BESLog.h:205
void trace_timing(const std::string &msg, const std::string &file, int line) const
Writes timing msg to the log stream with FILE and LINE.
Definition BESLog.h:258
void trace_error(const std::string &msg, const std::string &file, int line) const
Writes error msg to the log stream with FILE and LINE.
Definition BESLog.h:242
void dump(std::ostream &strm) const override
dumps information about this object
Definition BESLog.cc:262
void verbose_off()
turns off verbose logging
Definition BESLog.h:167
void verbose(const std::string &msg) const
Writes verbose msg to the log stream, if verbose logging is enabled.
Definition BESLog.h:212
void request(const std::string &msg) const
Writes request msg to the log stream.
Definition BESLog.h:191
~BESLog() override
Cleans up the logging mechanism.
Definition BESLog.cc:172
BESLog()
constructor that sets up logging for the application.
Definition BESLog.cc:77
void info(const std::string &msg) const
Writes info msg to the log stream.
Definition BESLog.h:198
top level BES object to house generic methods
Definition BESObj.h:54