1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
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 itkResourceProbesCollectorBase_hxx
19 #define itkResourceProbesCollectorBase_hxx
20 
21 #include "itkResourceProbesCollectorBase.h"
22 #include <iostream>
23 
24 namespace itk
25 {
26 
27 template< typename TProbe >
28 void
29 ResourceProbesCollectorBase< TProbe >
Start(const char * id)30 ::Start(const char *id)
31 {
32   // if the probe does not exist yet, it is created.
33   this->m_Probes[id].SetNameOfProbe(id);
34   this->m_Probes[id].Start();
35 }
36 
37 
38 template< typename TProbe >
39 void
40 ResourceProbesCollectorBase< TProbe >
Stop(const char * id)41 ::Stop(const char *id)
42 {
43   IdType tid = id;
44 
45   auto pos = this->m_Probes.find(tid);
46   if ( pos == this->m_Probes.end() )
47     {
48     itkGenericExceptionMacro(<< "The probe \"" << id << "\" does not exist. It can not be stopped.");
49     return;
50     }
51   pos->second.Stop();
52 }
53 
54 
55 template< typename TProbe >
56 const TProbe &
57 ResourceProbesCollectorBase< TProbe >
GetProbe(const char * id) const58 ::GetProbe(const char *id) const
59 {
60   IdType tid = id;
61 
62   auto pos = this->m_Probes.find(tid);
63   if ( pos == this->m_Probes.end() )
64     {
65     itkGenericExceptionMacro(<< "The probe \"" << id << "\" does not exist.");
66     }
67   return pos->second;
68 }
69 
70 
71 template< typename TProbe >
72 void
73 ResourceProbesCollectorBase< TProbe >
Report(std::ostream & os,bool printSystemInfo,bool printReportHead,bool useTabs)74 ::Report(std::ostream & os, bool printSystemInfo, bool printReportHead, bool useTabs)
75 {
76   auto probe = this->m_Probes.begin();
77   typename MapType::const_iterator end   = this->m_Probes.end();
78 
79   if ( probe == end )
80     {
81     os << "No probes have been created" << std::endl;
82     return;
83     }
84 
85   bool firstProbe = true;
86   while ( probe != end )
87     {
88     if(firstProbe)
89       {
90       probe->second.Report(os, printSystemInfo, printReportHead, useTabs);
91       firstProbe = false;
92       }
93     else
94       {
95       probe->second.Report(os, false, false, useTabs);
96       }
97 
98     ++probe;
99     }
100 }
101 
102 
103 template< typename TProbe >
104 void
105 ResourceProbesCollectorBase< TProbe >
Report(const char * name,std::ostream & os,bool printSystemInfo,bool printReportHead,bool useTabs)106 ::Report(const char *name, std::ostream & os, bool printSystemInfo, bool printReportHead, bool useTabs)
107 {
108   const IdType tid = name;
109 
110   auto pos = this->m_Probes.find(tid);
111   if ( pos == this->m_Probes.end() )
112     {
113     os << "The probe \"" << name << "\" does not exist. It's report is not available" <<std::endl;
114     return;
115     }
116 
117   pos->second.Report(os, printSystemInfo, printReportHead, useTabs);
118 }
119 
120 
121 template< typename TProbe >
122 void
123 ResourceProbesCollectorBase< TProbe >
ExpandedReport(std::ostream & os,bool printSystemInfo,bool printReportHead,bool useTabs)124 ::ExpandedReport(std::ostream & os, bool printSystemInfo, bool printReportHead, bool useTabs)
125 {
126   auto probe = this->m_Probes.begin();
127   typename MapType::const_iterator end = this->m_Probes.end();
128 
129   if ( probe == end )
130     {
131     os << "No probes have been created" << std::endl;
132     return;
133     }
134 
135   bool firstProbe = true;
136   while ( probe != end )
137     {
138     if(firstProbe)
139       {
140       probe->second.ExpandedReport(os, printSystemInfo, printReportHead, useTabs);
141       firstProbe = false;
142       }
143     else
144       {
145       probe->second.ExpandedReport(os, false, false, useTabs);
146       }
147 
148     ++probe;
149     }
150 }
151 
152 
153 template< typename TProbe >
154 void
155 ResourceProbesCollectorBase< TProbe >
ExpandedReport(const char * name,std::ostream & os,bool printSystemInfo,bool printReportHead,bool useTabs)156 ::ExpandedReport(const char *name, std::ostream & os, bool printSystemInfo, bool printReportHead, bool useTabs)
157 {
158   const IdType tid = name;
159 
160   auto pos = this->m_Probes.find(tid);
161   if ( pos == this->m_Probes.end() )
162     {
163     os << "The probe \"" << name << "\" does not exist. It's report is not available" <<std::endl;
164     return;
165     }
166 
167   pos->second.ExpandedReport(os, printSystemInfo, printReportHead, useTabs);
168 }
169 
170 
171 template< typename TProbe >
172 void
173 ResourceProbesCollectorBase< TProbe >
JSONReport(std::ostream & os,bool printSystemInfo)174 ::JSONReport(std::ostream & os, bool printSystemInfo)
175 {
176   auto probe = this->m_Probes.begin();
177   typename MapType::const_iterator end = this->m_Probes.end();
178 
179   if ( probe == end )
180     {
181     os << "{ \"Status\": \"No probes have been created\" }" << std::endl;
182     return;
183     }
184 
185   os << "{\n";
186   if (printSystemInfo)
187     {
188     os << "  \"SystemInformation\": ";
189     probe->second.PrintJSONSystemInformation(os);
190     os << ",\n";
191     }
192   os << "  \"Probes\": [\n";
193   bool firstProbe = true;
194   while ( probe != end )
195     {
196     if(firstProbe)
197       {
198       probe->second.JSONReport(os);
199       firstProbe = false;
200       }
201     else
202       {
203       os << ",\n";
204       probe->second.JSONReport(os);
205       }
206 
207     ++probe;
208     }
209   os << "\n  ]\n}" << std::endl;
210 }
211 
212 
213 template< typename TProbe >
214 void
215 ResourceProbesCollectorBase< TProbe >
JSONReport(const char * name,std::ostream & os)216 ::JSONReport(const char *name, std::ostream & os)
217 {
218   const IdType tid = name;
219 
220   auto pos = this->m_Probes.find(tid);
221   if ( pos == this->m_Probes.end() )
222     {
223     os << "  { \"ProbeName\": \"" << name << "\", \"Status\": \"Does not exist!\" }" <<std::endl;
224     return;
225     }
226 
227   pos->second.JSONReport(os);
228 }
229 
230 template< typename TProbe >
231 void
232 ResourceProbesCollectorBase< TProbe >
Clear()233 ::Clear()
234 {
235   this->m_Probes.clear();
236 }
237 
238 
239 } // end namespace itk
240 
241 #endif //itkResourceProbesCollectorBase_hxx
242