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
Trace.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 
33 #if !defined(LSST_PEX_LOGGING_TRACE_H)
34 #define LSST_PEX_LOGGING_TRACE_H 1
35 
36 #include <iostream>
37 #include <string>
38 #include <sstream>
39 #include <cstdarg>
40 #include <cstdio>
41 #include <boost/format.hpp>
42 
43 #include "lsst/pex/logging/Debug.h"
44 
45 namespace lsst {
46 namespace pex {
47 namespace logging {
48 
49 #ifndef LSST_DEBUGGING_ON
50 #define LSST_NO_TRACE 1
51 #endif
52 
53 #if !defined(LSST_NO_TRACE)
54 # define LSST_NO_TRACE 0
55 #endif
56 
57 #if !defined(LSST_MAX_TRACE)
58 # define LSST_MAX_TRACE -1
59 #endif
60 
93 class Trace {
94 public:
95 #if !LSST_NO_TRACE
96 
104  Trace(const std::string& name,
105  const int verbosity,
106  const std::string fmt,
107  ...
108  )
109  {
110  if (-1*verbosity >= Log::getDefaultLog().getThresholdFor(name)) {
111  va_list ap;
112  va_start(ap, fmt);
113  const int len = vsnprintf(NULL, 0, fmt.c_str(), ap) + 1; // "+ 1" for the '\0'
114  va_end(ap);
115 
116  char msg[len];
117  va_start(ap, fmt);
118  (void)vsnprintf(msg, len, fmt.c_str(), ap);
119  va_end(ap);
120 
121  Debug out(name);
122  out.debug(verbosity, msg);
123  }
124  }
125 
126  Trace(const std::string& name,
127  const int verbosity,
128  const char *msg
129  )
130  {
131  if (-1*verbosity >= Log::getDefaultLog().getThresholdFor(name)) {
132  Debug out(name);
133  out.debug(verbosity, msg);
134  }
135  }
136 
140  Trace(const std::string& name,
141  const int verbosity,
142  const boost::format& msg
143  )
144  {
145  if (-1*verbosity >= Log::getDefaultLog().getThresholdFor(name)) {
146  Debug out(name);
147  out.debug(verbosity, msg.str());
148  }
149  }
150 
151 #else
152 /*
153  Trace(const std::string& name, const int verbosity) {}
154 */
155  Trace(const std::string& name, const int verbosity,
156  const std::string& msg, ...) {}
157  Trace(const std::string& name, const int verbosity,
158  const boost::format& msg) {}
159 
160 #endif
161 
163  static void setDestination(std::ostream &) {
164  }
165 
166  static void setVerbosity(const std::string &name) {
167  if (name.length() == 0 || name == ".")
169  else
171  }
172  static void setVerbosity(const std::string &name, const int verbosity) {
173  if (name.length() == 0 || name == ".")
174  Log::getDefaultLog().setThreshold(-1*verbosity);
175  else
176  Log::getDefaultLog().setThresholdFor(name, -1*verbosity);
177  }
178  static int getVerbosity(const std::string &name) {
179  if (name.length() == 0 || name == ".")
180  return -1*Log::getDefaultLog().getThreshold();
181  else
182  return -1*Log::getDefaultLog().getThresholdFor(name);
183  }
184  static void printVerbosity(std::ostream& out) {
186  }
187  static void reset() {
189  }
190 };
191 
192 template<int VERBOSITY>
193 void TTrace(const char *name,
194  const char *fmt,
195  ...
196  ) {
197  if (LSST_MAX_TRACE < 0 || VERBOSITY <= LSST_MAX_TRACE) {
198  va_list ap;
199 
200  // first determine the length of the message
201  va_start(ap, fmt);
202  const int len = vsnprintf(NULL, 0, fmt, ap) + 1; // "+ 1" for the '\0'
203  va_end(ap);
204 
205  char msg[len];
206  va_start(ap, fmt);
207  (void)vsnprintf(msg, len, fmt, ap);
208  va_end(ap);
209 
210  Trace(name, VERBOSITY, msg);
211  }
212 }
213 
214 template<int VERBOSITY>
215 void TTrace(const std::string name,
216  const std::string fmt,
217  ...
218  ) {
219  if (LSST_MAX_TRACE < 0 || VERBOSITY <= LSST_MAX_TRACE) {
220  va_list ap;
221  va_start(ap, fmt);
222  Trace(name, VERBOSITY, fmt, ap);
223  va_end(ap);
224  }
225 }
226 
227 
228 } // namespace logging
229 } // namespace pex
230 } // namespace lsst
231 #endif // end LSST_PEX_LOGGING_TRACE_H
232 
static void setVerbosity(const std::string &name, const int verbosity)
Definition: Trace.h:172
table::Key< std::string > name
Definition: ApCorrMap.cc:71
int getThresholdFor(const std::string &name) const
int getThreshold() const
Definition: Log.h:281
#define LSST_MAX_TRACE
Maximum level to trace (only works for TTrace)
Definition: Trace.h:58
static void setVerbosity(const std::string &name)
Definition: Trace.h:166
limited backward compatibility to the DC2 run-time trace facilities
Definition: Trace.h:93
Trace(const std::string &name, const int verbosity, const std::string &msg,...)
Definition: Trace.h:155
static Log & getDefaultLog()
static void printVerbosity(std::ostream &out)
Definition: Trace.h:184
static const int INHERIT_THRESHOLD
Definition: Log.h:183
void printThresholds(std::ostream &out)
Definition: Log.h:628
Trace(const std::string &name, const int verbosity, const boost::format &msg)
Definition: Trace.h:157
static int getVerbosity(const std::string &name)
Definition: Trace.h:178
void setThresholdFor(const std::string &name, int threshold)
void debug(int verbosity, const std::string &message)
Definition: Debug.h:104
static void setDestination(std::ostream &)
set where logging is sent; ignored in new implementation
Definition: Trace.h:163
void TTrace(const char *name, const char *fmt,...)
Definition: Trace.h:193
static void reset()
Definition: Trace.h:187
void setThreshold(int threshold)
Definition: Log.h:293
definition of the Debug class and macros