LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
LogFormatter.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
24 // LogFormatter.cc
25 //
26 // Contact: Ray Plante
27 //
29 
32 #include "lsst/pex/logging/Log.h"
34 #include "lsst/pex/exceptions.h"
36 
37 #include <memory>
38 #include <boost/any.hpp>
39 #include <string>
40 #include <sstream>
41 
42 using std::string;
43 
44 namespace lsst {
45 namespace pex {
46 namespace logging {
47 
48 namespace dafBase = lsst::daf::base;
49 namespace pexExcept = lsst::pex::exceptions;
50 
52 // LogFormatter
54 
56 
58 // BriefFormatter
60 
62 
63 /*
64  * write out a log record to a stream
65  * @param strm the output stream to write the record to
66  * @param rec the record to write
67  */
68 void BriefFormatter::write(std::ostream *strm, LogRecord const& rec) {
69  string log;
70  int level=0;
71  string levstr(": ");
72  std::vector<std::string> comments;
73 
74  try {
75  level = rec.data().get<int>(LSST_LP_LEVEL);
76  if (level >= Log::FATAL) levstr = " FATAL: ";
77  else if (level >= Log::WARN) levstr = " WARNING: ";
78  else if (level < Log::INFO) levstr = " DEBUG: ";
79  } catch (pexExcept::TypeError const & ex) {
80  } catch (pexExcept::NotFoundError const & ex) {}
81 
82  try {
83  log = rec.data().get<string>(LSST_LP_LOG);
84  } catch (pexExcept::TypeError const & ex) {
85  log = "mis-specified_log_name";
86  } catch (pexExcept::NotFoundError const & ex) {}
87 
88  try {
89  comments = rec.data().getArray<string>(LSST_LP_COMMENT);
90  } catch (pexExcept::TypeError const & ex) {
91  comments.push_back("(mis-specified_comment)");
92  } catch (pexExcept::NotFoundError const & ex) {}
93 
94  for (auto const& vi : comments) {
95  (*strm) << log << levstr << vi << std::endl;
96  }
97 
98  if (isVerbose() || rec.willShowAll()) {
99  std::vector<std::string> names = rec.data().paramNames(false);
100  for (auto const& vi : names) {
101  if (vi == LSST_LP_COMMENT || vi == LSST_LP_LOG) continue;
102 
103  PropertyPrinter pp(rec.data(), vi);
104  for (PropertyPrinter::iterator pi=pp.begin(); pi.notAtEnd(); ++pi) {
105  (*strm) << " " << vi << ": ";
106  pi.write(strm) << std::endl;
107  }
108  }
109  (*strm) << std::endl;
110  }
111 }
112 
114 // IndentedFormatter
116 
118 
119 /*
120  * write out a log record to a stream
121  * @param strm the output stream to write the record to
122  * @param rec the record to write
123  */
124 void IndentedFormatter::write(std::ostream *strm, LogRecord const& rec) {
125  string log;
126  int level=0;
127  string levstr(": ");
128  std::vector<std::string> comments;
129 
130  try {
131  level = rec.data().get<int>(LSST_LP_LEVEL);
132  if (level >= Log::FATAL) levstr = " FATAL: ";
133  else if (level >= Log::WARN) levstr = " WARNING: ";
134  else if (level < Log::INFO) levstr = " DEBUG: ";
135  } catch (pexExcept::TypeError const & ex) {
136  } catch (pexExcept::NotFoundError const & ex) {}
137 
138  try {
139  log = rec.data().get<string>(LSST_LP_LOG);
140  } catch (pexExcept::TypeError const & ex) {
141  log = "mis-specified_log_name";
142  } catch (pexExcept::NotFoundError const & ex) {}
143 
144  try {
145  comments = rec.data().getArray<string>(LSST_LP_COMMENT);
146  } catch (pexExcept::TypeError const & ex) {
147  comments.push_back("(mis-specified_comment)");
148  } catch (pexExcept::NotFoundError const & ex) {}
149 
150  std::ostringstream indentstr;
151  if (level < 0) {
152  // indent the message
153  for (int i=level; i < 0; ++i) {
154  indentstr << ' ';
155  }
156  }
157  string indent(indentstr.str());
158 
159  for (auto const& vi : comments) {
160  (*strm) << indent << log << levstr << vi << std::endl;
161  }
162 
163  if (isVerbose() || rec.willShowAll()) {
164  std::vector<std::string> names = rec.data().paramNames(false);
165  for (auto const& vi : names) {
166  if (vi == LSST_LP_COMMENT || vi == LSST_LP_LOG) continue;
167 
168  PropertyPrinter pp(rec.data(), vi);
169  for (PropertyPrinter::iterator pi=pp.begin(); pi.notAtEnd(); ++pi) {
170  (*strm) << indent << " " << vi << ": ";
171  pi.write(strm) << std::endl;
172  }
173  }
174  (*strm) << std::endl;
175  }
176 }
177 
179 // NetLoggerFormatter
181 //@cond
182 
183 const string NetLoggerFormatter::defaultValDelim(": ");
184 
185 NetLoggerFormatter::NetLoggerFormatter(string const& valueDelim)
186  : LogFormatter(), _tplookup(), _midfix(valueDelim)
187 {
188  loadTypeLookup();
189 }
190 
192 
193 #define LSST_TL_ADD(T, C) _tplookup[typeid(T).name()] = C
194 
196  LSST_TL_ADD(int, 'i');
197  LSST_TL_ADD(long, 'l');
198  LSST_TL_ADD(long long, 'L');
199  LSST_TL_ADD(char, 'c');
200  LSST_TL_ADD(std::string, 's');
201  LSST_TL_ADD(dafBase::DateTime, 'L');
202  LSST_TL_ADD(float, 'f');
203  LSST_TL_ADD(double, 'd');
204  LSST_TL_ADD(bool, 'b');
205 }
206 
210 NetLoggerFormatter& NetLoggerFormatter::operator=(NetLoggerFormatter const& that)
211 {
212  if (this == &that) return *this;
213 
214  dynamic_cast<NetLoggerFormatter*>(this)->operator=(that);
215  _midfix = that._midfix;
216  return *this;
217 }
218 
224 void NetLoggerFormatter::write(std::ostream *strm, LogRecord const& rec) {
225  string newl("\n");
226  bool wrote = false;
227  std::vector<std::string> comments;
228 
229  std::vector<std::string> names = rec.data().paramNames(false);
230  for (auto const& vi : names) {
231  char tp = _tplookup[rec.data().typeOf(vi).name()];
232  if (vi == "DATE")
233  tp = 't';
234  else if (tp == 0)
235  tp = '?';
236 
237 
238  PropertyPrinter pp(rec.data(), vi);
239  for (PropertyPrinter::iterator pi=pp.begin(); pi.notAtEnd(); ++pi) {
240  (*strm) << tp << " " << vi << _midfix;
241  pi.write(strm) << newl;
242  if (!wrote) wrote = true;
243  }
244  }
245 
246  if (wrote) (*strm) << std::endl;
247 }
248 
250 // PrependedFormatter
252 
254 
255 /*
256  * write out a log record to a stream
257  * @param strm the output stream to write the record to
258  * @param rec the record to write
259  */
260 void PrependedFormatter::write(std::ostream *strm, LogRecord const& rec) {
261  string date;
262  try {
263  date = rec.data().get<string>(LSST_LP_DATE) + ": ";
264  } catch (...) {
265  date = "(failed to get timestamp): ";
266  }
267 
268  int level = 0;
269  string levstr(": ");
270  try {
271  level = rec.data().get<int>(LSST_LP_LEVEL);
272  if (level >= Log::FATAL) levstr = " FATAL: ";
273  else if (level >= Log::WARN) levstr = " WARNING: ";
274  else if (level < Log::INFO) levstr = " DEBUG: ";
275  } catch (pexExcept::TypeError const & ex) {
276  } catch (pexExcept::NotFoundError const & ex) {}
277 
278  string log;
279  try {
280  log = rec.data().get<string>(LSST_LP_LOG);
281  } catch (pexExcept::TypeError const & ex) {
282  log = "mis-specified_log_name";
283  } catch (pexExcept::NotFoundError const & ex) {}
284 
285  std::vector<std::string> comments;
286  try {
287  comments = rec.data().getArray<string>(LSST_LP_COMMENT);
288  } catch (pexExcept::TypeError const & ex) {
289  comments.push_back("(mis-specified_comment)");
290  } catch (pexExcept::NotFoundError const & ex) {}
291 
292  string label;
293  try {
294  label = rec.data().get<string>(LSST_LP_LABEL);
295  } catch (pexExcept::TypeError const & ex) {
296  label = "mis-specified_label";
297  } catch (pexExcept::NotFoundError const & ex) {}
298 
299  for (auto const& vi : comments) {
300  (*strm) << date << label << ": " << log << levstr << vi << std::endl;
301  }
302 
303  if (isVerbose() || rec.willShowAll()) {
304  std::vector<std::string> names = rec.data().paramNames(false);
305  for (auto const& vi : names) {
306  if (vi == LSST_LP_COMMENT || vi == LSST_LP_LOG || vi == LSST_LP_LABEL) continue;
307 
308  PropertyPrinter pp(rec.data(), vi);
309  for (PropertyPrinter::iterator pi=pp.begin(); pi.notAtEnd(); ++pi) {
310  (*strm) << " " << vi << ": ";
311  pi.write(strm) << std::endl;
312  }
313  }
314  (*strm) << std::endl;
315  }
316 }
317 
318 //@endcond
319 }}} // end lsst::pex::logging
320 
static const int INFO
Definition: Log.h:171
std::vector< std::string > paramNames(bool topLevelOnly=true) const
Definition: PropertySet.cc:142
static const int WARN
Definition: Log.h:177
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:58
#define LSST_LP_LOG
Definition: LogRecord.h:43
virtual void write(std::ostream *strm, LogRecord const &rec)
#define LSST_LP_LABEL
Definition: LogRecord.h:44
NetLoggerFormatter(std::string const &valueDelim=defaultValDelim)
an class for printing the values associated with a name in a PropertySet.
a container for constructing a single Log record
Definition: LogRecord.h:100
definition of the LogRecord, RecordProperty and Prop classes
def log
Definition: log.py:83
static const std::string defaultValDelim
Definition: LogFormatter.h:231
virtual void write(std::ostream *strm, LogRecord const &rec)
Definition: LogFormatter.cc:68
#define LSST_LP_LEVEL
Definition: LogRecord.h:45
#define LSST_LP_COMMENT
Definition: LogRecord.h:40
#define LSST_LP_DATE
Definition: LogRecord.h:42
definition of the PropertyPrinter class and its helpers
T get(std::string const &name) const
Definition: PropertySet.cc:246
virtual void write(std::ostream *strm, LogRecord const &rec)
static const int FATAL
Definition: Log.h:191
NetLoggerFormatter & operator=(NetLoggerFormatter const &that)
definitions of the LogFormatter.h abstract class and its implementing classes, BriefFormatter, NetLoggerFormatter
virtual void write(std::ostream *strm, LogRecord const &rec)
const lsst::daf::base::PropertySet & data() const
Definition: LogRecord.h:222
Interface for PropertySet class.
Include files required for standard LSST Exception handling.
std::vector< T > getArray(std::string const &name) const
a wrapper PrinterIter class that hides the polymorphic (and possibly templatized) nature of an underl...
an abstract class for formatting log records into a text stream.
Definition: LogFormatter.h:74