Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 #include "mitkEndoDebug.h"
13 #include <itksys/SystemTools.hxx>
14 #include <itkFastMutexLock.h>
15 #include <itkMutexLockHolder.h>
16 #include <fstream>
17 #include <ctime>
18 #include <cstdio>
19 
20 namespace mitk
21 {
22  struct EndoDebugData
23  {
24  EndoDebugData()
25  : m_DebugEnabled(false)
26  , m_ShowImagesInDebug(false)
27  , m_ShowImagesTimeOut(false)
28  , m_Mutex(itk::FastMutexLock::New())
29  , m_DebugImagesOutputDirectory("")
30  {
31 
32  }
33 
34  std::set<std::string> m_FilesToDebug;
35  std::set<std::string> m_SymbolsToDebug;
36  bool m_DebugEnabled;
37  bool m_ShowImagesInDebug;
38  size_t m_ShowImagesTimeOut;
39  std::ofstream m_Stream;
40  itk::FastMutexLock::Pointer m_Mutex;
41  std::string m_DebugImagesOutputDirectory;
42  };
43 
45  : d ( new EndoDebugData )
46  {
47 
48  }
49 
51  {
52  if(d->m_Stream.is_open())
53  d->m_Stream.close();
54  delete d;
55  }
56 
58  {
59  static EndoDebug instance;
60  return instance;
61  }
62 
63  std::string EndoDebug::GetUniqueFileName( const std::string& dir, const std::string& ext, const std::string& prefix )
64  {
65  std::stringstream s;
66  s.precision( 0 );
67 
68  std::string filename;
69  int i = 0;
70  while( filename.empty() || itksys::SystemTools::FileExists( (dir+"/"+filename).c_str() ) )
71  {
72  s.str("");
73  s << i;
74  filename = prefix + s.str() + "." + ext;
75  ++i;
76  }
77 
78  filename = dir+"/"+filename;
79 
80  return filename;
81  }
82 
83  std::string EndoDebug::GetFilenameWithoutExtension(const std::string& s)
84  {
85  return itksys::SystemTools::GetFilenameWithoutExtension( s );
86  }
87 
88  bool EndoDebug::AddFileToDebug(const std::string& s)
89  {
90  {
92  std::pair<std::set<std::string>::iterator, bool> res = d->m_FilesToDebug.insert( s );
93  return res.second;
94  }
95  }
96 
97  void EndoDebug::SetFilesToDebug(const std::set<std::string> &filesToDebug)
98  {
99  {
101  d->m_FilesToDebug = filesToDebug;
102  }
103  }
104 
105  std::set<std::string> EndoDebug::GetFilesToDebug()
106  {
107  {
109  return d->m_FilesToDebug;
110  }
111  }
112 
113  bool EndoDebug::AddSymbolToDebug(const std::string& symbolToDebug)
114  {
115  {
117  std::pair<std::set<std::string>::iterator, bool> res = d->m_SymbolsToDebug.insert( symbolToDebug );
118  return res.second;
119  }
120  }
121 
122  void EndoDebug::SetSymbolsToDebug(const std::set<std::string> &symbolsToDebug)
123  {
124  {
126  d->m_SymbolsToDebug = symbolsToDebug;
127  }
128  }
129 
130  std::set<std::string> EndoDebug::GetSymbolsToDebug()
131  {
132  {
134  return d->m_SymbolsToDebug;
135  }
136  }
137 
138  bool EndoDebug::DebugSymbol(const std::string& s)
139  {
140  {
142  return d->m_SymbolsToDebug.find(s)
143  != d->m_SymbolsToDebug.end();
144  }
145  }
146 
147  bool EndoDebug::DebugFile(const std::string& s)
148  {
149  std::string filename = GetFilenameWithoutExtension(s);
150 
151  {
153  return d->m_FilesToDebug.find(filename)
154  != d->m_FilesToDebug.end();
155  }
156  }
157 
158  bool EndoDebug::Debug( const std::string& fileToDebug, const std::string& symbol )
159  {
160  bool debug = false;
161 
162  {
163  bool debugEnabled = false;
164  size_t filesSize = 0;
165  size_t symbolsSize = 0;
166  bool symbolFound = false;
167  {
169  debugEnabled = d->m_DebugEnabled;
170  filesSize = d->m_FilesToDebug.size();
171  symbolsSize = d->m_SymbolsToDebug.size();
172  symbolFound = d->m_SymbolsToDebug.find(symbol) != d->m_SymbolsToDebug.end();
173  }
174 
175  if( debugEnabled )
176  {
177  if( filesSize == 0 )
178  debug = true;
179  else
180  debug = DebugFile(fileToDebug);
181 
182  // ok debug is determined so far, now check if symbol set
183  if( symbolsSize > 0 )
184  {
185  debug = symbolFound;
186  }
187  else
188  {
189  // do not show symbol debug output if no symbols are set at all
190  if( !symbol.empty() )
191  debug = false;
192  }
193  }
194  }
195 
196  return debug;
197  }
198 
199  void EndoDebug::SetDebugEnabled(bool _DebugEnabled)
200  {
201  {
203  d->m_DebugEnabled = _DebugEnabled;
204  }
205  }
206 
207  void EndoDebug::SetDebugImagesOutputDirectory(const std::string& _DebugImagesOutputDirectory)
208  {
209  {
211  d->m_DebugImagesOutputDirectory = _DebugImagesOutputDirectory;
212  }
213 
214  }
215 
217  {
218  {
220  return d->m_DebugEnabled;
221  }
222  }
223 
224  void EndoDebug::SetShowImagesInDebug(bool _ShowImagesInDebug)
225  {
226  {
228  d->m_ShowImagesInDebug = _ShowImagesInDebug;
229  }
230  }
231 
233  {
234  {
236  return d->m_ShowImagesInDebug;
237  }
238  }
239 
240  void EndoDebug::SetShowImagesTimeOut(size_t _ShowImagesTimeOut)
241  {
242  {
244  d->m_ShowImagesTimeOut = _ShowImagesTimeOut;
245  }
246  }
247 
249  {
250  {
252  return d->m_DebugImagesOutputDirectory;
253  }
254  }
255 
257  {
258  {
260  return d->m_ShowImagesTimeOut;
261  }
262  }
263 
264  void EndoDebug::SetLogFile( const std::string& file )
265  {
266  {
268  d->m_Stream.open ( file.c_str(), std::ios::out | std::ios::app);
269  }
270  }
271 
272  void EndoDebug::ShowMessage( const std::string& message )
273  {
274  {
276  if(d->m_Stream.is_open())
277  {
278  char *timestr;
279  struct tm *newtime;
280  time_t aclock;
281  time(&aclock);
282  newtime = localtime(&aclock);
283  timestr = asctime(newtime);
284 
285  d->m_Stream << timestr << ", " << message;
286  }
287  else
288  std::cout << message << std::flush;
289  }
290  }
291 }
bool AddSymbolToDebug(const std::string &symbolToDebug)
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)
std::string GetDebugImagesOutputDirectory() const
void SetShowImagesTimeOut(vcl_size_t _ShowImagesTimeOut)
bool AddFileToDebug(const std::string &fileToDebug)
void SetLogFile(const std::string &file)
void ShowMessage(const std::string &message)
std::set< std::string > GetFilesToDebug()
bool GetShowImagesInDebug()
std::set< std::string > GetSymbolsToDebug()
virtual ~EndoDebug()
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)