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
LogRecord.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 
28 #include "lsst/pex/exceptions.h"
29 #include "lsst/daf/base/DateTime.h"
30 
31 #include <boost/shared_ptr.hpp>
32 #include <stdexcept>
33 #include <time.h>
34 
35 namespace lsst {
36 namespace pex {
37 namespace logging {
38 
39 //@cond
40 using boost::format;
41 using std::string;
44 namespace pexExcept = lsst::pex::exceptions;
45 
46 
47 /*
48  * Create a log record to be sent to a given log.
49  * @param importance the loudness of the record. If this value is
50  * greater than or equal to the Log's importance
51  * threshold, the message will be recorded.
52  */
53 LogRecord::LogRecord(int threshold, int importance, bool showAll)
54  : _send(threshold <= importance), _showAll(showAll), _vol(importance),
55  _data(new PropertySet())
56 {
57  _init();
58 }
59 
60 /*
61  * Create a log record to be sent to a given log. The current time is
62  * recorded and set as the DATE property.
63  * @param threshold the importance threshold that determines if a message
64  * is printed.
65  * @param importance the loudness of the record. If this value is
66  * greater than or equal to the given importance
67  * threshold, the message will be recorded.
68  * @param preamble an ordered set of properties that constitute the
69  * preamble of this message. This should not include
70  * the current time.
71  */
72 LogRecord::LogRecord(int threshold, int importance, const PropertySet& preamble,
73  bool showAll)
74  : _send(threshold <= importance), _showAll(showAll), _vol(importance),
75  _data()
76 {
77  if (_send) {
78  _data = preamble.deepCopy();
79  }
80  else {
81  _data = boost::shared_ptr<PropertySet>(new PropertySet());
82  }
83  _init();
84 }
85 
86 /*
87  * delete this log record
88  */
89 LogRecord::~LogRecord() { }
90 
91 long long LogRecord::utcnow() {
92  struct timeval tv;
93  struct timezone tz;
94  gettimeofday(&tv,&tz);
95  // _tv.tv_sec = seconds since the epoch
96  // _tv.tv_usec = microseconds since tv.tv_sec
97 
98  long long nsec = static_cast<long long>(tv.tv_sec) * 1000000000L;
99  nsec += tv.tv_usec * 1000L;
100  return nsec;
101 }
102 
103 void LogRecord::setTimestamp() {
104  _data->set(LSST_LP_TIMESTAMP,
105  lsst::daf::base::DateTime(utcnow(),
107 }
108 
109 void LogRecord::setDate() {
110  if (! _send) return;
111  if (! data().exists(LSST_LP_TIMESTAMP)) setTimestamp();
112 
113  char datestr[40];
114  struct timeval tv = _data->get<DateTime>(LSST_LP_TIMESTAMP).timeval();
115 
116  struct tm timeinfo;
117  time_t secs = (time_t) tv.tv_sec;
118  gmtime_r(&secs, &timeinfo);
119 
120  if ( 0 == strftime(datestr,39,"%Y-%m-%dT%H:%M:%S.", &timeinfo) ) {
121  throw LSST_EXCEPT(pexExcept::RuntimeError,
122  "Failed to format time successfully");
123  }
124 
125  string fulldate(str(format("%s%d") % string(datestr) % tv.tv_usec));
126  data().add(LSST_LP_DATE, fulldate);
127 }
128 
129 size_t LogRecord::countParamValues() const {
130  size_t sum = 0;
131  std::vector<std::string> names = _data->names(false);
132  std::vector<std::string>::iterator it;
133  for(it = names.begin(); it != names.end(); ++it) {
134  sum += _data->valueCount(*it);
135  }
136  return sum;
137 }
138 
139 void LogRecord::addProperties(const PropertySet& props) {
140  PropertySet::Ptr temp(props.deepCopy());
141  if (temp->exists("LEVEL")) temp->remove("LEVEL");
142  if (temp->exists("LOG")) temp->remove("LOG");
143  if (temp->exists("TIMESTAMP")) temp->remove("TIMESTAMP");
144  if (temp->exists("DATE")) temp->remove("DATE");
145  data().combine(temp);
146 }
147 
148 //@endcond
149 }}} // end lsst::pex::logging
150 
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:58
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
#define LSST_LP_TIMESTAMP
Definition: LogRecord.h:41
boost::enable_if< typename ExpressionTraits< Scalar >::IsScalar, Scalar >::type sum(Scalar const &scalar)
Definition: operators.h:1250
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:58
definition of the LogRecord, RecordProperty and Prop classes
#define LSST_LP_DATE
Definition: LogRecord.h:42
Interface for DateTime class.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Class for storing generic metadata.
Definition: PropertySet.h:82
virtual Ptr deepCopy(void) const
Definition: PropertySet.cc:70
def exists
Definition: cuda.py:53
Include files required for standard LSST Exception handling.