]> Creatis software - gdcm.git/blob - src/gdcmDebug.cxx
Fix mistypings
[gdcm.git] / src / gdcmDebug.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:08 $
7   Version:   $Revision: 1.31 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDebug.h"
20 #include "gdcmCommandManager.h"
21
22 #include <iostream>
23
24 namespace GDCM_NAME_SPACE 
25 {
26 //-----------------------------------------------------------------------------
27 // Warning message level to be displayed
28 const int Debug::LINE_LENGTH = 79;
29
30 bool Debug::DebugFlag     = false;
31 bool Debug::LogFlag       = false;
32 bool Debug::WarningFlag   = false;
33 bool Debug::OutputToFile  = false;
34
35 std::ofstream Debug::OutputFileStream;
36 std::ostream &Debug::StandardStream = std::cerr;
37
38 //-----------------------------------------------------------------------------
39 // Constructor / Destructor
40 Debug::Debug()
41 {
42 }
43
44 Debug::~Debug()
45 {
46   if ( OutputFileStream.is_open() )
47       OutputFileStream.close();     
48 }
49
50 //-----------------------------------------------------------------------------
51 // Public
52 /**
53  * \brief   Sets both the debug flag and warning flag
54  *          (both used for debugging purpose)
55  * @param   flag Set the debug flag and warning flag
56  */ 
57 void Debug::SetDebugFlag (bool flag) 
58 {
59    // To help tracking a bug, both flags are necessary
60    DebugFlag   = flag;
61    WarningFlag = flag;
62 }
63
64 /**
65  * \brief   Sets the warning flag
66  * @param   flag Set the warning flag
67  */ 
68 void Debug::SetWarningFlag (bool flag) 
69 {
70    // Cannot unset Warning flag if Debug flag is on or if LogFlag is on.
71    if (flag == false)
72    { 
73       if (DebugFlag == true)
74          return;
75       if (LogFlag == true)
76          return;
77    }
78    WarningFlag = flag;
79 }
80
81 /**
82  * \brief   Sets the log flag
83  * @param   flag Set the log flag
84  */ 
85 void Debug::SetLogFlag (bool flag) 
86 {
87    // To log oddities, both flags are necessary      
88    WarningFlag = flag;   
89    LogFlag = flag;
90 }
91
92 /**
93  * \brief   Accessor
94  * @param   flag whether we want to redirect to file
95  */ 
96 void Debug::SetOutputToFile (bool flag) 
97 {
98    OutputToFile = flag;
99 }
100
101 /**
102  * \brief   Accessor to know whether debug info are redirected to file
103  */ 
104 bool Debug::GetOutputToFile ()
105 {
106    return OutputToFile;
107 }
108
109 /**
110  * \brief Set the filename the debug stream should be redirect to
111  *        Settting a filename also sets DebugToFile to true
112  * @param   filename  File to redirect debug info
113  *          Absolutely nothing is check. You have to pass in
114  *          a correct filename
115  */ 
116 void Debug::SetOutputFileName (std::string const &filename)
117 {
118    OutputToFile = true;  // Just in case ... 
119    DebugFlag   = true;  // Just in case ...
120    if ( OutputFileStream.is_open() )
121       OutputFileStream.close();
122    OutputFileStream.open( filename.c_str() );
123 }
124
125 /**
126  * \brief Internal use only. Allow us to retrieve the static from anywhere
127  *        in gdcm code
128  * @return Debug file
129  */
130 std::ostream &Debug::GetOutput ()
131 {
132    if(OutputToFile)
133       return OutputFileStream;
134    else
135       return StandardStream;
136 }
137
138 void Debug::SendToOutput(unsigned int type,std::string const &msg,const Base *object)
139 {
140    bool executed=false;
141    if( type != CMD_DEBUG && type != CMD_ASSERT )
142       executed=CommandManager::ExecuteCommandConst(object,type,msg);
143
144    if(!executed)
145       GetOutput() << Command::GetCommandAsString(type) << ": " << msg;
146 }
147
148 //-----------------------------------------------------------------------------
149 // Protected
150
151 //-----------------------------------------------------------------------------
152 // Private
153    
154 //-----------------------------------------------------------------------------
155 // Print
156
157 //-----------------------------------------------------------------------------
158 } // end namespace gdcm