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
27void yarp::os::impl::ThreadedPort::ThreadedPort::run()
28{
29 size_t size=0;
30 do
31 {
32 mut.lock();
33 size = messages.size();
34 mut.unlock();
35 if (size > 0 && m_port) { process(); }
36 else {break;}
37 } while (1);
38}
39
40void yarp::os::impl::ThreadedPort::process()
41{
42 yarp::os::Bottle& b = m_port->prepare();
43 mut.lock();
44 b = messages.back();
45 messages.pop_back();
46 mut.unlock();
47 m_port->write(true);
48}
49
54
56{
57 this->stop(); // blocking call, wait for the thread to finish
58 m_port = nullptr;
59}
60
62{
63#if 0
64 //if this is enabled, the bottle is sent immediately
65 yarp::os::Bottle& b = m_port->prepare();
66 mut.lock();
67 b = bot;
68 mut.unlock();
69 m_port->write(true);
70#else
71 //if this is enabled, the bottle is sent in a queue and the thread will send it later
72 mut.lock();
73 messages.push_front(bot);
74 mut.unlock();
75#endif
76}
77
79
85
87
88yarp::os::impl::LogForwarder::LogForwarder()
89{
90 char hostname[HOST_NAME_MAX];
92
94 std::string proc_label = yarp::conf::environment::get_string("YARP_LOG_PROCESS_LABEL");
95
96 outputPort.setWriteOnly();
97 std::string logPortName = "/log/" + std::string(hostname) +
98 "/" + processInfo.name.substr(processInfo.name.find_last_of("\\/") + 1);
99
100 if (proc_label!="") { logPortName += "[" + proc_label + "]"; }
101
102 logPortName += "/" + std::to_string(processInfo.pid);
103
104 if (!outputPort.open(logPortName)) {
105 printf("LogForwarder error while opening port %s\n", logPortName.c_str());
106 }
107
108 outputPort.addOutput("/yarplogger", "fast_tcp");
109 tport.attach(&outputPort);
110
111 started = true;
112}
113
115{
116 return outputPort.getName();
117}
118
119void yarp::os::impl::LogForwarder::forward(const std::string& message)
120{
122 b.clear();
123 std::string port = "[" + outputPort.getName() + "]";
124 b.addString(port);
125 b.addString(message);
126 tport.insert(b);
127}
128
130{
131 if (started)
132 {
133 std::ostringstream ost;
134 auto systemtime = yarp::os::SystemClock::nowSystem();
135 auto networktime = (!yarp::os::NetworkBase::isNetworkInitialized() ? 0.0 : (yarp::os::Time::isSystemClock() ? systemtime : yarp::os::Time::now()));
136
137 ost << "(level INFO)";
138 ost << " (systemtime " << yarp::conf::numeric::to_string(systemtime) << ")";
139 ost << " (networktime " << yarp::conf::numeric::to_string(networktime) << ")";
140
141 yarp::os::impl::LogForwarder& fw = getInstance();
142 fw.forward(ost.str());
143 while (fw.outputPort.isWriting()) {
145 }
146
147 fw.tport.terminate();
148 fw.outputPort.interrupt();
149 fw.outputPort.close();
150
151 started = false;
152 }
153}
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:230
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.
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:876
An abstraction for a periodic thread.
bool start()
Call this to start the thread.
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:66
std::string to_string(IntegerType x)
Definition numeric.h:115
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
The ProcessInfo struct provides the operating system process information.
Definition SystemInfo.h:112