]> Creatis software - gdcm.git/blob - src/gdcmDebug.cxx
* Improvement #2 : the CommandManager is now a static class so,
[gdcm.git] / src / gdcmDebug.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDebug.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/28 16:31:22 $
7   Version:   $Revision: 1.28 $
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 
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::WarningFlag   = false;
32 bool Debug::OutputToFile  = false;
33
34 std::ofstream Debug::OutputFileStream;
35 std::ostream &Debug::StandardStream = std::cerr;
36
37 //-----------------------------------------------------------------------------
38 // Constructor / Destructor
39 Debug::Debug()
40 {
41 }
42
43 Debug::~Debug()
44 {
45   if ( OutputFileStream.is_open() )
46       OutputFileStream.close();     
47 }
48
49 //-----------------------------------------------------------------------------
50 // Public
51 /**
52  * \brief   Sets both the debug flag and warning flag
53  *          (both used for debugging purpose)
54  * @param   flag Set the debug flag and warning flag
55  */ 
56 void Debug::SetDebugFlag (bool flag) 
57 {
58    // To help tracking a bug, both flags are necessary
59    DebugFlag   = flag;
60    WarningFlag = flag;
61 }
62
63 /**
64  * \brief   Sets the warning flag
65  * @param   flag Set the warning flag
66  */ 
67 void Debug::SetWarningFlag (bool flag) 
68 {
69    // Cannot unset Warning flag if Debug flag is on.
70    if (flag == false && DebugFlag == true)
71       return;
72    WarningFlag = flag;
73 }
74
75 /**
76  * \brief   Accessor
77  * @param   flag whether we want to redirect to file
78  */ 
79 void Debug::SetOutputToFile (bool flag) 
80 {
81    OutputToFile = flag;
82 }
83
84 /**
85  * \brief   Accessor to know whether debug info are redirected to file
86  */ 
87 bool Debug::GetOutputToFile ()
88 {
89    return OutputToFile;
90 }
91
92 /**
93  * \brief Set the filename the debug stream should be redirect to
94  *        Settting a filename also sets DebugToFile to true
95  * @param   filename  File to redirect debug info
96  *          Absolutely nothing is check. You have to pass in
97  *          a correct filename
98  */ 
99 void Debug::SetOutputFileName (std::string const &filename)
100 {
101    OutputToFile = true;  // Just in case ... 
102    DebugFlag   = true;  // Just in case ...
103    if ( OutputFileStream.is_open() )
104       OutputFileStream.close();
105    OutputFileStream.open( filename.c_str() );
106 }
107
108 /**
109  * \brief Internal use only. Allow us to retrieve the static from anywhere
110  *        in gdcm code
111  * @return Debug file
112  */
113 std::ostream &Debug::GetOutput ()
114 {
115    if(OutputToFile)
116       return OutputFileStream;
117    else
118       return StandardStream;
119 }
120
121 void Debug::SendToOutput(unsigned int type,std::string const &msg,const Base *object)
122 {
123    bool executed=false;
124    executed=CommandManager::ExecuteCommandConst(object,type,msg);
125
126    if(!executed)
127       GetOutput() << Command::GetCommandAsString(type) << ": " << msg;
128 }
129
130 //-----------------------------------------------------------------------------
131 // Protected
132
133 //-----------------------------------------------------------------------------
134 // Private
135    
136 //-----------------------------------------------------------------------------
137 // Print
138
139 //-----------------------------------------------------------------------------
140 } // end namespace gdcm