NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
stats_logger.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 #ifndef LOCALIZATION_COMMON_STATS_LOGGER_H_
19 #define LOCALIZATION_COMMON_STATS_LOGGER_H_
20 
23 
24 #include <vector>
25 
26 namespace localization_common {
27 // Logs various statistics and runtimes to a desired output (command line, CSV file, text file) using averager and timer
28 // objects. Uses references to provided averagers and timers to log min/max/mean/stddev values and runtimes
29 // respectively.
30 class StatsLogger {
31  public:
32  explicit StatsLogger(const bool log_on_destruction = true);
33 
34  ~StatsLogger();
35 
36  // Add averager for logging
38 
39  // Add timer for logging
41 
42  // Log averages and times to command line.
43  void Log() const;
44 
45  // Log averages and times to a text file.
46  void LogToFile(std::ofstream& ofstream) const;
47 
48  // Log averages and times to a CSV file.
49  void LogToCsv(std::ofstream& ofstream) const;
50 
51  std::vector<std::reference_wrapper<localization_common::Timer>> timers_;
52  std::vector<std::reference_wrapper<localization_common::Averager>> averagers_;
53 
54  private:
55  template <typename Logger>
56  void LogToFile(const std::vector<std::reference_wrapper<Logger>>& loggers, std::ofstream& ofstream) const {
57  for (const auto& logger : loggers) logger.get().LogToFile(ofstream);
58  }
59 
60  template <typename Logger>
61  void LogToCsv(const std::vector<std::reference_wrapper<Logger>>& loggers, std::ofstream& ofstream) const {
62  for (const auto& logger : loggers) logger.get().LogToCsv(ofstream);
63  }
64 
65  template <typename Logger>
66  void Log(const std::vector<std::reference_wrapper<Logger>>& loggers) const {
67  for (const auto& logger : loggers) logger.get().Log();
68  }
69 
70  bool log_on_destruction_;
71 };
72 } // namespace localization_common
73 
74 #endif // LOCALIZATION_COMMON_STATS_LOGGER_H_
localization_common
Definition: averager.h:33
localization_common::StatsLogger::~StatsLogger
~StatsLogger()
Definition: stats_logger.cc:23
localization_common::StatsLogger::AddTimer
void AddTimer(localization_common::Timer &timer)
Definition: stats_logger.cc:29
localization_common::Timer
Definition: timer.h:30
localization_common::StatsLogger::timers_
std::vector< std::reference_wrapper< localization_common::Timer > > timers_
Definition: stats_logger.h:51
localization_common::Averager
Definition: averager.h:34
localization_common::StatsLogger::AddAverager
void AddAverager(localization_common::Averager &averager)
Definition: stats_logger.cc:27
localization_common::StatsLogger
Definition: stats_logger.h:30
averager.h
localization_common::StatsLogger::averagers_
std::vector< std::reference_wrapper< localization_common::Averager > > averagers_
Definition: stats_logger.h:52
timer.h
localization_common::StatsLogger::Log
void Log() const
Definition: stats_logger.cc:31
localization_common::StatsLogger::LogToFile
void LogToFile(std::ofstream &ofstream) const
Definition: stats_logger.cc:36
localization_common::StatsLogger::StatsLogger
StatsLogger(const bool log_on_destruction=true)
Definition: stats_logger.cc:21
localization_common::StatsLogger::LogToCsv
void LogToCsv(std::ofstream &ofstream) const
Definition: stats_logger.cc:41