ITK  5.4.0
Insight Toolkit
itkLoggerThreadWrapper.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 itkLoggerThreadWrapper_h
19 #define itkLoggerThreadWrapper_h
20 
21 #include <string>
22 #include <queue>
23 #include <thread>
24 #include <atomic>
25 
26 #include "itkObjectFactory.h"
27 #include <mutex>
28 #include "ITKCommonExport.h"
29 
30 namespace itk
31 {
32 
38 {
39 public:
44  enum class Operation : uint8_t
45  {
49  WRITE
50  };
51 };
52 // Define how to print enumeration
53 extern ITKCommon_EXPORT std::ostream &
54  operator<<(std::ostream & out, const LoggerThreadWrapperEnums::Operation value);
55 
56 
67 template <typename SimpleLoggerType>
68 class ITK_TEMPLATE_EXPORT LoggerThreadWrapper : public SimpleLoggerType
69 {
70 public:
72  using Superclass = SimpleLoggerType;
75 
77  itkOverrideGetNameOfClassMacro(LoggerThreadWrapper);
78 
80  itkNewMacro(Self);
81 
82  using OutputType = typename SimpleLoggerType::OutputType;
83  using PriorityLevelEnum = typename SimpleLoggerType::PriorityLevelEnum;
84  using DelayType = unsigned int;
85 
87 
88 #if !defined(ITK_LEGACY_REMOVE)
89  using LoggerThreadWrapperOperationType = OperationEnum;
90 
91  static constexpr OperationEnum SET_PRIORITY_LEVEL = OperationEnum::SET_PRIORITY_LEVEL;
92  static constexpr OperationEnum SET_LEVEL_FOR_FLUSHING = OperationEnum::SET_LEVEL_FOR_FLUSHING;
93  static constexpr OperationEnum ADD_LOG_OUTPUT = OperationEnum::ADD_LOG_OUTPUT;
94  static constexpr OperationEnum WRITE = OperationEnum::WRITE;
95 #endif
96 
100  void
101  SetPriorityLevel(PriorityLevelEnum level) override;
102 
107  GetPriorityLevel() const override;
108 
109  void
110  SetLevelForFlushing(PriorityLevelEnum level) override;
111 
113  GetLevelForFlushing() const override;
114 
118  virtual void
119  SetDelay(DelayType delay);
120 
124  virtual DelayType
125  GetDelay() const;
126 
128  void
129  AddLogOutput(OutputType * output) override;
130 
131  void
132  Write(PriorityLevelEnum level, std::string const & content) override;
133 
134  void
135  Flush() override;
136 
137 protected:
138  void
139  PrivateFlush() override;
140 
143 
145  ~LoggerThreadWrapper() override;
146 
148  void
149  PrintSelf(std::ostream & os, Indent indent) const override;
150 
151  void
152  ThreadFunction();
153 
154 private:
155  std::thread m_Thread{};
156 
157  std::atomic<bool> m_TerminationRequested{};
158 
159  std::queue<OperationEnum> m_OperationQ{};
160 
161  std::queue<std::string> m_MessageQ{};
162 
163  std::queue<PriorityLevelEnum> m_LevelQ{};
164 
165  std::queue<typename OutputType::Pointer> m_OutputQ{};
166 
167  mutable std::mutex m_Mutex{};
168 
169  DelayType m_Delay{};
170 
171 }; // class LoggerThreadWrapper
172 
173 } // namespace itk
174 
175 #ifndef ITK_MANUAL_INSTANTIATION
176 # include "itkLoggerThreadWrapper.hxx"
177 #endif
178 
179 #endif // itkLoggerThreadWrapper_h
itk::LoggerThreadWrapperEnums::Operation::WRITE
itkObjectFactory.h
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::LoggerThreadWrapperEnums::Operation::SET_PRIORITY_LEVEL
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LoggerThreadWrapperEnums
enums for LoggerThreadWrapper
Definition: itkLoggerThreadWrapper.h:37
itk::LoggerThreadWrapper
Used for providing logging service as a separate thread.
Definition: itkLoggerThreadWrapper.h:68
itk::LoggerThreadWrapper::PriorityLevelEnum
typename SimpleLoggerType::PriorityLevelEnum PriorityLevelEnum
Definition: itkLoggerThreadWrapper.h:83
itk::LoggerThreadWrapper::DelayType
unsigned int DelayType
Definition: itkLoggerThreadWrapper.h:84
itk::LoggerThreadWrapperEnums::Operation::SET_LEVEL_FOR_FLUSHING
Operation
itk::LoggerThreadWrapper::OutputType
typename SimpleLoggerType::OutputType OutputType
Definition: itkLoggerThreadWrapper.h:82
itk::LoggerThreadWrapperEnums::Operation
Operation
Definition: itkLoggerThreadWrapper.h:44
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::LoggerThreadWrapper::Superclass
SimpleLoggerType Superclass
Definition: itkLoggerThreadWrapper.h:72
itk::LoggerThreadWrapperEnums::Operation::ADD_LOG_OUTPUT