Orfeo Toolbox  3.16
itkThreadLogger.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkThreadLogger.cxx,v $
5  Language: C++
6  Date: $Date: 2009-03-03 15:09:50 $
7  Version: $Revision: 1.5 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #include<iostream>
19 #include "itkThreadLogger.h"
20 
21 
22 namespace itk
23 {
24 
29 {
30  this->m_WaitMutex.Unlock();
31  this->m_Mutex.Lock();
32  this->m_OperationQ.push(SET_PRIORITY_LEVEL);
33  this->m_LevelQ.push(level);
34  this->m_Mutex.Unlock();
35  this->m_WaitMutex.Lock();
36 }
37 
42 {
43  this->m_Mutex.Lock();
44  PriorityLevelType level = this->m_PriorityLevel;
45  this->m_Mutex.Unlock();
46  return level;
47 }
48 
50 {
51  this->m_WaitMutex.Unlock();
52  this->m_Mutex.Lock();
53  this->m_LevelForFlushing = level;
55  this->m_LevelQ.push(level);
56  this->m_Mutex.Unlock();
57  this->m_WaitMutex.Lock();
58 }
59 
61 {
62  this->m_Mutex.Lock();
64  this->m_Mutex.Unlock();
65  return level;
66 }
67 
70 {
71  this->m_WaitMutex.Unlock();
72  this->m_Mutex.Lock();
73  this->m_OperationQ.push(ADD_LOG_OUTPUT);
74  this->m_OutputQ.push(output);
75  this->m_Mutex.Unlock();
76  this->m_WaitMutex.Lock();
77 }
78 
79 void ThreadLogger::Write(PriorityLevelType level, std::string const & content)
80 {
81  this->m_WaitMutex.Unlock();
82  this->m_Mutex.Lock();
83  this->m_OperationQ.push(WRITE);
84  this->m_MessageQ.push(content);
85  this->m_LevelQ.push(level);
86  this->m_Mutex.Unlock();
87  this->m_WaitMutex.Lock();
88 }
89 
90 
92 {
93  this->m_Mutex.Lock();
94 
95  while( !this->m_OperationQ.empty() )
96  {
97  switch( this->m_OperationQ.front() )
98  {
100  this->m_PriorityLevel = this->m_LevelQ.front();
101  this->m_LevelQ.pop();
102  break;
103 
105  this->m_LevelForFlushing = this->m_LevelQ.front();
106  this->m_LevelQ.pop();
107  break;
108 
110  this->m_Output->AddLogOutput(this->m_OutputQ.front());
111  this->m_OutputQ.pop();
112  break;
113 
114  case ThreadLogger::WRITE:
115  this->Logger::Write(this->m_LevelQ.front(), this->m_MessageQ.front());
116  this->m_LevelQ.pop();
117  this->m_MessageQ.pop();
118  break;
119  case ThreadLogger::FLUSH:
120  this->Logger::Flush();
121  break;
122  }
123  this->m_OperationQ.pop();
124  }
125  this->m_Output->Flush();
126  this->m_Mutex.Unlock();
127 
128 }
129 
130 
133 {
134  this->m_WaitMutex.Lock();
135  this->m_Threader = MultiThreader::New();
136  this->m_ThreadID = this->m_Threader->SpawnThread(ThreadFunction, this);
137 }
138 
139 
142 {
143  this->m_WaitMutex.Unlock();
144  if( this->m_Threader )
145  {
146  this->m_Threader->TerminateThread(this->m_ThreadID);
147  }
148 }
149 
150 
152 {
153  struct MultiThreader::ThreadInfoStruct * pInfo = (struct MultiThreader::ThreadInfoStruct*)pInfoStruct;
154 
155  if( pInfo == NULL )
156  {
158  }
159 
160  if( pInfo->UserData == NULL )
161  {
163  }
164 
165  ThreadLogger *pLogger = (ThreadLogger*)pInfo->UserData;
166 
167  while(1)
168  {
169 
170  pLogger->m_WaitMutex.Lock();
171 
172  pInfo->ActiveFlagLock->Lock();
173  int activeFlag = *pInfo->ActiveFlag;
174  pInfo->ActiveFlagLock->Unlock();
175  if( !activeFlag )
176  {
177  break;
178  }
179 
180  pLogger->m_Mutex.Lock();
181  while( !pLogger->m_OperationQ.empty() )
182  {
183  switch( pLogger->m_OperationQ.front() )
184  {
186  pLogger->m_PriorityLevel = pLogger->m_LevelQ.front();
187  pLogger->m_LevelQ.pop();
188  break;
189 
191  pLogger->m_LevelForFlushing = pLogger->m_LevelQ.front();
192  pLogger->m_LevelQ.pop();
193  break;
194 
196  pLogger->m_Output->AddLogOutput(pLogger->m_OutputQ.front());
197  pLogger->m_OutputQ.pop();
198  break;
199 
200  case ThreadLogger::WRITE:
201  pLogger->Logger::Write(pLogger->m_LevelQ.front(), pLogger->m_MessageQ.front());
202  pLogger->m_LevelQ.pop();
203  pLogger->m_MessageQ.pop();
204  break;
205  case ThreadLogger::FLUSH:
206  pLogger->Logger::Flush();
207  break;
208  }
209  pLogger->m_OperationQ.pop();
210  }
211  pLogger->m_Mutex.Unlock();
212  pLogger->m_WaitMutex.Unlock();
213  }
215 }
216 
217 
219 void ThreadLogger::PrintSelf(std::ostream &os, Indent indent) const
220 {
221  Superclass::PrintSelf(os,indent);
222 
223  os << indent << "Thread ID: " << this->m_ThreadID << std::endl;
224  os << indent << "Operation Queue Size: " << this->m_OperationQ.size() << std::endl;
225  os << indent << "Message Queue Size: " << this->m_MessageQ.size() << std::endl;
226  os << indent << "Level Queue Size: " << this->m_LevelQ.size() << std::endl;
227  os << indent << "Output Queue Size: " << this->m_OutputQ.size() << std::endl;
228 }
229 
230 
231 } // namespace itk

Generated at Sun Feb 3 2013 00:09:44 for Orfeo Toolbox with doxygen 1.8.1.1