Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Misc/MsqDebug.cpp
Go to the documentation of this file.
1 /* *****************************************************************
2  MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4  Copyright 2004 Sandia Corporation and Argonne National
5  Laboratory. Under the terms of Contract DE-AC04-94AL85000
6  with Sandia Corporation, the U.S. Government retains certain
7  rights in this software.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  (lgpl.txt) along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  ***************************************************************** */
24 
25 #include "MsqDebug.hpp"
26 
27 #include <stdio.h>
28 #include <stdarg.h>
29 
30 namespace Mesquite
31 {
32 
33 /* IMPORTANT: Be careful to initialize the InitializeFlags object
34  *LAST* as it will access the other static data. */
35 
36 msq_std::vector<msq_stdio::ostream*> MsqDebug::streams;
37 msq_std::vector<bool> MsqDebug::flags;
38 
39 #ifdef MSQ_ENABLE_DEBUG
41 {
42  const unsigned flag_array[] = { MSQ_ENABLE_DEBUG, 0 };
43  size_t length = sizeof(flag_array) / sizeof(unsigned) - 1;
44  while (length > 0)
45  MsqDebug::set( flag_array[--length], true );
46 }
47 #else
49 #endif
50 
52 
53 
54 bool MsqDebug::get( unsigned flag )
55 {
56  return flag < flags.size() && flags[flag];
57 }
58 
59 void MsqDebug::set( unsigned flag, bool state )
60 {
61  if (state)
62  {
63  if (flag >= flags.size())
64  {
65  flags.resize(flag+1);
66  }
67  flags[flag] = true;
68  }
69  else
70  {
71  if (flag < flags.size())
72  flags[flag] = false;
73  }
74 }
75 
77 {
78  flags.clear();
79 }
80 
81 msq_stdio::ostream& MsqDebug::get_stream( unsigned flag )
82 {
83  if (flag < streams.size())
84  return *streams[flag];
85  else
86  return msq_stdio::cout;
87 }
88 
89 void MsqDebug::set_stream( unsigned flag, msq_stdio::ostream& stream )
90 {
91  if (flag >= streams.size())
92  {
93  size_t old_size = streams.size();
94  streams.resize( flag );
95  for (unsigned i = old_size; i < flag; ++i)
96  streams[i] = &msq_stdio::cout;
97  }
98  streams[flag] = &stream;
99 }
100 
101 
102 void MsqDebug::FormatPrinter::print( const char* format, ... ) const
103 {
104  if (!MsqDebug::get( myFlag ))
105  return;
106 
107  char buffer[512];
108 
109 #if defined(HAVE_VSNPRINTF)
110  va_list args;
111  va_start( args, format );
112  vsnprintf( buffer, sizeof(buffer), format, args );
113  va_end( args );
114 #elif defined(HAVE__VSNPRINTF)
115  va_list args;
116  va_start( args, format );
117  _vsnprintf( buffer, sizeof(buffer), format, args );
118  va_end( args );
119 #elif defined(HAVE_VSPRINTF)
120  va_list args;
121  va_start( args, format );
122  vsprintf( buffer, format, args );
123  va_end( args );
124 #else
125  strncpy( buffer, sizeof(buffer), format );
126  buffer[sizeof(buffer)-1] = '\0';
127 #endif
128 
129  MsqDebug::get_stream( myFlag ) << buffer;
130 }
131 
132 }
133 
static msq_std::vector< msq_stdio::ostream * > streams
static bool get(unsigned flag)
Check a debug flag.
static msq_stdio::ostream & get_stream(unsigned flag)
Get the output stream to be used for a given debug flag.
double length(Vector3D *const v, int n)
static msq_std::vector< bool > flags
static void set(unsigned flag, bool state)
Set a debug flag.
static InitializeFlags init
static void set_stream(unsigned flag, msq_stdio::ostream &stream)
Set the output stream to be used for a given debug flag.
blockLoc i
Definition: read.cpp:79
void print(const char *format,...) const
static void disable_all()
Disable all debug streams.