OTB  9.0.0
Orfeo Toolbox
otbStandardOneLineFilterWatcher.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbStandardOneLineFilterWatcher_hxx
23 #define otbStandardOneLineFilterWatcher_hxx
24 
25 #include <iostream>
26 #include <sstream>
27 
29 #include "otbStopwatch.h"
30 #include "otbSystem.h"
31 
32 namespace otb
33 {
34 template <class PrintCallbackType>
36 {
37  m_DefaultCallback = std::make_shared<PrintCallbackType>();
38  m_Callback = m_DefaultCallback.get();
39 }
40 
41 template <class PrintCallbackType>
43  : FilterWatcherBase(process, comment), m_StarsCount(50), m_CurrentNbStars(-1)
44 {
45  m_DefaultCallback = std::make_shared<PrintCallbackType>();
46  m_Callback = m_DefaultCallback.get();
47 }
48 
49 template <class PrintCallbackType>
50 StandardOneLineFilterWatcher<PrintCallbackType>::StandardOneLineFilterWatcher(itk::ProcessObject* process, const std::string& comment)
51  : FilterWatcherBase(process, comment.c_str()), m_StarsCount(50), m_CurrentNbStars(-1)
52 {
53  m_DefaultCallback = std::make_shared<PrintCallbackType>();
54  m_Callback = m_DefaultCallback.get();
55 }
56 
57 template <class PrintCallbackType>
59 {
60  if (m_Process)
61  {
62  int progressPercent = static_cast<int>(m_Process->GetProgress() * 100);
63  int nbStars = static_cast<int>(m_Process->GetProgress() * m_StarsCount);
64  int nbBlanks = m_StarsCount - nbStars;
65 
66  if (nbBlanks < 0)
67  {
68  nbBlanks = 0;
69  }
70 
71  if (nbStars > m_StarsCount)
72  {
73  nbStars = m_StarsCount;
74  }
75 
76  if (progressPercent > 100)
77  {
78  progressPercent = 100;
79  }
80 
81  if (nbStars > m_CurrentNbStars)
82  {
83  std::string stars(nbStars, '*');
84  std::string blanks(nbBlanks, ' ');
85  std::ostringstream oss;
86  oss << m_Comment << ": " << progressPercent << "% [" << stars << blanks << "]";
87  if (m_Callback->IsInteractive())
88  {
89  m_Callback->Call("\r" + oss.str());
90  m_Callback->Flush();
91  }
92  else
93  {
94  m_Buffer = oss.str();
95  }
96  }
97 
98  m_CurrentNbStars = nbStars;
99  }
100 }
101 
102 template <class PrintCallbackType>
104 {
105  m_Stopwatch.Start();
106 }
107 
108 template <class PrintCallbackType>
110 {
111  m_Stopwatch.Stop();
112 
113  if (m_Process && !m_Callback->IsInteractive())
114  {
115  m_Callback->Call(m_Buffer);
116  m_Buffer = std::string("");
117  }
118 
119  m_Callback->Call(" (" + m_Stopwatch.GetElapsedHumanReadableTime() + ")\n");
120 }
121 
122 } // end namespace otb
123 
124 #endif
otb::StandardOneLineFilterWatcher
This class shows the percentage progress execution of the pipeline filtering process.
Definition: otbStandardOneLineFilterWatcher.h:58
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbSystem.h
otb::FilterWatcherBase
This class is an abstract class Provides an interface to progress task mechanic.
Definition: otbFilterWatcherBase.h:48
otb::StandardOneLineFilterWatcher::StandardOneLineFilterWatcher
StandardOneLineFilterWatcher()
Definition: otbStandardOneLineFilterWatcher.hxx:35
otbStandardOneLineFilterWatcher.h
otbStopwatch.h