JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
Manager.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18// This code is inspired by the Neuromorphic Robotics Toolkit (http://nrtkit.org)
19
22#include <jevois/Debug/Log.H>
23#include <unordered_map>
24#include <fstream>
25
26// ######################################################################
27jevois::Manager::Manager(std::string const & instanceID) :
28 jevois::Component(instanceID), itsGotArgs(false)
29{ }
30
31// ######################################################################
32jevois::Manager::Manager(int argc, char const* argv[], std::string const & instanceID) :
33 jevois::Component(instanceID), itsCommandLineArgs((char const **)(argv), (char const **)(argv+argc)),
34 itsGotArgs(true)
35{ }
36
37// ######################################################################
38void jevois::Manager::setCommandLineArgs(int argc, char const* argv[])
39{
40 itsCommandLineArgs = std::vector<std::string>((char const **)(argv), (char const **)(argv+argc));
41 itsGotArgs = true;
42}
43
44// ######################################################################
47
48// ######################################################################
50{
51 if (itsGotArgs == false)
52 LERROR("No command-line arguments given; did you forget to call jevois::Manager::setArgs()?");
53
54 if (itsCommandLineArgs.size() > 0) itsRemainingArgs = parseCommandLine(itsCommandLineArgs);
55}
56
57// ######################################################################
59{
60 // Note: exit() tries to uninit which can deadlock since we are in init() here...
61 //if (help::get()) { printHelpMessage(); std::abort(); /*exit(0);*/ } // yes this is brutal!
62
63 // BEGIN_JEVOIS_CODE_SNIPPET manager4.C
64
65 if (help::get()) { printHelpMessage(); LINFO("JeVois: exit after help message"); exit(0); }
66
67 // The --help parameter is only useful for parsing of command-line arguments. After that is done, we here hide it as
68 // we will instead provide a 'help' command:
69 help::freeze(true);
70
71 // END_JEVOIS_CODE_SNIPPET
72
73 // Do not confuse users with a non-working tracelevel parameter if tracing has not been compiled in:
74#if !defined(JEVOIS_TRACE_ENABLE) || !defined(JEVOIS_LDEBUG_ENABLE)
75 tracelevel::freeze(true);
76#endif
77}
78
79// ######################################################################
81{
82 constructHelpMessage(std::cout);
83}
84
85// ######################################################################
86void jevois::Manager::constructHelpMessage(std::ostream & out) const
87{
88 std::unordered_map<std::string, // category:description
89 std::unordered_map<std::string, // --name (type) default=[def]
90 std::vector<std::pair<std::string, // component name
91 std::string // current param value
92 > > > > helplist;
93 // First our own options, excluding our subs:
94 this->populateHelpMessage("", helplist, false);
95
96 // Then all our components/modules, we call them directly instead of just recursing down from us so that the manager
97 // name is omitted from all descriptors:
98 {
99 boost::shared_lock<boost::shared_mutex> lck(itsSubMtx);
100 for (std::shared_ptr<jevois::Component> c : itsSubComponents) c->populateHelpMessage("", helplist);
101 }
102
103 // Helplist should never be empty since the Manager has options, but in any case...
104 if (helplist.empty()) { out << "NO PARAMETERS."; return; }
105
106 out << "PARAMETERS:" << std::endl << std::endl;
107
108 for (auto & c : helplist)
109 {
110 // Print out the category name and description
111 out << c.first << std::endl;
112
113 // Print out the parameter details
114 for (auto const & n : c.second)
115 {
116 out << n.first << std::endl;
117
118 // Print out the name of each component that exports this parameter definition, but strip the manager's name for
119 // brevity, unless that's the only thing in the descriptor:
120 out << " Exported By: ";
121 for (auto const & cp : n.second) // pair: <component, value>
122 {
123 out << cp.first; // component descriptor
124 if (cp.second.empty() == false) out << " value=[" << cp.second << ']'; // value
125 if (cp != *(n.second.end()-1)) out << ", ";
126 }
127
128 out << std::endl;
129 out << std::endl;
130 }
131 out << std::endl;
132 }
133 out << std::flush;
134}
135
136// ######################################################################
137std::vector<std::string> const jevois::Manager::parseCommandLine(std::vector<std::string> const & commandLineArgs)
138{
139 // Start by pushing the program name into remaining args
140 std::vector<std::string> remainingArgs;
141 remainingArgs.push_back(commandLineArgs[0]);
142
143 // process all the -- args, push other things into remaining args:
144 std::vector<std::string>::const_iterator argIt;
145 for (argIt = commandLineArgs.begin() + 1; argIt != commandLineArgs.end(); ++argIt)
146 {
147 // All arguments should start with "--", store as remaining arg anything that does not:
148 if (argIt->length() < 2 || (*argIt)[0] != '-' || (*argIt)[1] != '-') { remainingArgs.push_back(*argIt); continue; }
149
150 // If the argument is just a lone "--", then we are done with command line parsing:
151 if (*argIt == "--") break;
152
153 // Split the string by "=" to separate the parameter name from the value
154 size_t const equalsPos = argIt->find_first_of('=');
155 if (equalsPos < 3) LFATAL("Cannot parse command-line argument with no name [" << *argIt << ']');
156
157 std::string const parameterName = argIt->substr(2, equalsPos - 2);
158 std::string const parameterValue = (equalsPos == std::string::npos) ? "true" : argIt->substr(equalsPos + 1);
159
160 // Set the parameter recursively, will throw if not found, and here we allow multiple matches and set all matching
161 // parameters to the given value:
162 setParamString(parameterName, parameterValue);
163 }
164
165 // Add anything after a lone -- to the remaining args:
166 while (argIt != commandLineArgs.end()) { remainingArgs.push_back(*argIt); ++argIt; }
167
168 return remainingArgs;
169}
170
171// ######################################################################
172std::vector<std::string> const & jevois::Manager::remainingArgs() const
173{ return itsRemainingArgs; }
174
175// ######################################################################
176void jevois::Manager::removeComponent(std::string const & instance, bool warnIfNotFound)
177{
178 // Keep this code in sync with Componnet::removeSubComponent
179
180 boost::upgrade_lock<boost::shared_mutex> uplck(itsSubMtx);
181
182 for (auto itr = itsSubComponents.begin(); itr != itsSubComponents.end(); ++itr)
183 if ((*itr)->instanceName() == instance)
184 {
185 doRemoveSubComponent(itr, uplck, "Component");
186 return;
187 }
188
189 if (warnIfNotFound) LERROR("Component [" << instance << "] not found. Ignored.");
190}
191// BEGIN_JEVOIS_CODE_SNIPPET manager3.C
192
193// ######################################################################
194void jevois::Manager::onParamChange(jevois::manager::loglevel const &, jevois::manager::LogLevel const & newval)
195{
196 switch(newval)
197 {
198 case jevois::manager::LogLevel::fatal: jevois::logLevel = LOG_CRIT; break;
199 case jevois::manager::LogLevel::error: jevois::logLevel = LOG_ERR; break;
200 case jevois::manager::LogLevel::info: jevois::logLevel = LOG_INFO; break;
201#ifdef JEVOIS_LDEBUG_ENABLE
202 case jevois::manager::LogLevel::debug: jevois::logLevel = LOG_DEBUG; break;
203#endif
204 }
205}
206
207// ######################################################################
208void jevois::Manager::onParamChange(jevois::manager::tracelevel const &, unsigned int const & newval)
209{
210#if !defined(JEVOIS_TRACE_ENABLE) || !defined(JEVOIS_LDEBUG_ENABLE)
211 if (newval)
212 LERROR("Debug trace has been disabled at compile-time, re-compile with -DJEVOIS_LDEBUG_ENABLE=ON and "
213 "-DJEVOIS_TRACE_ENABLE=ON to see trace info");
214#endif
215
216 jevois::traceLevel = newval;
217}
218
219// END_JEVOIS_CODE_SNIPPET
A component of a model hierarchy.
Definition Component.H:182
friend class Manager
Definition Component.H:513
void setCommandLineArgs(int argc, char const *argv[])
Set the command-line arguments, call this before start() if args were not passed at construction.
Definition Manager.C:38
void removeComponent(std::shared_ptr< Comp > &component)
Remove a top-level Component from the Manager, by shared_ptr.
void onParamChange(manager::loglevel const &param, manager::LogLevel const &newval) override
Parameter callback.
void postInit() override
Checks for the –help flag.
Definition Manager.C:58
virtual ~Manager()
Destructor.
Definition Manager.C:45
void constructHelpMessage(std::ostream &out) const
Constructs a help message from all parameters in the model, and outputs it to 'out'.
Definition Manager.C:86
std::vector< std::string > const & remainingArgs() const
Get the remaining arguments that were not parsed by the command line.
Definition Manager.C:172
void printHelpMessage() const
Constructs a help message and tries to send it to /usr/bin/less.
Definition Manager.C:80
void preInit() override
Calls parseCommandLine()
Definition Manager.C:49
int logLevel
Current log level.
Definition Log.C:29
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
int traceLevel
Current trace level.
Definition Log.C:30
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition Log.H:211
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition Log.H:194
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2