Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_VerboseObject.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_VERBOSE_OBJECT_HPP
43 #define TEUCHOS_VERBOSE_OBJECT_HPP
44 
45 #include "Teuchos_RCP.hpp"
46 #include "Teuchos_FancyOStream.hpp"
48 
49 
50 namespace Teuchos {
51 
52 
66 class TEUCHOSCORE_LIB_DLL_EXPORT VerboseObjectBase {
67 public:
68 
70 
71 
77  static void setDefaultOStream( const RCP<FancyOStream> &defaultOStream );
78 
80  static RCP<FancyOStream> getDefaultOStream();
81 
83 
85 
86 
88  virtual ~VerboseObjectBase();
89 
92  explicit
94  const RCP<FancyOStream> &oStream = Teuchos::null
95  );
96 
99  virtual void initializeVerboseObjectBase(
100  const RCP<FancyOStream> &oStream = Teuchos::null
101  );
102 
108  virtual const VerboseObjectBase& setOStream(
109  const RCP<FancyOStream> &oStream) const;
110 
117  virtual const VerboseObjectBase& setOverridingOStream(
118  const RCP<FancyOStream> &oStream) const;
119 
121  virtual VerboseObjectBase& setLinePrefix(const std::string &linePrefix);
122 
124 
126 
127 
131  virtual RCP<FancyOStream> getOStream() const;
132 
139  virtual RCP<FancyOStream> getOverridingOStream() const;
140 
142  virtual std::string getLinePrefix() const;
143 
145 
147 
148 
162  virtual OSTab getOSTab(const int tabs = 1, const std::string &linePrefix = "") const;
163 
165 
166 protected:
167 
176  virtual void informUpdatedVerbosityState() const;
177 
178 private:
179 
180  std::string thisLinePrefix_;
181 
182 //use pragmas to disable some false-positive warnings for windows sharedlibs export
183 #ifdef _MSC_VER
184 #pragma warning(push)
185 #pragma warning(disable:4251)
186 #endif
187  mutable RCP<FancyOStream> thisOStream_;
188  mutable RCP<FancyOStream> thisOverridingOStream_;
189 #ifdef _MSC_VER
190 #pragma warning(pop)
191 #endif
192 
193  static RCP<FancyOStream>& privateDefaultOStream();
194 
195 };
196 
197 
233 template<class ObjectType>
234 class VerboseObject : virtual public VerboseObjectBase {
235 public:
237 
238 
243  static void setDefaultVerbLevel( const EVerbosityLevel defaultVerbLevel);
244 
246  static EVerbosityLevel getDefaultVerbLevel();
247 
249 
251 
253  explicit
255  const EVerbosityLevel verbLevel = VERB_DEFAULT, // Note, this must be the same as the default value for defaultVerbLevel_
256  const RCP<FancyOStream> &oStream = Teuchos::null
257  );
258 
266  virtual void initializeVerboseObject(
267  const EVerbosityLevel verbLevel = VERB_DEFAULT,
268  const RCP<FancyOStream> &oStream = Teuchos::null
269  );
270 
276  virtual const VerboseObject& setVerbLevel(
277  const EVerbosityLevel verbLevel) const;
278 
285  virtual const VerboseObject& setOverridingVerbLevel(
286  const EVerbosityLevel verbLevel) const;
287 
289 
291 
293  virtual EVerbosityLevel getVerbLevel() const;
294 
296 
297 private:
298 
299  mutable EVerbosityLevel thisVerbLevel_;
300  mutable EVerbosityLevel thisOverridingVerbLevel_;
301 
302  static EVerbosityLevel& privateDefaultVerbLevel();
303 
304 };
305 
306 
310 template<class ObjectType>
312 public:
315  const RCP<const VerboseObject<ObjectType> > &verboseObject,
316  const RCP<FancyOStream> &newOStream,
317  const EVerbosityLevel newVerbLevel
318  ):
319  verboseObject_(verboseObject),
320  oldVerbLevel_(VERB_DEFAULT)
321  {
322  if(verboseObject_.get()) {
323  oldOStream_ = verboseObject_->getOStream();
324  oldVerbLevel_ = verboseObject_->getVerbLevel();
325  verboseObject_->setOStream(newOStream);
326  verboseObject_->setVerbLevel(newVerbLevel);
327  }
328  }
331  {
332  if(verboseObject_.get()) {
333  verboseObject_->setOStream(oldOStream_);
334  verboseObject_->setVerbLevel(oldVerbLevel_);
335  }
336  }
337 private:
338  RCP<const VerboseObject<ObjectType> > verboseObject_;
339  RCP<FancyOStream> oldOStream_;
340  EVerbosityLevel oldVerbLevel_;
341  // Not defined and not to be called
345 };
346 
347 
348 // //////////////////////////////////
349 // Template defintions
350 
351 
352 //
353 // VerboseObject
354 //
355 
356 
357 // Public static member functions
358 
359 
360 template<class ObjectType>
362 {
363  privateDefaultVerbLevel() = defaultVerbLevel;
364 }
365 
366 
367 template<class ObjectType>
369 {
370  return privateDefaultVerbLevel();
371 }
372 
373 
374 // Constructors/Initializers
375 
376 
377 template<class ObjectType>
379  const EVerbosityLevel verbLevel,
380  const RCP<FancyOStream> &oStream
381  )
382  : thisOverridingVerbLevel_(VERB_DEFAULT)
383 {
384  this->initializeVerboseObject(verbLevel,oStream);
385 }
386 
387 
388 template<class ObjectType>
390  const EVerbosityLevel verbLevel,
391  const RCP<FancyOStream> &oStream
392  )
393 {
394  thisVerbLevel_ = verbLevel;
395  this->initializeVerboseObjectBase(oStream);
396 }
397 
398 
399 template<class ObjectType>
402 {
403  thisVerbLevel_ = verbLevel;
405  return *this;
406 }
407 
408 
409 template<class ObjectType>
412  const EVerbosityLevel verbLevel
413  ) const
414 {
415  thisOverridingVerbLevel_ = verbLevel;
417  return *this;
418 }
419 
420 
421 // Query functions
422 
423 
424 template<class ObjectType>
426 {
427  if (VERB_DEFAULT != thisOverridingVerbLevel_)
428  return thisOverridingVerbLevel_;
429  if (VERB_DEFAULT == thisVerbLevel_)
430  return getDefaultVerbLevel();
431  return thisVerbLevel_;
432 }
433 
434 
435 // Private static members
436 
437 
438 template<class ObjectType>
440 {
441  static EVerbosityLevel defaultVerbLevel = VERB_DEFAULT;
442  return defaultVerbLevel;
443 }
444 
445 
446 } // namespace Teuchos
447 
448 
449 #endif // TEUCHOS_VERBOSE_OBJECT_HPP
Generate output as defined by the object.
static void setDefaultVerbLevel(const EVerbosityLevel defaultVerbLevel)
Set the default verbosity level.
EVerbosityLevel
Verbosity level.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
virtual void initializeVerboseObject(const EVerbosityLevel verbLevel=VERB_DEFAULT, const RCP< FancyOStream > &oStream=Teuchos::null)
Initialize the VerboseObject.
Set and release a stream and verbosity level.
VerboseObjectTempState(const RCP< const VerboseObject< ObjectType > > &verboseObject, const RCP< FancyOStream > &newOStream, const EVerbosityLevel newVerbLevel)
Templated base class for objects that can print their activities to a stream and have a verbosity lev...
static EVerbosityLevel getDefaultVerbLevel()
Get the default verbosity level.
Non-templated base class for objects that can print their activities to a stream. ...
virtual const VerboseObject & setVerbLevel(const EVerbosityLevel verbLevel) const
Set this object&#39;s verbosity level.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
virtual void initializeVerboseObjectBase(const RCP< FancyOStream > &oStream=Teuchos::null)
Calls initializeVerboseObject().
VerboseObject(const EVerbosityLevel verbLevel=VERB_DEFAULT, const RCP< FancyOStream > &oStream=Teuchos::null)
Constructor: calls initializeVerboseObject().
Smart reference counting pointer class for automatic garbage collection.
virtual EVerbosityLevel getVerbLevel() const
Get the verbosity level.
Reference-counted pointer class and non-member templated function implementations.
virtual void informUpdatedVerbosityState() const
Function that is called whenever the verbosity state is updated.
virtual const VerboseObject & setOverridingVerbLevel(const EVerbosityLevel verbLevel) const
Set the overriding verbosity level for *this object.