Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkEndoDebug.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 #include "mitkEndoDebug.h"
17 #include <itksys/SystemTools.hxx>
18 #include <itkFastMutexLock.h>
19 #include <itkMutexLockHolder.h>
20 #include <fstream>
21 #include <ctime>
22 #include <cstdio>
23 
24 namespace mitk
25 {
26  struct EndoDebugData
27  {
28  EndoDebugData()
29  : m_DebugEnabled(false)
30  , m_ShowImagesInDebug(false)
31  , m_ShowImagesTimeOut(false)
32  , m_Mutex(itk::FastMutexLock::New())
33  , m_DebugImagesOutputDirectory("")
34  {
35 
36  }
37 
38  std::set<std::string> m_FilesToDebug;
39  std::set<std::string> m_SymbolsToDebug;
40  bool m_DebugEnabled;
41  bool m_ShowImagesInDebug;
42  size_t m_ShowImagesTimeOut;
43  std::ofstream m_Stream;
45  std::string m_DebugImagesOutputDirectory;
46  };
47 
49  : d ( new EndoDebugData )
50  {
51 
52  }
53 
55  {
56  if(d->m_Stream.is_open())
57  d->m_Stream.close();
58  delete d;
59  }
60 
62  {
63  static EndoDebug instance;
64  return instance;
65  }
66 
67  std::string EndoDebug::GetUniqueFileName( const std::string& dir, const std::string& ext, const std::string& prefix )
68  {
69  std::stringstream s;
70  s.precision( 0 );
71 
72  std::string filename;
73  int i = 0;
74  while( filename.empty() || itksys::SystemTools::FileExists( (dir+"/"+filename).c_str() ) )
75  {
76  s.str("");
77  s << i;
78  filename = prefix + s.str() + "." + ext;
79  ++i;
80  }
81 
82  filename = dir+"/"+filename;
83 
84  return filename;
85  }
86 
87  std::string EndoDebug::GetFilenameWithoutExtension(const std::string& s)
88  {
89  return itksys::SystemTools::GetFilenameWithoutExtension( s );
90  }
91 
92  bool EndoDebug::AddFileToDebug(const std::string& s)
93  {
94  {
96  std::pair<std::set<std::string>::iterator, bool> res = d->m_FilesToDebug.insert( s );
97  return res.second;
98  }
99  }
100 
101  void EndoDebug::SetFilesToDebug(const std::set<std::string> &filesToDebug)
102  {
103  {
105  d->m_FilesToDebug = filesToDebug;
106  }
107  }
108 
109  std::set<std::string> EndoDebug::GetFilesToDebug()
110  {
111  {
113  return d->m_FilesToDebug;
114  }
115  }
116 
117  bool EndoDebug::AddSymbolToDebug(const std::string& symbolToDebug)
118  {
119  {
121  std::pair<std::set<std::string>::iterator, bool> res = d->m_SymbolsToDebug.insert( symbolToDebug );
122  return res.second;
123  }
124  }
125 
126  void EndoDebug::SetSymbolsToDebug(const std::set<std::string> &symbolsToDebug)
127  {
128  {
130  d->m_SymbolsToDebug = symbolsToDebug;
131  }
132  }
133 
134  std::set<std::string> EndoDebug::GetSymbolsToDebug()
135  {
136  {
138  return d->m_SymbolsToDebug;
139  }
140  }
141 
142  bool EndoDebug::DebugSymbol(const std::string& s)
143  {
144  {
146  return d->m_SymbolsToDebug.find(s)
147  != d->m_SymbolsToDebug.end();
148  }
149  }
150 
151  bool EndoDebug::DebugFile(const std::string& s)
152  {
153  std::string filename = GetFilenameWithoutExtension(s);
154 
155  {
157  return d->m_FilesToDebug.find(filename)
158  != d->m_FilesToDebug.end();
159  }
160  }
161 
162  bool EndoDebug::Debug( const std::string& fileToDebug, const std::string& symbol )
163  {
164  bool debug = false;
165 
166  {
167  bool debugEnabled = false;
168  size_t filesSize = 0;
169  size_t symbolsSize = 0;
170  bool symbolFound = false;
171  {
173  debugEnabled = d->m_DebugEnabled;
174  filesSize = d->m_FilesToDebug.size();
175  symbolsSize = d->m_SymbolsToDebug.size();
176  symbolFound = d->m_SymbolsToDebug.find(symbol) != d->m_SymbolsToDebug.end();
177  }
178 
179  if( debugEnabled )
180  {
181  if( filesSize == 0 )
182  debug = true;
183  else
184  debug = DebugFile(fileToDebug);
185 
186  // ok debug is determined so far, now check if symbol set
187  if( symbolsSize > 0 )
188  {
189  debug = symbolFound;
190  }
191  else
192  {
193  // do not show symbol debug output if no symbols are set at all
194  if( !symbol.empty() )
195  debug = false;
196  }
197  }
198  }
199 
200  return debug;
201  }
202 
203  void EndoDebug::SetDebugEnabled(bool _DebugEnabled)
204  {
205  {
207  d->m_DebugEnabled = _DebugEnabled;
208  }
209  }
210 
211  void EndoDebug::SetDebugImagesOutputDirectory(const std::string& _DebugImagesOutputDirectory)
212  {
213  {
215  d->m_DebugImagesOutputDirectory = _DebugImagesOutputDirectory;
216  }
217 
218  }
219 
221  {
222  {
224  return d->m_DebugEnabled;
225  }
226  }
227 
228  void EndoDebug::SetShowImagesInDebug(bool _ShowImagesInDebug)
229  {
230  {
232  d->m_ShowImagesInDebug = _ShowImagesInDebug;
233  }
234  }
235 
237  {
238  {
240  return d->m_ShowImagesInDebug;
241  }
242  }
243 
244  void EndoDebug::SetShowImagesTimeOut(size_t _ShowImagesTimeOut)
245  {
246  {
248  d->m_ShowImagesTimeOut = _ShowImagesTimeOut;
249  }
250  }
251 
253  {
254  {
256  return d->m_DebugImagesOutputDirectory;
257  }
258  }
259 
261  {
262  {
264  return d->m_ShowImagesTimeOut;
265  }
266  }
267 
268  void EndoDebug::SetLogFile( const std::string& file )
269  {
270  {
272  d->m_Stream.open ( file.c_str(), std::ios::out | std::ios::app);
273  }
274  }
275 
276  void EndoDebug::ShowMessage( const std::string& message )
277  {
278  {
280  if(d->m_Stream.is_open())
281  {
282  char *timestr;
283  struct tm *newtime;
284  time_t aclock;
285  time(&aclock);
286  newtime = localtime(&aclock);
287  timestr = asctime(newtime);
288 
289  d->m_Stream << timestr << ", " << message;
290  }
291  else
292  std::cout << message << std::flush;
293  }
294  }
295 }
bool AddSymbolToDebug(const std::string &symbolToDebug)
itk::SmartPointer< Self > Pointer
static EndoDebug & GetInstance()
void SetSymbolsToDebug(const std::set< std::string > &symbolsToDebug)
void SetShowImagesInDebug(bool _ShowImagesInDebug)
DataCollection - Class to facilitate loading/accessing structured data.
bool DebugSymbol(const std::string &symbolToDebug)
std::string GetFilenameWithoutExtension(const std::string &s)
void SetDebugEnabled(bool _DebugEnabled)
void SetShowImagesTimeOut(vcl_size_t _ShowImagesTimeOut)
bool AddFileToDebug(const std::string &fileToDebug)
void SetLogFile(const std::string &file)
void ShowMessage(const std::string &message)
static const std::string filename
std::set< std::string > GetFilesToDebug()
bool GetShowImagesInDebug()
std::set< std::string > GetSymbolsToDebug()
virtual ~EndoDebug()
std::string GetDebugImagesOutputDirectory() const
void SetFilesToDebug(const std::set< std::string > &filesToDebug)
void SetDebugImagesOutputDirectory(const std::string &_DebugImagesOutputDirectory)
bool Debug(const std::string &fileToDebug, const std::string &symbol="")
vcl_size_t GetShowImagesTimeOut()
static std::string GetUniqueFileName(const std::string &dir, const std::string &ext="jpg", const std::string &prefix="")
bool DebugFile(const std::string &fileToDebug)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.