ITK  5.2.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  * http://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 
25 #include "itkObjectFactory.h"
26 #include <mutex>
27 #include "ITKCommonExport.h"
28 
29 namespace itk
30 {
31 
37 {
38 public:
43  enum class Operation : uint8_t
44  {
48  WRITE
49  };
50 };
51 // Define how to print enumeration
52 extern ITKCommon_EXPORT std::ostream &
53  operator<<(std::ostream & out, const LoggerThreadWrapperEnums::Operation value);
54 
55 
66 template <typename SimpleLoggerType>
67 class ITK_TEMPLATE_EXPORT LoggerThreadWrapper : public SimpleLoggerType
68 {
69 public:
71  using Superclass = SimpleLoggerType;
74 
76  itkTypeMacro(LoggerThreadWrapper, SimpleLoggerType);
77 
79  itkNewMacro(Self);
80 
81  using OutputType = typename SimpleLoggerType::OutputType;
82  using PriorityLevelEnum = typename SimpleLoggerType::PriorityLevelEnum;
83  using DelayType = unsigned int;
84 
86 
87 #if !defined(ITK_LEGACY_REMOVE)
88  using LoggerThreadWrapperOperationType = OperationEnum;
89 
90  static constexpr OperationEnum SET_PRIORITY_LEVEL = OperationEnum::SET_PRIORITY_LEVEL;
91  static constexpr OperationEnum SET_LEVEL_FOR_FLUSHING = OperationEnum::SET_LEVEL_FOR_FLUSHING;
92  static constexpr OperationEnum ADD_LOG_OUTPUT = OperationEnum::ADD_LOG_OUTPUT;
93  static constexpr OperationEnum WRITE = OperationEnum::WRITE;
94 #endif
95 
99  void
100  SetPriorityLevel(PriorityLevelEnum level) override;
101 
106  GetPriorityLevel() const override;
107 
108  void
109  SetLevelForFlushing(PriorityLevelEnum level) override;
110 
112  GetLevelForFlushing() const override;
113 
117  virtual void
118  SetDelay(DelayType delay);
119 
123  virtual DelayType
124  GetDelay() const;
125 
127  void
128  AddLogOutput(OutputType * output) override;
129 
130  void
131  Write(PriorityLevelEnum level, std::string const & content) override;
132 
133  void
134  Flush() override;
135 
136 protected:
139 
141  ~LoggerThreadWrapper() override;
142 
144  void
145  PrintSelf(std::ostream & os, Indent indent) const override;
146 
147  void
148  ThreadFunction();
149 
150 private:
151  using OperationContainerType = std::queue<OperationEnum>;
152 
153  using MessageContainerType = std::queue<std::string>;
154 
155  using LevelContainerType = std::queue<PriorityLevelEnum>;
156 
157  using OutputContainerType = std::queue<typename OutputType::Pointer>;
158 
159  std::thread m_Thread;
160 
162 
164 
166 
168 
170 
171  mutable std::mutex m_Mutex;
172 
174 
175 }; // class LoggerThreadWrapper
176 
177 } // namespace itk
178 
179 #ifndef ITK_MANUAL_INSTANTIATION
180 # include "itkLoggerThreadWrapper.hxx"
181 #endif
182 
183 #endif // itkLoggerThreadWrapper_h
itk::LoggerThreadWrapperEnums::Operation::WRITE
itk::uint8_t
::uint8_t uint8_t
Definition: itkIntTypes.h:29
itkObjectFactory.h
itk::LoggerThreadWrapper::m_MessageQ
MessageContainerType m_MessageQ
Definition: itkLoggerThreadWrapper.h:165
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:213
itk::LoggerThreadWrapperEnums::Operation::SET_PRIORITY_LEVEL
itk::LoggerThreadWrapper::m_OutputQ
OutputContainerType m_OutputQ
Definition: itkLoggerThreadWrapper.h:169
itk::LoggerThreadWrapper::m_OperationQ
OperationContainerType m_OperationQ
Definition: itkLoggerThreadWrapper.h:163
itk::LoggerThreadWrapper::MessageContainerType
std::queue< std::string > MessageContainerType
Definition: itkLoggerThreadWrapper.h:153
itk::LoggerThreadWrapper::OutputContainerType
std::queue< typename OutputType::Pointer > OutputContainerType
Definition: itkLoggerThreadWrapper.h:157
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LoggerThreadWrapper::OperationContainerType
std::queue< OperationEnum > OperationContainerType
Definition: itkLoggerThreadWrapper.h:151
itk::LoggerThreadWrapperEnums
enums for LoggerThreadWrapper
Definition: itkLoggerThreadWrapper.h:36
itk::LoggerThreadWrapper
Used for providing logging service as a separate thread.
Definition: itkLoggerThreadWrapper.h:67
itk::LoggerThreadWrapper::LevelContainerType
std::queue< PriorityLevelEnum > LevelContainerType
Definition: itkLoggerThreadWrapper.h:155
itk::LoggerThreadWrapper::PriorityLevelEnum
typename SimpleLoggerType::PriorityLevelEnum PriorityLevelEnum
Definition: itkLoggerThreadWrapper.h:82
itk::LoggerThreadWrapper::DelayType
unsigned int DelayType
Definition: itkLoggerThreadWrapper.h:83
itk::LoggerThreadWrapper::m_Delay
DelayType m_Delay
Definition: itkLoggerThreadWrapper.h:173
itk::LoggerThreadWrapperEnums::Operation::SET_LEVEL_FOR_FLUSHING
Operation
itk::LoggerThreadWrapper::OutputType
typename SimpleLoggerType::OutputType OutputType
Definition: itkLoggerThreadWrapper.h:81
itk::LoggerThreadWrapperEnums::Operation
Operation
Definition: itkLoggerThreadWrapper.h:43
itk::LoggerThreadWrapper::m_LevelQ
LevelContainerType m_LevelQ
Definition: itkLoggerThreadWrapper.h:167
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:71
itk::LoggerThreadWrapper::m_Mutex
std::mutex m_Mutex
Definition: itkLoggerThreadWrapper.h:171
itk::LoggerThreadWrapperEnums::Operation::ADD_LOG_OUTPUT
itk::LoggerThreadWrapper::m_Thread
std::thread m_Thread
Definition: itkLoggerThreadWrapper.h:159
itk::LoggerThreadWrapper::m_TerminationRequested
bool m_TerminationRequested
Definition: itkLoggerThreadWrapper.h:161