ITK  6.0.0
Insight Toolkit
itkThreadLogger.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkThreadLogger_h
19 #define itkThreadLogger_h
20 
21 #include "itkLogger.h"
22 #include <mutex>
23 #include <atomic>
24 
25 #include <string>
26 #include <queue>
27 #include <thread>
28 
29 namespace itk
30 {
41 class ITKCommon_EXPORT ThreadLogger : public Logger
42 {
43 public:
44  using Self = ThreadLogger;
45  using Superclass = Logger;
48 
50  itkOverrideGetNameOfClassMacro(ThreadLogger);
51 
53  itkNewMacro(Self);
54 
56 
58 
59  using DelayType = unsigned int;
60 
62  typedef enum
63  {
68  FLUSH
69  } OperationType;
70 
74  void
75  SetPriorityLevel(PriorityLevelEnum level) override;
76 
80  PriorityLevelEnum
81  GetPriorityLevel() const override;
82 
83  void
84  SetLevelForFlushing(PriorityLevelEnum level) override;
85 
86  PriorityLevelEnum
87  GetLevelForFlushing() const override;
88 
92  virtual void
93  SetDelay(DelayType delay);
94 
98  virtual DelayType
99  GetDelay() const;
100 
102  void
103  AddLogOutput(OutputType * output) override;
104 
105  void
106  Write(PriorityLevelEnum level, const std::string & content) override;
107 
108  void
109  Flush() override;
110 
111 protected:
113  ThreadLogger();
114 
116  ~ThreadLogger() override;
117 
119  void
120  PrintSelf(std::ostream & os, Indent indent) const override;
121 
122  void
123  ThreadFunction();
124 
125 private:
126  void
127  InternalFlush();
128 
129  std::thread m_Thread{};
130 
131  std::atomic<bool> m_TerminationRequested{};
132 
133  std::queue<OperationType> m_OperationQ{};
134 
135  std::queue<std::string> m_MessageQ{};
136 
137  std::queue<PriorityLevelEnum> m_LevelQ{};
138 
139  std::queue<OutputType::Pointer> m_OutputQ{};
140 
141  mutable std::mutex m_Mutex{};
142 
143  DelayType m_Delay{};
144 
145 }; // class ThreadLogger
146 } // namespace itk
147 
148 #endif // itkThreadLogger_h
itk::LoggerBase::PriorityLevelEnum
LoggerBaseEnums::PriorityLevel PriorityLevelEnum
Definition: itkLoggerBase.h:94
itk::ThreadLogger::SET_LEVEL_FOR_FLUSHING
Definition: itkThreadLogger.h:65
itk::ThreadLogger::ADD_LOG_OUTPUT
Definition: itkThreadLogger.h:66
itk::Logger
Used for logging information during a run.
Definition: itkLogger.h:36
itk::LoggerBase::OutputType
MultipleLogOutput::OutputType OutputType
Definition: itkLoggerBase.h:92
itk::ThreadLogger::PriorityLevelEnum
Logger::PriorityLevelEnum PriorityLevelEnum
Definition: itkThreadLogger.h:57
itk::ThreadLogger::SET_PRIORITY_LEVEL
Definition: itkThreadLogger.h:64
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::LogOutput
Represents an output stream.
Definition: itkLogOutput.h:39
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::ThreadLogger
Providing logging service as a separate thread.
Definition: itkThreadLogger.h:41
itk::LoggerBase
Used for logging information during a run.
Definition: itkLoggerBase.h:81
itk::ThreadLogger::DelayType
unsigned int DelayType
Definition: itkThreadLogger.h:59
itkLogger.h
itk::ThreadLogger::WRITE
Definition: itkThreadLogger.h:67