LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Log.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
30 #ifndef LSST_PEX_LOG_H
31 #define LSST_PEX_LOG_H
32 
37 
38 #include <vector>
39 #include <list>
40 #include <cstdarg>
41 #include <boost/shared_ptr.hpp>
42 
43 // If the compiler does not support attributes, disable them
44 #ifndef __GNUC__
45 # define __attribute__(x)
46 #endif
47 // Tell the compiler that a function takes a printf-style format string
48 // and varargs; it can then warn if the args don't match the format string.
49 // The "fmt" and "start" args are the positions of the format string and
50 // the start of the varargs; +1 for C++ member functions.
51 #define ATTRIB_FORMAT(fmt,start) __attribute__ ((format(printf,fmt,start)))
52 
53 namespace lsst {
54 namespace pex {
55 namespace logging {
56 
154 class Log {
155 public:
156 
163  static const int DEBUG;
164 
171  static const int INFO;
172 
177  static const int WARN;
178 
183  static const int INHERIT_THRESHOLD;
184 
191  static const int FATAL;
192 
202  Log(const int threshold=INFO, const std::string& name="");
203 
229  Log(const std::list<boost::shared_ptr<LogDestination> >& destinations,
230  const lsst::daf::base::PropertySet& preamble,
231  const std::string& name="", const int threshold=INFO,
232  bool defaultShowAll=false);
233 
252  Log(const Log& parent, const std::string& childName,
253  int threshold=INHERIT_THRESHOLD);
254 
258  Log(const Log& that);
259 
263  virtual ~Log();
264 
268  Log& operator=(const Log& that);
269 
274  const std::string& getName() const { return _name; }
275 
281  int getThreshold() const {
282  return ((_threshold > INHERIT_THRESHOLD || _name.length() == 0)
283  ? _threshold
284  : _thresholds->getThresholdFor(_name) );
285  }
286 
293  void setThreshold(int threshold) {
294  _threshold = threshold;
295  _thresholds->setThresholdFor(_name, threshold);
296  }
297 
302  bool sends(int importance) const { return (importance >= getThreshold()); }
303 
309 
319  void setThresholdFor(const std::string& name, int threshold);
320 
326  int getThresholdFor(const std::string& name) const;
327 
335  bool willShowAll() const {
336  return
337  (_myShowAll.get()) ? *_myShowAll
338  : ((_defShowAll.get()) ? *_defShowAll : false);
339  }
340 
356  void setShowAll(bool yesno) {
357  if (_myShowAll.get())
358  *_myShowAll = yesno;
359  else
360  _myShowAll.reset(new bool(yesno));
361  }
362 
367  void resetShowAll() {
368  if (_defShowAll.get() == _myShowAll.get())
369  *_myShowAll = false;
370  else
371  _myShowAll.reset();
372  }
373 
377  template <class T>
378  void addPreambleProperty(const std::string& name, const T& val);
379 
384  template <class T>
385  void setPreambleProperty(const std::string& name, const T& val);
386 
404  Log *createChildLog(const std::string& childName,
405  int threshold=INHERIT_THRESHOLD) const;
406 
413  void log(int importance, const std::string& message,
414  const lsst::daf::base::PropertySet& properties);
415 
423  template <class T>
424  void log(int importance, const std::string& message,
425  const std::string& name, const T& val);
426 
433  template <class T>
434  void log(int importance, const std::string& message,
435  const RecordProperty<T>& prop);
436 
442  void log(int importance, const std::string& message);
443 
450  void log(int importance, const boost::format& message) {
451  log(importance, message.str());
452  }
453 
471 #define LEVELF(fname, lev) \
472  void fname(const std::string& message, \
473  const lsst::daf::base::PropertySet& properties) { \
474  log(lev, message, properties); \
475  } \
476  template <class T> \
477  void fname(const std::string& message, \
478  const std::string& name, const T& val) { \
479  log<T>(lev, message, name, val); \
480  } \
481  template <class T> \
482  void fname(const std::string& message, \
483  const RecordProperty<T>& prop) { \
484  log<T>(lev, message, prop); \
485  } \
486  void fname(const std::string& message) { \
487  log(lev, message); \
488  } \
489  void fname(const boost::format& message) { \
490  log(lev, message); \
491  }
496 #undef LEVELF
497 
506  void format(int importance, const char *fmt, ...)
507  ATTRIB_FORMAT(3, 4);
508 
517 #define LEVELF(fname, lev) \
518  void fname(const char* fmt, ...) \
519  ATTRIB_FORMAT(2, 3) { \
520  if (lev < getThreshold()) return; \
521  va_list ap; \
522  va_start(ap, fmt); \
523  _format(lev, fmt, ap); \
524  va_end(ap); \
525  }
526 
531 
532 #undef LEVELF
533 
537  void send(const LogRecord& record);
538 
549  void addDestination(std::ostream& destination, int threshold);
550 
564  void addDestination(std::ostream &destination, int threshold,
565  const boost::shared_ptr<LogFormatter> &formatter);
566 
573  void addDestination(const boost::shared_ptr<LogDestination> &destination) {
574  _destinations.push_back(destination);
575  }
576 
581 
589  void markPersistent() { _preamble->markPersistent(); }
590 
594  static Log& getDefaultLog();
595 
615  static void createDefaultLog(
616  const std::list<boost::shared_ptr<LogDestination> >& destinations,
617  const lsst::daf::base::PropertySet& preamble,
618  const std::string& name="", const int threshold=INFO);
619 
623  static void closeDefaultLog();
624 
628  void printThresholds(std::ostream& out) {
629  _thresholds->printThresholds(out);
630  }
631 
637  void reset() { _thresholds->forgetAllNames(); }
638 
639 protected:
643  static Log *defaultLog;
644 
651  static void setDefaultLog(Log* deflog);
652 
653  static const std::string _sep;
654 
659  void _send(int threshold, int importance, const char *fmt, va_list ap);
660 
665  void _format(int importance, const char* fmt, va_list ap);
666 
667 private:
668  void completePreamble();
669 
671  boost::shared_ptr<bool> _defShowAll;
672  boost::shared_ptr<bool> _myShowAll;
673  std::string _name;
674 
675 protected:
679  boost::shared_ptr<threshold::Memory> _thresholds;
680 
684  std::list<boost::shared_ptr<LogDestination> > _destinations;
685 
691 };
692 
693 template <class T>
694 void Log::addPreambleProperty(const std::string& name, const T& val) {
695  _preamble->add<T>(name, val);
696 }
697 
698 template <class T>
699 void Log::setPreambleProperty(const std::string& name, const T& val) {
700  _preamble->set<T>(name, val);
701 }
702 
703 template <class T>
704 void Log::log(int importance, const std::string& message,
705  const std::string& name, const T& val) {
706 
707  int threshold = getThreshold();
708  if (importance < threshold)
709  return;
710  LogRecord rec(threshold, importance, *_preamble, willShowAll());
711  rec.addComment(message);
712  rec.addProperty(name, val);
713  send(rec);
714 }
715 
716 template <class T>
717 void Log::log(int importance, const std::string& message,
718  const RecordProperty<T>& prop)
719 {
720  log(importance, message, prop.name, prop.value);
721 }
722 
723 
729 class LogRec : public LogRecord {
730 public:
731 
739  LogRec(Log& log, int importance)
740  : LogRecord(log.getThreshold(), importance, log.getPreamble()),
741  _sent(false), _log(&log)
742  { }
743 
747  LogRec(const LogRec& that)
748  : LogRecord(that), _sent(false), _log(that._log)
749  { }
750 
754  virtual ~LogRec();
755 
759  LogRec& operator=(const LogRec& that) {
760  LogRecord::operator=(that);
761  _log = that._log;
762  return *this;
763  }
764 
769  enum Manip {
775  };
776 
780  LogRec& operator<<(const std::string& comment); /* {
781  addComment(comment);
782  return *this;
783  } */
784 
788  LogRec& operator<<(const boost::format& comment) {
789  addComment(comment);
790  return *this;
791  }
792 
796  template <class T>
797  LogRec& operator<<(const RecordProperty<T>& prop) {
798  addProperty(prop.name, prop.value);
799  return *this;
800  }
801 
806  addProperties(props);
807  return *this;
808  }
809 
817  if (_send) {
818  switch (signal) {
819  case endr:
820  _log->send(*this);
821  _sent = true;
822  break;
823  default:
824  ;
825  }
826  }
827  return *this;
828  }
829 
830 private:
831  bool _sent;
833 };
834 
838 typedef LogRec Rec;
839 
840 }}} // end lsst::pex::logging
841 
842 #undef ATTRIB_FORMAT
843 
844 #endif // end LSST_PEX_LOG_H
LogRec(const LogRec &that)
Definition: Log.h:747
LogRec & operator<<(const std::string &comment)
static void setDefaultLog(Log *deflog)
table::Key< std::string > name
Definition: ApCorrMap.cc:71
int getThresholdFor(const std::string &name) const
LogRec & operator<<(const boost::format &comment)
Definition: Log.h:788
void addComment(const std::string &comment)
Definition: LogRecord.h:165
int getThreshold() const
Definition: Log.h:281
LogRec Rec
Definition: Log.h:838
void _format(int importance, const char *fmt, va_list ap)
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
#define LEVELF(fname, lev)
Definition: Log.h:517
void format(int importance, const char *fmt,...)
void fatalf(const char *fmt,...)
Definition: Log.h:530
static const int WARN
Definition: Log.h:177
void infof(const char *fmt,...)
Definition: Log.h:528
void setShowAll(bool yesno)
Definition: Log.h:356
static const std::string _sep
Definition: Log.h:653
Log & operator=(const Log &that)
void log(int importance, const std::string &message, const lsst::daf::base::PropertySet &properties)
void send(const LogRecord &record)
lsst::daf::base::PropertySet::Ptr _preamble
Definition: Log.h:690
a place to record messages and descriptions of the state of processing.
Definition: Log.h:154
static Log & getDefaultLog()
A LogRecord attached to a particular Log that suppports stream stream semantics.
Definition: Log.h:729
void setPreambleProperty(const std::string &name, const T &val)
Definition: Log.h:699
boost::shared_ptr< bool > _myShowAll
Definition: Log.h:672
bool val
static void closeDefaultLog()
a container for constructing a single Log record
Definition: LogRecord.h:99
definition of the LogRecord, RecordProperty and Prop classes
static const int INHERIT_THRESHOLD
Definition: Log.h:183
def log
Definition: log.py:85
void logdebug(const std::string &message, const lsst::daf::base::PropertySet &properties)
Definition: Log.h:492
void _send(int threshold, int importance, const char *fmt, va_list ap)
void printThresholds(std::ostream &out)
Definition: Log.h:628
bool willShowAll() const
Definition: Log.h:335
const std::string & getName() const
Definition: Log.h:274
void addDestination(std::ostream &destination, int threshold)
static const int INFO
Definition: Log.h:171
definition of the LogDestination class
LogRec(Log &log, int importance)
Definition: Log.h:739
void addProperty(const RecordProperty< T > &property)
Definition: LogRecord.h:327
void debugf(const char *fmt,...)
Definition: Log.h:527
LogRec & operator<<(const lsst::daf::base::PropertySet &props)
Definition: Log.h:805
static const int FATAL
Definition: Log.h:191
void fatal(const std::string &message, const lsst::daf::base::PropertySet &properties)
Definition: Log.h:495
static void createDefaultLog(const std::list< boost::shared_ptr< LogDestination > > &destinations, const lsst::daf::base::PropertySet &preamble, const std::string &name="", const int threshold=INFO)
bool sends(int importance) const
Definition: Log.h:302
void resetThreshold()
Definition: Log.h:308
LogRec & operator<<(Manip signal)
Definition: Log.h:816
Log * createChildLog(const std::string &childName, int threshold=INHERIT_THRESHOLD) const
definition of the Memory class
void log(int importance, const boost::format &message)
Definition: Log.h:450
void setThresholdFor(const std::string &name, int threshold)
boost::shared_ptr< bool > _defShowAll
Definition: Log.h:671
Class for storing generic metadata.
Definition: PropertySet.h:82
void warn(const std::string &message, const lsst::daf::base::PropertySet &properties)
Definition: Log.h:494
std::list< boost::shared_ptr< LogDestination > > _destinations
Definition: Log.h:684
const lsst::daf::base::PropertySet & getPreamble()
Definition: Log.h:580
Interface for PropertySet class.
void warnf(const char *fmt,...)
Definition: Log.h:529
static const int DEBUG
Definition: Log.h:163
std::string _name
Definition: Log.h:673
void resetShowAll()
Definition: Log.h:367
void markPersistent()
Definition: Log.h:589
#define ATTRIB_FORMAT(fmt, start)
Definition: Log.h:51
a container for a named data property for a LogRecord
Definition: LogRecord.h:62
void addProperties(const lsst::daf::base::PropertySet &props)
void addPreambleProperty(const std::string &name, const T &val)
Definition: Log.h:694
Log(const int threshold=INFO, const std::string &name="")
LogRec & operator=(const LogRec &that)
Definition: Log.h:759
boost::shared_ptr< threshold::Memory > _thresholds
Definition: Log.h:679
void info(const std::string &message, const lsst::daf::base::PropertySet &properties)
Definition: Log.h:493
void setThreshold(int threshold)
Definition: Log.h:293
LogRecord & operator=(const LogRecord &that)
Definition: LogRecord.h:151
static Log * defaultLog
Definition: Log.h:643
void addDestination(const boost::shared_ptr< LogDestination > &destination)
Definition: Log.h:573