Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
102  virtual const VerboseObjectBase& setOStream(
103  const RCP<FancyOStream> &oStream) const;
104 
111  virtual const VerboseObjectBase& setOverridingOStream(
112  const RCP<FancyOStream> &oStream) const;
113 
115  virtual VerboseObjectBase& setLinePrefix(const std::string &linePrefix);
116 
118 
120 
121 
125  virtual RCP<FancyOStream> getOStream() const;
126 
133  virtual RCP<FancyOStream> getOverridingOStream() const;
134 
136  virtual std::string getLinePrefix() const;
137 
139 
141 
142 
156  virtual OSTab getOSTab(const int tabs = 1, const std::string &linePrefix = "") const;
157 
159 
160 protected:
161 
164  void initializeVerboseObjectBase(
165  const RCP<FancyOStream> &oStream = Teuchos::null
166  );
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 
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 
264  virtual const VerboseObject& setVerbLevel(
265  const EVerbosityLevel verbLevel) const;
266 
273  virtual const VerboseObject& setOverridingVerbLevel(
274  const EVerbosityLevel verbLevel) const;
275 
277 
279 
281  virtual EVerbosityLevel getVerbLevel() const;
282 
284 
285 protected:
286 
295  const EVerbosityLevel verbLevel = VERB_DEFAULT,
296  const RCP<FancyOStream> &oStream = Teuchos::null
297  );
298 
299 private:
300 
301  mutable EVerbosityLevel thisVerbLevel_;
302  mutable EVerbosityLevel thisOverridingVerbLevel_;
303 
304  static EVerbosityLevel& privateDefaultVerbLevel();
305 
306 };
307 
308 
312 template<class ObjectType>
314 public:
317  const RCP<const VerboseObject<ObjectType> > &verboseObject,
318  const RCP<FancyOStream> &newOStream,
319  const EVerbosityLevel newVerbLevel
320  ):
321  verboseObject_(verboseObject),
322  oldVerbLevel_(VERB_DEFAULT)
323  {
324  if(verboseObject_.get()) {
325  oldOStream_ = verboseObject_->getOStream();
326  oldVerbLevel_ = verboseObject_->getVerbLevel();
327  verboseObject_->setOStream(newOStream);
328  verboseObject_->setVerbLevel(newVerbLevel);
329  }
330  }
333  {
334  if(verboseObject_.get()) {
335  verboseObject_->setOStream(oldOStream_);
336  verboseObject_->setVerbLevel(oldVerbLevel_);
337  }
338  }
339 private:
340  RCP<const VerboseObject<ObjectType> > verboseObject_;
341  RCP<FancyOStream> oldOStream_;
342  EVerbosityLevel oldVerbLevel_;
343  // Not defined and not to be called
347 };
348 
349 
350 // //////////////////////////////////
351 // Template defintions
352 
353 
354 //
355 // VerboseObject
356 //
357 
358 
359 // Public static member functions
360 
361 
362 template<class ObjectType>
364 {
365  privateDefaultVerbLevel() = defaultVerbLevel;
366 }
367 
368 
369 template<class ObjectType>
371 {
372  return privateDefaultVerbLevel();
373 }
374 
375 
376 // Constructors/Initializers
377 
378 
379 template<class ObjectType>
381  const EVerbosityLevel verbLevel,
382  const RCP<FancyOStream> &oStream
383  )
384  : thisOverridingVerbLevel_(VERB_DEFAULT)
385 {
386  this->initializeVerboseObject(verbLevel,oStream);
387 }
388 
389 
390 template<class ObjectType>
392  const EVerbosityLevel verbLevel,
393  const RCP<FancyOStream> &oStream
394  )
395 {
396  thisVerbLevel_ = verbLevel;
397  this->initializeVerboseObjectBase(oStream);
398 }
399 
400 
401 template<class ObjectType>
404 {
405  thisVerbLevel_ = verbLevel;
406  informUpdatedVerbosityState();
407  return *this;
408 }
409 
410 
411 template<class ObjectType>
414  const EVerbosityLevel verbLevel
415  ) const
416 {
417  thisOverridingVerbLevel_ = verbLevel;
418  informUpdatedVerbosityState();
419  return *this;
420 }
421 
422 
423 // Query functions
424 
425 
426 template<class ObjectType>
428 {
429  if (VERB_DEFAULT != thisOverridingVerbLevel_)
430  return thisOverridingVerbLevel_;
431  if (VERB_DEFAULT == thisVerbLevel_)
432  return getDefaultVerbLevel();
433  return thisVerbLevel_;
434 }
435 
436 
437 // Private static members
438 
439 
440 template<class ObjectType>
442 {
443  static EVerbosityLevel defaultVerbLevel = VERB_DEFAULT;
444  return defaultVerbLevel;
445 }
446 
447 
448 } // namespace Teuchos
449 
450 
451 #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...
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.
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 const VerboseObject & setOverridingVerbLevel(const EVerbosityLevel verbLevel) const
Set the overriding verbosity level for *this object.