YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
LogForwarder.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/Log.h>
9#include <yarp/os/NetType.h>
10#include <yarp/os/Network.h>
11#include <yarp/os/Os.h>
12#include <yarp/os/SystemInfo.h>
13#include <yarp/os/Time.h>
15
17
18#include <sstream>
19
20bool yarp::os::impl::LogForwarder::started{false};
21
26
31
32void yarp::os::impl::ThreadedPort::ThreadedPort::run()
33{
34 size_t size=0;
35 do
36 {
37 mut.lock();
38 size = messages.size();
39 mut.unlock();
40 if (size > 0 && m_port) { process(); }
41 else {break;}
42 } while (1);
43}
44
45void yarp::os::impl::ThreadedPort::process()
46{
47 yarp::os::Bottle& b = m_port->prepare();
48 mut.lock();
49 b = messages.back();
50 messages.pop_back();
51 mut.unlock();
52 m_port->write(true);
53}
54
59
61{
62 this->stop(); // blocking call, wait for the thread to finish
63 m_port = nullptr;
64}
65
67{
68#if 0
69 //if this is enabled, the bottle is sent immediately
70 yarp::os::Bottle& b = m_port->prepare();
71 mut.lock();
72 b = bot;
73 mut.unlock();
74 m_port->write(true);
75#else
76 //if this is enabled, the bottle is sent in a queue and the thread will send it later
77 mut.lock();
78 messages.push_front(bot);
79 mut.unlock();
80#endif
81}
82
84
90
92
93yarp::os::impl::LogForwarder::LogForwarder()
94{
95 char hostname[HOST_NAME_MAX];
97
99 std::string proc_label = yarp::conf::environment::get_string("YARP_LOG_PROCESS_LABEL");
100
101 outputPort.setWriteOnly();
102 std::string logPortName = "/log/" + std::string(hostname) +
103 "/" + processInfo.name.substr(processInfo.name.find_last_of("\\/") + 1);
104
105 if (proc_label!="") { logPortName += "[" + proc_label + "]"; }
106
107 logPortName += "/" + std::to_string(processInfo.pid);
108
109 if (!outputPort.open(logPortName)) {
110 printf("LogForwarder error while opening port %s\n", logPortName.c_str());
111 }
112
113 outputPort.addOutput("/yarplogger", "fast_tcp");
114 tport.attach(&outputPort);
115
116 started = true;
117}
118
120{
121 return outputPort.getName();
122}
123
124void yarp::os::impl::LogForwarder::forward(const std::string& message)
125{
127 b.clear();
128 std::string port = "[" + outputPort.getName() + "]";
129 b.addString(port);
130 b.addString(message);
131 tport.insert(b);
132}
133
135{
136 if (started)
137 {
138 std::ostringstream ost;
139 auto systemtime = yarp::os::SystemClock::nowSystem();
140 auto networktime = (!yarp::os::NetworkBase::isNetworkInitialized() ? 0.0 : (yarp::os::Time::isSystemClock() ? systemtime : yarp::os::Time::now()));
141
142 ost << "(level INFO)";
143 ost << " (systemtime " << yarp::conf::numeric::to_string(systemtime) << ")";
144 ost << " (networktime " << yarp::conf::numeric::to_string(networktime) << ")";
145
146 yarp::os::impl::LogForwarder& fw = getInstance();
147 fw.forward(ost.str());
148 while (fw.outputPort.isWriting()) {
150 }
151
152 fw.tport.terminate();
153 fw.outputPort.interrupt();
154 fw.outputPort.close();
155
156 started = false;
157 }
158}
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition Bottle.cpp:231
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:176
A mini-server for performing network communication in the background.
std::string getName() const override
Get name of port.
void close() override
Stop port activity.
void interrupt() override
Interrupt any current reads or writes attached to the port.
bool isWriting() override
Report whether the port is currently writing data.
static bool isNetworkInitialized()
Definition Network.cpp:964
An abstraction for a periodic thread.
bool start()
Call this to start the thread.
Holds the operating system process information.
static double nowSystem()
static void delaySystem(double seconds)
static ProcessInfo getProcessInfo(int pid=0)
gets the operating system process information given by its PID.
void forward(const std::string &message)
static LogForwarder & getInstance()
void attach(yarp::os::BufferedPort< yarp::os::Bottle > *port)
void insert(const yarp::os::Bottle &bot)
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition environment.h:65
std::string to_string(IntegerType x)
Definition numeric.h:116
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
bool isSystemClock()
Check if YARP is providing system time.
Definition Time.cpp:255
std::string gethostname()
Portable wrapper for the gethostname() function.
Definition Os.cpp:98
The main, catch-all namespace for YARP.
Definition dirs.h:16