LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
LogFormatter.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008, 2009, 2010 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 <boost/shared_ptr.hpp>
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 
59 
61 // BriefFormatter
63 
64 /*
65  * delete the formatter.
66  */
68 
69 /*
70  * write out a log record to a stream
71  * @param strm the output stream to write the record to
72  * @param rec the record to write
73  */
74 void BriefFormatter::write(std::ostream *strm, const LogRecord& rec) {
75  string log;
76  int level=0;
77  string levstr(": ");
78  std::vector<std::string> comments;
79  std::vector<std::string>::iterator vi;
80 
81  try {
82  level = rec.data().get<int>(LSST_LP_LEVEL);
83  if (level >= Log::FATAL) levstr = " FATAL: ";
84  else if (level >= Log::WARN) levstr = " WARNING: ";
85  else if (level < Log::INFO) levstr = " DEBUG: ";
86  } catch (pexExcept::TypeError ex) {
87  } catch (pexExcept::NotFoundError ex) { }
88 
89  try {
90  log = rec.data().get<string>(LSST_LP_LOG);
91  } catch (pexExcept::TypeError ex) {
92  log = "mis-specified_log_name";
93  } catch (pexExcept::NotFoundError ex) { }
94 
95  try {
96  comments = rec.data().getArray<string>(LSST_LP_COMMENT);
97  } catch (pexExcept::TypeError ex) {
98  comments.push_back("(mis-specified_comment)");
99  } catch (pexExcept::NotFoundError ex) { }
100 
101  for(vi = comments.begin(); vi != comments.end(); ++vi) {
102  (*strm) << log << levstr << *vi << std::endl;
103  }
104 
105  if (isVerbose() || rec.willShowAll()) {
106  std::vector<std::string> names = rec.data().paramNames(false);
107  for(vi = names.begin(); vi != names.end(); ++vi) {
108  if (*vi == LSST_LP_COMMENT || *vi == LSST_LP_LOG)
109  continue;
110 
111  PropertyPrinter pp(rec.data(), *vi);
112  for(PropertyPrinter::iterator pi=pp.begin(); pi.notAtEnd(); ++pi) {
113  (*strm) << " " << *vi << ": ";
114  pi.write(strm) << std::endl;
115  }
116  }
117  (*strm) << std::endl;
118  }
119 }
120 
122 // IndentedFormatter
124 
125 /*
126  * delete the formatter.
127  */
129 
130 /*
131  * write out a log record to a stream
132  * @param strm the output stream to write the record to
133  * @param rec the record to write
134  */
135 void IndentedFormatter::write(std::ostream *strm, const LogRecord& rec) {
136  string log;
137  int level=0;
138  string levstr(": ");
139  std::vector<std::string> comments;
140  std::vector<std::string>::iterator vi;
141 
142  try {
143  level = rec.data().get<int>(LSST_LP_LEVEL);
144  if (level >= Log::FATAL) levstr = " FATAL: ";
145  else if (level >= Log::WARN) levstr = " WARNING: ";
146  else if (level < Log::INFO) levstr = " DEBUG: ";
147  } catch (pexExcept::TypeError ex) {
148  } catch (pexExcept::NotFoundError ex) { }
149 
150  try {
151  log = rec.data().get<string>(LSST_LP_LOG);
152  } catch (pexExcept::TypeError ex) {
153  log = "mis-specified_log_name";
154  } catch (pexExcept::NotFoundError ex) { }
155 
156  try {
157  comments = rec.data().getArray<string>(LSST_LP_COMMENT);
158  } catch (pexExcept::TypeError ex) {
159  comments.push_back("(mis-specified_comment)");
160  } catch (pexExcept::NotFoundError ex) { }
161 
162  std::ostringstream indentstr;
163  if (level < 0) {
164  // indent the message
165  for(int i=level; i < 0; ++i) {
166  indentstr << ' ';
167  }
168  }
169  string indent(indentstr.str());
170 
171  for(vi = comments.begin(); vi != comments.end(); ++vi) {
172  (*strm) << indent << log << levstr << *vi << std::endl;
173  }
174 
175  if (isVerbose() || rec.willShowAll()) {
176  std::vector<std::string> names = rec.data().paramNames(false);
177  for(vi = names.begin(); vi != names.end(); ++vi) {
178  if (*vi == LSST_LP_COMMENT || *vi == LSST_LP_LOG)
179  continue;
180 
181  PropertyPrinter pp(rec.data(), *vi);
182  for(PropertyPrinter::iterator pi=pp.begin(); pi.notAtEnd(); ++pi) {
183  (*strm) << indent << " " << *vi << ": ";
184  pi.write(strm) << std::endl;
185  }
186  }
187  (*strm) << std::endl;
188  }
189 }
190 
192 // NetLoggerFormatter
194 //@cond
195 
196 const string NetLoggerFormatter::defaultValDelim(": ");
197 
198 NetLoggerFormatter::NetLoggerFormatter(const string& valueDelim)
199  : LogFormatter(), _tplookup(), _midfix(valueDelim)
200 {
201  loadTypeLookup();
202 }
203 
204 NetLoggerFormatter::~NetLoggerFormatter() { }
205 
206 #define LSST_TL_ADD(T, C) _tplookup[typeid(T).name()] = C
207 
208 void NetLoggerFormatter::loadTypeLookup() {
209  LSST_TL_ADD(int, 'i');
210  LSST_TL_ADD(long, 'l');
211  LSST_TL_ADD(long long, 'L');
212  LSST_TL_ADD(char, 'c');
213  LSST_TL_ADD(std::string, 's');
214  LSST_TL_ADD(dafBase::DateTime, 'L');
215  LSST_TL_ADD(float, 'f');
216  LSST_TL_ADD(double, 'd');
217  LSST_TL_ADD(bool, 'b');
218 }
219 
223 NetLoggerFormatter& NetLoggerFormatter::operator=(const NetLoggerFormatter& that)
224 {
225  if (this == &that) return *this;
226 
227  dynamic_cast<NetLoggerFormatter*>(this)->operator=(that);
228  _midfix = that._midfix;
229  return *this;
230 }
231 
237 void NetLoggerFormatter::write(std::ostream *strm, const LogRecord& rec) {
238  string newl("\n");
239  bool wrote = false;
240  std::vector<std::string> comments;
241  std::vector<std::string>::iterator vi;
242 
243  std::vector<std::string> names = rec.data().paramNames(false);
244  for(vi = names.begin(); vi != names.end(); ++vi) {
245  char tp = _tplookup[rec.data().typeOf(*vi).name()];
246  if (*vi == "DATE")
247  tp = 't';
248  else if (tp == 0)
249  tp = '?';
250 
251 
252  PropertyPrinter pp(rec.data(), *vi);
253  for(PropertyPrinter::iterator pi=pp.begin(); pi.notAtEnd(); ++pi) {
254  (*strm) << tp << " " << *vi << _midfix;
255  pi.write(strm) << newl;
256  if (!wrote) wrote = true;
257  }
258  }
259 
260  if (wrote) (*strm) << std::endl;
261 }
262 
263 //@endcond
264 }}} // end lsst::pex::logging
265 
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:58
#define LSST_LP_LOG
Definition: LogRecord.h:43
NetLoggerFormatter(const std::string &valueDelim=defaultValDelim)
static const int WARN
Definition: Log.h:177
definition of the LogRecord, RecordProperty and Prop classes
def log
Definition: log.py:85
static const int INFO
Definition: Log.h:171
#define LSST_LP_LEVEL
Definition: LogRecord.h:44
virtual void write(std::ostream *strm, const LogRecord &rec)
#define LSST_LP_COMMENT
Definition: LogRecord.h:40
static const int FATAL
Definition: Log.h:191
definition of the PropertyPrinter class and its helpers
virtual void write(std::ostream *strm, const LogRecord &rec)
definitions of the LogFormatter.h abstract class and its implementing classes, BriefFormatter, NetLoggerFormatter
Interface for PropertySet class.
static const std::string defaultValDelim
Definition: LogFormatter.h:271
Include files required for standard LSST Exception handling.