Stride Reference Manual  - generated for commit 9643b11
ControlHelper.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, 2018, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #include "ControlHelper.h"
22 
23 #include "pop/Population.h"
24 #include "sim/SimRunner.h"
25 #include "util/ConfigInfo.h"
26 #include "util/FileSys.h"
27 #include "util/LogUtils.h"
28 #include "util/TimeStamp.h"
29 #include "viewers/CliViewer.h"
33 
34 #include <boost/property_tree/xml_parser.hpp>
35 #include <regex>
36 
37 using namespace std;
38 using namespace stride::util;
39 using namespace boost::property_tree;
40 using namespace boost::property_tree::xml_parser;
41 
42 namespace stride {
43 
44 ControlHelper::ControlHelper()
45  : m_config(), m_name(), m_output_prefix(), m_run_clock("run"), m_stride_logger(nullptr), m_use_install_dirs()
46 
47 {
48 }
49 
51 {
52  Shutdown();
53 }
54 
55 ControlHelper::ControlHelper(const ptree& config, string name) : ControlHelper()
56 {
58  m_config = config;
59  m_name = std::move(name);
60  m_output_prefix = m_config.get<string>("run.output_prefix");
61  m_use_install_dirs = m_config.get<bool>("run.use_install_dirs");
62 }
63 
65 {
66  if (m_use_install_dirs) {
67  auto log = [](const string& s) -> void { cerr << s << endl; };
68  if (!FileSys::CheckInstallEnv(log)) {
69  throw runtime_error("ControlHelper::CheckEnv> Install dirs not OK.");
70  }
71  }
72 }
73 
75 {
78  }
79 }
80 
82 {
83  m_stride_logger = spdlog::get("stride_logger");
84  // If there is no stride_logger yet ...
85  if (!m_stride_logger) {
86  const auto path = FileSys::BuildPath(m_output_prefix, "stride_log.txt");
87  if (m_name == "TestController") {
88  m_stride_logger = LogUtils::CreateFileLogger("stride_logger", path.string());
89  } else {
90  m_stride_logger = LogUtils::CreateCliLogger("stride_logger", path.string());
91  }
92  spdlog::register_logger(m_stride_logger);
93  }
94 
95  // spd log levels are: trace, debug, info, warn, error, critical, off
96  const auto logLevel = m_config.get<string>("run.stride_log_level");
97  m_stride_logger->set_level(spdlog::level::from_str(logLevel));
98  m_stride_logger->flush_on(spdlog::level::err);
99 
100 }
101 
103 {
104  m_run_clock.Stop();
105  m_stride_logger->info("{} shutting down after: {}", m_name, m_run_clock.ToString());
106  m_stride_logger->flush();
107  spdlog::drop("stride_logger");
108 }
109 
111 {
112  m_stride_logger->info("{} starting up at: {}", m_name, TimeStamp().ToString());
113  m_stride_logger->info("Executing revision: {}", ConfigInfo::GitRevision());
114  m_stride_logger->info("Processor count: {}", ConfigInfo::ProcessorCount());
115  m_stride_logger->info("Creating dir: {}", m_output_prefix);
116  m_stride_logger->trace("Executing: {}", FileSys::GetExecPath().string());
117  m_stride_logger->trace("Current directory: {}", FileSys::GetCurrentDir().string());
118  if (m_use_install_dirs) {
119  m_stride_logger->trace("Install directory: {}", FileSys::GetRootDir().string());
120  m_stride_logger->trace("Config directory: {}", FileSys::GetConfigDir().string());
121  m_stride_logger->trace("Data directory: {}", FileSys::GetDataDir().string());
122  }
123  if (ConfigInfo::HaveOpenMP()) {
124  m_stride_logger->info("Max number OpenMP threads in this environment: {}",
126  m_stride_logger->info("Configured number of threads: {}",
127  m_config.get<unsigned int>("run.num_threads"));
128  } else {
129  m_stride_logger->info("Not using OpenMP threads.");
130  }
131  stringstream ss;
132  write_xml(ss, m_config, xml_writer_make_settings<ptree::key_type>(' ', 8));
133  const auto s = ss.str();
134  stringstream spretty;
135  std::regex_replace(std::ostreambuf_iterator<char>(spretty), s.begin(), s.end(), std::regex("(\\n+)"), "\n");
136  m_stride_logger->trace("Config :\n {}", spretty.str());
137 }
138 
139 void ControlHelper::RegisterViewers(shared_ptr<SimRunner> runner)
140 {
141  // Command line viewer
142  m_stride_logger->info("Registering CliViewer");
143  const auto cli_v = make_shared<viewers::CliViewer>(runner, m_stride_logger);
144  runner->Register(cli_v, bind(&viewers::CliViewer::Update, cli_v, placeholders::_1));
145 
146  // Infection counts viewer
147  if (m_config.get<bool>("run.output_cases", false)) {
148  m_stride_logger->info("Registering InfectedFileViewer");
149  const auto v = make_shared<viewers::InfectedFileViewer>(runner, m_output_prefix);
150  runner->Register(v, bind(&viewers::InfectedFileViewer::Update, v, placeholders::_1));
151  }
152 
153  // Persons viewer
154  if (m_config.get<bool>("run.output_persons", false)) {
155  m_stride_logger->info("Registering PersonsFileViewer.");
156  const auto v = make_shared<viewers::PersonsFileViewer>(runner, m_output_prefix);
157  runner->Register(v, bind(&viewers::PersonsFileViewer::Update, v, placeholders::_1));
158  }
159 
160  // Summary viewer
161  if (m_config.get<bool>("run.output_summary", false)) {
162  m_stride_logger->info("Registering SummaryFileViewer");
163  const auto v = make_shared<viewers::SummaryFileViewer>(runner, m_output_prefix);
164  runner->Register(v, bind(&viewers::SummaryFileViewer::Update, v, placeholders::_1));
165  }
166 }
167 
168 } // namespace stride
static unsigned int NumberAvailableThreads()
Return number of threads (in case of OpenMP).
Definition: ConfigInfo.cpp:44
static filesys::path GetDataDir()
/// Return data dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:88
void Update(sim_event::Id id)
Let viewer perform update.
void RegisterViewers(std::shared_ptr< SimRunner > runner)
Register the viewers of the SimRunner.
static filesys::path GetCurrentDir()
Get path to the current directory.
Definition: FileSys.h:69
Stopwatch & Start()
Starts stopwatch if it was stopped.
Definition: Stopwatch.h:50
static filesys::path GetExecPath()
Get path of the executable.
Definition: FileSys.h:72
Observer for SimEvents for commandline interface usage.
void Update(sim_event::Id id)
Let viewer perform update.
Definition: CliViewer.cpp:39
~ControlHelper()
Simple destructor.
boost::property_tree::ptree m_config
Main configuration for run and sim.
Definition: ControlHelper.h:78
std::string m_output_prefix
Prefix to output (name prefix or prefix dir)
Definition: ControlHelper.h:80
void CheckEnv()
Check install environment.
Utilities for the project.
string ToString(Id l)
Converts a LogMode value to corresponding name.
bool m_use_install_dirs
Working dir or install dir mode.
Definition: ControlHelper.h:83
std::shared_ptr< spdlog::logger > m_stride_logger
General logger.
Definition: ControlHelper.h:82
Stopwatch & Stop()
Stops the stopwatch if it was running.
Definition: Stopwatch.h:60
static std::string GitRevision()
Return git revision string.
Definition: ConfigInfo.cpp:33
void LogStartup()
Logs info on setup for cli environment to stride_logger.
Observer for Persons output.
ControlHelper()
Empty controller: used as target for delegation.
Observer for Persons output.
void Update(sim_event::Id id)
Let viewer perform update.
static bool CreateDirectory(std::string s)
Create a directory relative to the current directory with the given path, returns if it was succesful...
Definition: FileSys.cpp:199
static std::shared_ptr< spdlog::logger > CreateCliLogger(const std::string &logger_name, const std::string &file_name)
Return a (not-yet-registered) commandline and file logger, without registering it.
Definition: LogUtils.cpp:32
Provides wall-clock time stamp using the time call.
Definition: TimeStamp.h:34
std::string ToString() const
Returns string representation of readout.
Definition: Stopwatch.h:94
STL namespace.
static filesys::path GetConfigDir()
Return config dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:85
static filesys::path GetRootDir()
Return install root dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:91
static bool CheckInstallEnv(std::function< void(const std::string &)> logger=std::function< void(const std::string &)>())
Verify that current dir is root dir and all install dirs are present.
Definition: FileSys.cpp:65
Logging (spdlog) utilities.
util::Stopwatch m_run_clock
Stopwatch for timing the computation.
Definition: ControlHelper.h:81
static filesys::path BuildPath(const std::string &output_prefix, const std::string &filename)
Interpret prefix (directory or filename prefix) and return appropriate path.
Definition: FileSys.cpp:52
std::string m_name
Contoller&#39;s name.
Definition: ControlHelper.h:79
void Shutdown()
Logs info on setup for cli environment to stride_logger.
Info on configuration.
static unsigned int ProcessorCount()
Return processor count for system.
Definition: ConfigInfo.cpp:42
Observer for Infected output.
Header file for the core Population class.
static std::shared_ptr< spdlog::logger > CreateFileLogger(const std::string &logger_name, const std::string &file_name)
Return a (not-yet-registered) file logger, without registering it.
Definition: LogUtils.cpp:53
static constexpr bool HaveOpenMP()
True if OpenMP in executable, false otherwise.
Definition: ConfigInfo.h:35
static bool IsDirectoryString(const std::string &s)
String represents a directory path (relative or absolute) iff it contains at least one / (may be a tr...
Definition: FileSys.cpp:193
TimeStamp class.
Interface for install directory queries.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
void Update(sim_event::Id id)
Let viewer perform update.
void InstallLogger()
Make the appropriate logger for cli environment and register as stride_logger.
Header for the command line controller.
Header for the SimRunner class.