Loading...
Searching...
No Matches
CommandBankUtils.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32
33// New design (2020)
34
35#ifndef COMMAND_BANK_UTILS_H_
36#define COMMAND_BANK_UTILS_H_
37
38#include <iostream>
39#include <fstream>
40
41#include "CommandBank.h"
42#include "CommandSections.h"
43#include "drain/image/Image.h"
44
45
46namespace drain {
47
48
49// Static
50/*
51class CommandBankUtils : public CommandBank {
52public:
53};
54*/
55
56// Rename CommandUtils?
57
58
59
60
61class CmdVerbosity : public SimpleCommand<int> {
62
63public:
64
65 CmdVerbosity() : SimpleCommand<int>(__FUNCTION__, "Logging verbosity", "level", drain::getLog().getVerbosity()) {
66 };
67
68
69 inline
70 void exec() const {
71 // NEW
72 Context & ctx = getContext<Context>();
73 ctx.log.setVerbosity(value);
74
75 // OLD
76 drain::getLog().setVerbosity(value);
77 drain::image::getImgLog().setVerbosity(value-1);
78 }
79
80};
81
82class CmdDebug : public BasicCommand {
83
84public:
85
86 inline
87 CmdDebug() : BasicCommand(__FUNCTION__, "Set verbosity to LOG_DEBUG") { // TODO
88 };
89
90 inline
91 void exec() const {
92 Context & ctx = getContext<Context>();
93 ctx.log.setVerbosity(LOG_DEBUG);
94 drain::getLog().setVerbosity(LOG_DEBUG);
95 drain::image::getImgLog().setVerbosity(LOG_DEBUG);
96 //r.run("verbose","8"); // FIXME r.setVerbosity();
97 };
98
99};
100
101//class CmdLog : public SimpleCommand<> {
102class CmdLog : public BasicCommand {
103
104public:
105
106 CmdLog(CommandBank & cmdBank);
107
108 CmdLog(const CmdLog & cmd);
109
110 void exec() const;
111
112protected:
113
114 CommandBank & bank;
115
116 std::string filename;
117
118 // For numeric or string keys
119 std::string level;
120
121 bool timing;
122
123};
124
125
126
127
128
130
131public:
132
133 inline
134 CmdStatus() : drain::BasicCommand(__FUNCTION__, "Dump information on current images.") {
135 };
136
137 void exec() const;
138
139};
140
141
148
149public:
150
151 inline
152 CmdExpandVariables() : BasicCommand(__FUNCTION__, "Toggle variable expansions on/off") {
153 };
154
155 inline
156 void exec() const {
157 SmartContext & ctx = getContext<SmartContext>();
158 ctx.expandVariables = !ctx.expandVariables;
159 };
160
161};
162
163
165
169class CmdScript : public SimpleCommand<std::string> {
170
171public:
172
173 inline
174 CmdScript(CommandBank & cmdBank) :
175 SimpleCommand<std::string>(__FUNCTION__, "Define script.", "script"),
176 bank(cmdBank){
177 cmdBank.scriptCmd = getName(); // mark me special
178 };
179
180 inline
181 CmdScript(const CmdScript & cmd) : SimpleCommand<std::string>(cmd), bank(cmd.bank){
182 }
183
184protected:
185
186 // Copy constructor should copy this as well.
187 // FUture versions may store the script in Context?
188 CommandBank & bank;
189
190};
191
192
194
201
202public:
203
204 inline
205 CmdExecScript() :
206 BasicCommand(__FUNCTION__, "Execute script.") {
207 // const drain::Flagger::ivalue_t
208 cmd_section_type TRIGGER = drain::Static::get<drain::TriggerSection>().index;
209 this->section |= TRIGGER;
210 };
211
212
213 inline
214 void exec() const {
215 SmartContext & ctx = getContext<SmartContext>();
216 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
217 mout.debug("Storing script with '" , getName() , "' ." );
218 };
219
220
221};
222
223
224
225
227
231class CmdExecFile : public SimpleCommand<std::string> {
232
233public:
234
235 CmdExecFile(CommandBank & cmdBank) :
236 SimpleCommand<std::string>(__FUNCTION__, "Execute commands from a file.", "filename"),
237 bank(cmdBank){
238 cmdBank.execFileCmd = getName(); // mark me special
239 };
240
241protected:
242
243 // Copy constructor should copy this as well.
244 CommandBank & bank;
245
246};
247
248
250class HelpCmd : public SimpleCommand<std::string> {
251
252public:
253
254
255 HelpCmd(CommandBank & cmdBank) : SimpleCommand<std::string>(__FUNCTION__, "Display help.", "key", "", "command|sections"), cmdBank(cmdBank) {
256 };
257
258 inline
259 void exec() const {
260 if (value.empty()){
261 if (cmdBank.has("general")){
262 cmdBank.help("general");
263 exit(0);
264 }
265 }
266 cmdBank.help(value);
267 // TODO: "see-also" commands as a list, which is checked.
268 exit(0);
269 }
270
271protected:
272
273 // Copy constructor should copy this as well.
274 CommandBank & cmdBank;
275
276};
277
278
279
280
281//template <class C=Context>
282class CmdFormat : public SimpleCommand<std::string> {
283
284public:
285
286 CmdFormat() : SimpleCommand<std::string>(__FUNCTION__,"Set format for data dumps (see --sample or --outputFile)", "format","") { // SimpleCommand<std::string>(getResources().generalCommands, name, alias, "Sets a format std::string.") {
287 };
288
289 inline
290 void exec() const {
291 SmartContext &ctx = getContext<SmartContext>();
292 ctx.formatStr = value;
293 }
294
295
296};
297
298
299
300
301template <class C=Context>
302class CmdFormatFile : public SimpleCommand<std::string> {
303
304public:
305
306
307 CmdFormatFile() : SimpleCommand<>(__FUNCTION__, "Read format for metadata dump from a file","filename","","std::string") {
308 };
309
310 void exec() const;
311
312};
313
314
315template <class C>
317
318 C &ctx = getContext<C>();
319
320 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
321
322 //drain::Input ifstr(value);
323 std::ifstream ifstr;
324 ifstr.open(value.c_str(), std::ios::in);
325 if (ifstr.good()){
326 std::stringstream sstr;
327 sstr << ifstr.rdbuf();
328 /*
329 for (int c = ifstr.get(); !ifstr.eof(); c = ifstr.get()){ // why not getline?
330 sstr << (char)c;
331 }
332 */
333 ifstr.close();
334 //Context &ctx = getContext<>();
335 ctx.formatStr = sstr.str(); // SmartContext ?
336
337 }
338 else
339 mout.error(getName() , ": opening file '" , value , "' failed." );
340
341};
342
343
345class CmdNotFound : public SimpleCommand<> {
346
347public:
348
349 CmdNotFound(CommandBank & cmdBank) :
350 SimpleCommand<std::string>(__FUNCTION__, "Throw exception on unfound ", "cmdArg","")
351 //cmdBank(cmdBank)
352 {
353 section = 0; // hidden
354 cmdBank.notFoundHandlerCmdKey = "notFound"; // getName();
355 };
356
357 void exec() const {
358 Context & ctx = getContext<>();
359
360 drain::Logger mout(ctx.log, __FILE__, __FUNCTION__);
361
362 mout.error("Command '" , value , "' not found." );
364 }
365
366};
367
368
369
370} /* namespace drain */
371
372#endif
bool has(const K &key) const
Check if a cloner is defined for this key.
Definition Bank.h:141
Simple implementation of Command: adds name , description and parameters .
Definition Command.h:429
const std::string & getName() const final
Definition Command.h:354
Definition CommandBankUtils.h:82
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:91
Load script file and execute the commands immediately using current Context.
Definition CommandBankUtils.h:231
Executes the defined script.
Definition CommandBankUtils.h:200
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:214
Definition CommandBankUtils.h:147
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:156
Definition CommandBankUtils.h:302
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:316
Definition CommandBankUtils.h:282
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:290
Definition CommandBankUtils.h:102
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.cpp:52
Special command for handling undefined commands.
Definition CommandBankUtils.h:345
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:357
Define a script.
Definition CommandBankUtils.h:169
Definition CommandBankUtils.h:129
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.cpp:73
Definition CommandBankUtils.h:61
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:70
Container and execution tools for commands derived from Command.
Definition CommandBank.h:56
std::string notFoundHandlerCmdKey
If defined, the command - key not getName() - to which all unresolved commands are directed.
Definition CommandBank.h:230
std::string execFileCmd
Command for reading and executing commands from a file in the current (running) context.
Definition CommandBank.h:273
void help(std::ostream &ostr=std::cout)
Basic help dump, displays help commands to proceed.
Definition CommandBank.cpp:832
std::string scriptCmd
Command for storing a routine.
Definition CommandBank.h:242
int cmd_section_type
Definition Command.h:182
std::string formatStr
Definition Context.h:199
Definition Context.h:57
CommandBank-dependent.
Definition CommandBankUtils.h:250
void exec() const
Run the command with current parameter values.
Definition CommandBankUtils.h:259
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & debug(const TT &... args)
Debug information.
Definition Log.h:666
Logger & error(const TT &... args)
Echoes.
Definition Log.h:416
A single-parameter command.
Definition Command.h:453
Definition Context.h:205
Definition DataSelector.cpp:1277