MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkEndoDebug.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12#include "mitkEndoDebug.h"
13#include <itksys/SystemTools.hxx>
14#include <mutex>
15#include <fstream>
16#include <ctime>
17#include <cstdio>
18
19namespace mitk
20{
22 {
24 : m_DebugEnabled(false)
25 , m_ShowImagesInDebug(false)
26 , m_ShowImagesTimeOut(false)
28 {
29
30 }
31
32 std::set<std::string> m_FilesToDebug;
33 std::set<std::string> m_SymbolsToDebug;
37 std::ofstream m_Stream;
38 std::mutex m_Mutex;
40 };
41
43 : d ( new EndoDebugData )
44 {
45
46 }
47
49 {
50 if(d->m_Stream.is_open())
51 d->m_Stream.close();
52 delete d;
53 }
54
56 {
57 static EndoDebug instance;
58 return instance;
59 }
60
61 std::string EndoDebug::GetUniqueFileName( const std::string& dir, const std::string& ext, const std::string& prefix )
62 {
63 std::stringstream s;
64 s.precision( 0 );
65
66 std::string filename;
67 int i = 0;
68 while( filename.empty() || itksys::SystemTools::FileExists( (dir+"/"+filename).c_str() ) )
69 {
70 s.str("");
71 s << i;
72 filename = prefix + s.str() + "." + ext;
73 ++i;
74 }
75
76 filename = dir+"/"+filename;
77
78 return filename;
79 }
80
81 std::string EndoDebug::GetFilenameWithoutExtension(const std::string& s)
82 {
83 return itksys::SystemTools::GetFilenameWithoutExtension( s );
84 }
85
86 bool EndoDebug::AddFileToDebug(const std::string& s)
87 {
88 std::lock_guard<std::mutex> lock(d->m_Mutex);
89 std::pair<std::set<std::string>::iterator, bool> res = d->m_FilesToDebug.insert( s );
90 return res.second;
91 }
92
93 void EndoDebug::SetFilesToDebug(const std::set<std::string> &filesToDebug)
94 {
95 std::lock_guard<std::mutex> lock(d->m_Mutex);
96 d->m_FilesToDebug = filesToDebug;
97 }
98
99 std::set<std::string> EndoDebug::GetFilesToDebug()
100 {
101 std::lock_guard<std::mutex> lock(d->m_Mutex);
102 return d->m_FilesToDebug;
103 }
104
105 bool EndoDebug::AddSymbolToDebug(const std::string& symbolToDebug)
106 {
107 std::lock_guard<std::mutex> lock(d->m_Mutex);
108 std::pair<std::set<std::string>::iterator, bool> res = d->m_SymbolsToDebug.insert( symbolToDebug );
109 return res.second;
110 }
111
112 void EndoDebug::SetSymbolsToDebug(const std::set<std::string> &symbolsToDebug)
113 {
114 std::lock_guard<std::mutex> lock(d->m_Mutex);
115 d->m_SymbolsToDebug = symbolsToDebug;
116 }
117
118 std::set<std::string> EndoDebug::GetSymbolsToDebug()
119 {
120 std::lock_guard<std::mutex> lock(d->m_Mutex);
121 return d->m_SymbolsToDebug;
122 }
123
124 bool EndoDebug::DebugSymbol(const std::string& s)
125 {
126 std::lock_guard<std::mutex> lock(d->m_Mutex);
127 return d->m_SymbolsToDebug.find(s)
128 != d->m_SymbolsToDebug.end();
129 }
130
131 bool EndoDebug::DebugFile(const std::string& s)
132 {
133 std::string filename = GetFilenameWithoutExtension(s);
134
135 std::lock_guard<std::mutex> lock(d->m_Mutex);
136 return d->m_FilesToDebug.find(filename)
137 != d->m_FilesToDebug.end();
138 }
139
140 bool EndoDebug::Debug( const std::string& fileToDebug, const std::string& symbol )
141 {
142 bool debug = false;
143
144 {
145 bool debugEnabled = false;
146 size_t filesSize = 0;
147 size_t symbolsSize = 0;
148 bool symbolFound = false;
149 {
150 std::lock_guard<std::mutex> lock(d->m_Mutex);
151 debugEnabled = d->m_DebugEnabled;
152 filesSize = d->m_FilesToDebug.size();
153 symbolsSize = d->m_SymbolsToDebug.size();
154 symbolFound = d->m_SymbolsToDebug.find(symbol) != d->m_SymbolsToDebug.end();
155 }
156
157 if( debugEnabled )
158 {
159 if( filesSize == 0 )
160 debug = true;
161 else
162 debug = DebugFile(fileToDebug);
163
164 // ok debug is determined so far, now check if symbol set
165 if( symbolsSize > 0 )
166 {
167 debug = symbolFound;
168 }
169 else
170 {
171 // do not show symbol debug output if no symbols are set at all
172 if( !symbol.empty() )
173 debug = false;
174 }
175 }
176 }
177
178 return debug;
179 }
180
181 void EndoDebug::SetDebugEnabled(bool _DebugEnabled)
182 {
183 std::lock_guard<std::mutex> lock(d->m_Mutex);
184 d->m_DebugEnabled = _DebugEnabled;
185 }
186
187 void EndoDebug::SetDebugImagesOutputDirectory(const std::string& _DebugImagesOutputDirectory)
188 {
189 std::lock_guard<std::mutex> lock(d->m_Mutex);
190 d->m_DebugImagesOutputDirectory = _DebugImagesOutputDirectory;
191 }
192
194 {
195 std::lock_guard<std::mutex> lock(d->m_Mutex);
196 return d->m_DebugEnabled;
197 }
198
199 void EndoDebug::SetShowImagesInDebug(bool _ShowImagesInDebug)
200 {
201 std::lock_guard<std::mutex> lock(d->m_Mutex);
202 d->m_ShowImagesInDebug = _ShowImagesInDebug;
203 }
204
206 {
207 std::lock_guard<std::mutex> lock(d->m_Mutex);
208 return d->m_ShowImagesInDebug;
209 }
210
211 void EndoDebug::SetShowImagesTimeOut(size_t _ShowImagesTimeOut)
212 {
213 std::lock_guard<std::mutex> lock(d->m_Mutex);
214 d->m_ShowImagesTimeOut = _ShowImagesTimeOut;
215 }
216
218 {
219 std::lock_guard<std::mutex> lock(d->m_Mutex);
221 }
222
224 {
225 std::lock_guard<std::mutex> lock(d->m_Mutex);
226 return d->m_ShowImagesTimeOut;
227 }
228
229 void EndoDebug::SetLogFile( const std::string& file )
230 {
231 std::lock_guard<std::mutex> lock(d->m_Mutex);
232 d->m_Stream.open ( file.c_str(), std::ios::out | std::ios::app);
233 }
234
235 void EndoDebug::ShowMessage( const std::string& message )
236 {
237 std::lock_guard<std::mutex> lock(d->m_Mutex);
238 if(d->m_Stream.is_open())
239 {
240 char *timestr;
241 struct tm *newtime;
242 time_t aclock;
243 time(&aclock);
244 newtime = localtime(&aclock);
245 timestr = asctime(newtime);
246
247 d->m_Stream << timestr << ", " << message;
248 }
249 else
250 std::cout << message << std::flush;
251 }
252}
IGT Exceptions.
std::string m_DebugImagesOutputDirectory
std::ofstream m_Stream
std::set< std::string > m_FilesToDebug
std::set< std::string > m_SymbolsToDebug
void SetFilesToDebug(const std::set< std::string > &filesToDebug)
std::string GetFilenameWithoutExtension(const std::string &s)
std::set< std::string > GetSymbolsToDebug()
void SetSymbolsToDebug(const std::set< std::string > &symbolsToDebug)
bool Debug(const std::string &fileToDebug, const std::string &symbol="")
bool DebugSymbol(const std::string &symbolToDebug)
void SetShowImagesInDebug(bool _ShowImagesInDebug)
bool AddSymbolToDebug(const std::string &symbolToDebug)
void SetShowImagesTimeOut(size_t _ShowImagesTimeOut)
static std::string GetUniqueFileName(const std::string &dir, const std::string &ext="jpg", const std::string &prefix="")
void SetDebugImagesOutputDirectory(const std::string &_DebugImagesOutputDirectory)
size_t GetShowImagesTimeOut()
bool DebugFile(const std::string &fileToDebug)
static EndoDebug & GetInstance()
void SetDebugEnabled(bool _DebugEnabled)
std::string GetDebugImagesOutputDirectory() const
void ShowMessage(const std::string &message)
void SetLogFile(const std::string &file)
std::set< std::string > GetFilesToDebug()
bool AddFileToDebug(const std::string &fileToDebug)