Bayes Filters Library
Logger.cpp
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 #include <BayesFilters/Logger.h>
9 
10 #include <iostream>
11 
12 using namespace bfl;
13 
14 
15 Logger::~Logger() noexcept
16 {
17  if (log_enabled_)
18  disable_log();
19 }
20 
21 
22 bool Logger::enable_log(const std::string& folder_path, const std::string& file_name_prefix)
23 {
24  if (!log_enabled_)
25  {
26  const std::vector<std::string>& file_names = log_file_names(folder_path, file_name_prefix);
27 
28  if (file_names.size() == 0)
29  {
30  std::cerr << "WARNING::LOGGER::ENABLELOG\n";
31  std::cerr << "\tWARNING: Log facility could not be started due to missing file names." << std::endl;
32 
33  return false;
34  }
35 
36  folder_path_ = folder_path;
37  file_name_prefix_ = file_name_prefix;
38 
39  auto file_name = file_names.begin();
40  for (size_t i = 0; i < file_names.size(); ++i, ++file_name)
41  {
42  log_files_.emplace_back(std::ofstream(*file_name + ".txt", std::ofstream::out | std::ofstream::app));
43 
44  if (!log_files_[i].is_open())
45  {
46  std::cerr << "WARNING::LOGGER::ENABLELOG\n";
47  std::cerr << "\tWARNING: Log facility could not be started for file " + *file_name + ".txt." << std::endl;
48 
49  log_files_.clear();
50 
51  file_names_.clear();
52 
53  return false;
54  }
55 
56  file_names_.emplace_back(*file_name);
57  }
58 
59  log_enabled_ = true;
60 
61  return true;
62  }
63  else
64  return false;
65 }
66 
67 
69 {
70  if (log_enabled_)
71  {
72  log_files_.clear();
73 
74  file_names_.clear();
75 
76  log_enabled_ = false;
77 
78  return true;
79  }
80  else
81  return false;
82 }
83 
84 
85 std::string Logger::get_folder_path() const
86 {
87  return folder_path_;
88 }
89 
90 
91 std::string Logger::get_file_name_prefix() const
92 {
93  return file_name_prefix_;
94 }
95 
96 
97 std::vector<std::string> Logger::log_file_names(const std::string& folder_path, const std::string& file_name_prefix)
98 {
99  static_cast<void>(folder_path);
100  static_cast<void>(file_name_prefix);
101 
102  std::cerr << "WARNING::LOGGER::LOG_FILENAMES\n";
103  std::cerr << "\tWARNING: Log file names where not provided. Did you override `log_file_names()`?" << std::endl;
104 
105  return {};
106 }
107 
108 
110 { }
bfl::Logger::log_files_
std::vector< std::ofstream > log_files_
Definition: Logger.h:78
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::file_name_prefix_
std::string file_name_prefix_
Definition: Logger.h:72
bfl::Logger::~Logger
virtual ~Logger() noexcept
Definition: Logger.cpp:15
Logger.h
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::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::log_enabled_
bool log_enabled_
Definition: Logger.h:76
bfl::Logger::file_names_
std::vector< std::string > file_names_
Definition: Logger.h:74