YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include <yarp/os/Network.h>
7#include <yarp/os/Port.h>
8#include <yarp/os/Bottle.h>
9#include <yarp/os/Time.h>
10#include <yarp/os/Vocab.h>
11#include <yarp/os/RFModule.h>
13
14#include <string>
15#include <cstdio>
16
17using namespace yarp::os;
18using namespace yarp::yarpLogger;
19
21{
22 LoggerEngine* the_logger = nullptr;
23
24 protected:
26
28 {
29 the_logger = new LoggerEngine ("/logger");
30
31 rpcPort.open("/logger/rpc:i");
33 //attachTerminal();
34
35 the_logger->start_logging();
36 return true;
37 }
38
39 bool interruptModule() override
40 {
42 return true;
43 }
44
45 bool close() override
46 {
48 rpcPort.close();
49
50 if (the_logger)
51 {
52 delete the_logger;
53 the_logger=nullptr;
54 }
55 return true;
56 }
57
58 double getPeriod() override
59 {
60 return 10.0;
61 }
62
63 bool updateModule() override
64 {
65 printf("logger running, listening to %d ports\n",the_logger->get_num_of_processes());
66 return true;
67 }
68
69 bool respond(const yarp::os::Bottle& command,yarp::os::Bottle& reply) override
70 {
71 reply.clear();
72 if (command.get(0).asString()=="quit")
73 {
74 reply.addString("ack");
75 return false;
76 }
77 else if (command.get(0).asString()=="start")
78 {
79 if (the_logger->is_logging()==false)
80 {
81 this->the_logger->start_logging();
82 }
83 reply.addString("ack");
84 }
85 else if (command.get(0).asString()=="stop")
86 {
87 if (the_logger->is_logging()==true)
88 {
89 this->the_logger->stop_logging();
90 }
91 reply.addString("ack");
92 }
93 else if (command.get(0).asString()=="save")
94 {
95 std::string filename = command.get(1).asString();
96 this->the_logger->save_all_logs_to_file(filename);
97 reply.addString("ack");
98 }
99 else if (command.get(0).asString()=="load")
100 {
101 std::string filename = command.get(1).asString();
102 this->the_logger->load_all_logs_from_file(filename);
103 reply.addString("ack");
104 }
105 else if (command.get(0).asString()=="ask_by_proc")
106 {
107 std::string proc_name = command.get(1).asString();
108 std::list<MessageEntry> m;
109 the_logger->get_messages_by_process(proc_name, m);
110 std::list<MessageEntry>::iterator it;
111 for (it = m.begin(); it != m.end(); it++) {
112 printf(" %s %d %s \n", it->yarprun_timestamp.c_str(), it->level.toInt(), it->text.c_str());
113 }
114 reply.addString("ack");
115 }
116 else if (command.get(0).asString()=="ask_all")
117 {
118 std::list<MessageEntry> m;
119 the_logger->get_messages(m);
120 std::list<MessageEntry>::iterator it;
121 for (it = m.begin(); it != m.end(); it++) {
122 printf(" %s %d %s \n", it->yarprun_timestamp.c_str(), it->level.toInt(), it->text.c_str());
123 }
124 reply.addString("ack");
125 }
126 else if (command.get(0).asString()=="discover")
127 {
128 std::list<std::string> ports;
129 the_logger->discover(ports);
130 reply.addString("ack");
131 }
132 else if (command.get(0).asString()=="connect")
133 {
134 std::list<std::string> ports;
135 the_logger->discover(ports);
136 the_logger->connect(ports);
137 reply.addString("ack");
138 }
139 else if (command.get(0).asString()=="get_info")
140 {
141 std::list<LogEntryInfo> infos;
142 the_logger->get_infos(infos);
143 std::list<LogEntryInfo>::iterator it;
144 for (it = infos.begin(); it != infos.end(); it++)
145 {
146 std::tm* tm = localtime(&it->last_update);
147 if (tm) {
148 printf("%s %s hour:%d minute:%d sec:%d \n", it->port_prefix.c_str(), it->port_complete.c_str(), tm->tm_hour, tm->tm_min, tm->tm_sec);
149 } else {
150 printf("%s %s no data received yet \n", it->port_prefix.c_str(), it->port_complete.c_str());
151 }
152 }
153 reply.addString("ack");
154 }
155 else
156 {
157 reply.addString("nack");
158 }
159 return true;
160 }
161};
162
163int main(int argc, char *argv[])
164{
166 if (!yarp.checkNetwork())
167 {
168 fprintf(stderr,"ERROR: check YARP network.\n");
169 return -1;
170 }
171
173 rf.setDefaultConfigFile("yarprunLogger.ini"); //overridden by --from parameter
174 rf.setDefaultContext("yarprunLogger"); //overridden by --context parameter
175 rf.configure(argc,argv);
176
178 return icub_logger.runModule(rf);
179}
bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply) override
Respond to a message.
Definition main.cpp:69
bool close() override
Close function.
Definition main.cpp:45
bool configure(yarp::os::ResourceFinder &rf) override
Configure the module, pass a ResourceFinder object to the module.
Definition main.cpp:27
bool interruptModule() override
Try to halt any ongoing operations by threads managed by the module.
Definition main.cpp:39
bool updateModule() override
Override this to do whatever your module needs to do.
Definition main.cpp:63
yarp::os::Port rpcPort
Definition main.cpp:25
double getPeriod() override
You can override this to control the approximate periodicity at which updateModule() is called by run...
Definition main.cpp:58
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:246
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
A mini-server for performing network communication in the background.
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:706
A mini-server for network communication.
Definition Port.h:46
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition Port.cpp:383
void close() override
Stop port activity.
Definition Port.cpp:363
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
A base-class for standard YARP modules that supports ResourceFinder.
Definition RFModule.h:20
virtual bool attach(yarp::os::Port &source)
Make any input from a Port object go to the respond() method.
Definition RFModule.cpp:456
Helper class for finding config files and other external resources.
bool setDefaultContext(const std::string &contextName)
Sets the context for the current ResourceFinder object.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
bool setDefaultConfigFile(const std::string &fname)
Provide a default value for the configuration file (can be overridden from command line with the –fro...
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
void connect(const std::list< std::string > &ports)
void get_messages(std::list< MessageEntry > &messages)
void get_messages_by_process(std::string process, std::list< MessageEntry > &messages, bool from_beginning=false)
void discover(std::list< std::string > &ports)
bool load_all_logs_from_file(std::string filename)
bool save_all_logs_to_file(std::string filename)
void get_infos(std::list< LogEntryInfo > &infos)
int main(int argc, char *argv[])
Definition main.cpp:163
An interface to the operating system, including Port based communication.
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
The main, catch-all namespace for YARP.
Definition dirs.h:16