Bayes Filters Library
Logger.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
8 #ifndef LOGGER_H
9 #define LOGGER_H
10 
11 #include <fstream>
12 #include <iostream>
13 #include <string>
14 #include <vector>
15 
16 namespace bfl {
17  class Logger;
18 }
19 
20 
22 {
23 public:
24  virtual ~Logger() noexcept;
25 
26  bool enable_log(const std::string& folder_path, const std::string& file_name_prefix);
27 
28  bool disable_log();
29 
30  std::string get_folder_path() const;
31 
32  std::string get_file_name_prefix() const;
33 
34  template<typename DatumType>
35  void logger(DatumType datum)
36  {
37  if (log_enabled_)
38  log_files_[0] << datum << std::endl;
39  }
40 
41  template<typename... DataType>
42  void logger(DataType... data)
43  {
44  if (log_enabled_)
45  logger_helper(0, data...);
46  }
47 
48  template<typename DatumType>
49  void logger(DatumType datum) const
50  {
51  if (log_enabled_)
52  log_files_[0] << datum << std::endl;
53  }
54 
55  template<typename... DataType>
56  void logger(DataType... data) const
57  {
58  if (log_enabled_)
59  logger_helper(0, data...);
60  }
61 
62 
63 protected:
64  virtual std::vector<std::string> log_file_names(const std::string& folder_path, const std::string& file_name_prefix);
65 
66  virtual void log();
67 
68 
69 private:
70  std::string folder_path_;
71 
72  std::string file_name_prefix_;
73 
74  std::vector<std::string> file_names_;
75 
76  bool log_enabled_ = false;
77 
78  mutable std::vector<std::ofstream> log_files_;
79 
80  template<typename DatumType>
81  void logger_helper(const size_t pos, DatumType datum)
82  {
83  log_files_[pos] << datum << std::endl;
84  }
85 
86  template<typename DatumType, typename... DataType>
87  void logger_helper(const size_t pos, DatumType datum, DataType... data)
88  {
89  log_files_[pos] << datum << std::endl;
90 
91  logger_helper(pos + 1, data...);
92  }
93 
94  template<typename DatumType>
95  void logger_helper(const size_t pos, DatumType datum) const
96  {
97  log_files_[pos] << datum << std::endl;
98  }
99 
100  template<typename DatumType, typename... DataType>
101  void logger_helper(const size_t pos, DatumType datum, DataType... data) const
102  {
103  log_files_[pos] << datum << std::endl;
104 
105  logger_helper(pos + 1, data...);
106  }
107 };
108 
109 #endif /* LOGGER_H */
bfl::Logger::logger
void logger(DataType... data)
Definition: Logger.h:42
bfl::Logger::log_files_
std::vector< std::ofstream > log_files_
Definition: Logger.h:78
bfl::Logger::logger
void logger(DatumType datum)
Definition: Logger.h:35
bfl::Logger::enable_log
bool enable_log(const std::string &folder_path, const std::string &file_name_prefix)
Definition: Logger.cpp:22
bfl::Logger::get_file_name_prefix
std::string get_file_name_prefix() const
Definition: Logger.cpp:91
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
bfl::Logger::get_folder_path
std::string get_folder_path() const
Definition: Logger.cpp:85
bfl::Logger::logger
void logger(DatumType datum) const
Definition: Logger.h:49
bfl::Logger::file_name_prefix_
std::string file_name_prefix_
Definition: Logger.h:72
bfl::Logger::logger_helper
void logger_helper(const size_t pos, DatumType datum) const
Definition: Logger.h:95
bfl::Logger::~Logger
virtual ~Logger() noexcept
Definition: Logger.cpp:15
bfl::Logger
Definition: Logger.h:21
bfl::Logger::log_file_names
virtual std::vector< std::string > log_file_names(const std::string &folder_path, const std::string &file_name_prefix)
Definition: Logger.cpp:97
bfl::Logger::logger_helper
void logger_helper(const size_t pos, DatumType datum, DataType... data)
Definition: Logger.h:87
bfl::Logger::log
virtual void log()
Definition: Logger.cpp:109
bfl::Logger::folder_path_
std::string folder_path_
Definition: Logger.h:70
bfl::Logger::disable_log
bool disable_log()
Definition: Logger.cpp:68
bfl::Logger::logger_helper
void logger_helper(const size_t pos, DatumType datum, DataType... data) const
Definition: Logger.h:101
bfl::Logger::log_enabled_
bool log_enabled_
Definition: Logger.h:76
bfl::Logger::file_names_
std::vector< std::string > file_names_
Definition: Logger.h:74
bfl::Logger::logger
void logger(DataType... data) const
Definition: Logger.h:56
bfl::Logger::logger_helper
void logger_helper(const size_t pos, DatumType datum)
Definition: Logger.h:81