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
27
29
30yarp::os::impl::LogForwarder::LogForwarder()
31{
32 char hostname[HOST_NAME_MAX];
34
36 std::string proc_label = yarp::conf::environment::get_string("YARP_LOG_PROCESS_LABEL");
37
38 outputPort.setWriteOnly();
39 std::string logPortName = "/log/" + std::string(hostname) +
40 "/" + processInfo.name.substr(processInfo.name.find_last_of("\\/") + 1);
41
42 if (proc_label!="") { logPortName += "[" + proc_label + "]"; }
43
44 logPortName += "/" + std::to_string(processInfo.pid);
45
46 if (!outputPort.open(logPortName)) {
47 printf("LogForwarder error while opening port %s\n", logPortName.c_str());
48 }
49 outputPort.enableBackgroundWrite(true);
50 outputPort.addOutput("/yarplogger", "fast_tcp");
51
52 started = true;
53}
54
55void yarp::os::impl::LogForwarder::forward(const std::string& message)
56{
57 mutex.lock();
58 static Bottle b;
59 b.clear();
60 std::string port = "[" + outputPort.getName() + "]";
61 b.addString(port);
62 b.addString(message);
63 outputPort.write(b);
64 mutex.unlock();
65}
66
68{
69 if (started) {
70 std::ostringstream ost;
71 auto systemtime = yarp::os::SystemClock::nowSystem();
73
74 ost << "(level INFO)";
75 ost << " (systemtime " << yarp::conf::numeric::to_string(systemtime) << ")";
76 ost << " (networktime " << yarp::conf::numeric::to_string(networktime) << ")";
77
78 yarp::os::impl::LogForwarder& fw = getInstance();
79 fw.forward(ost.str());
80 while (fw.outputPort.isWriting()) {
82 }
83 fw.outputPort.interrupt();
84 fw.outputPort.close();
85 }
86}
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
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.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
static bool isNetworkInitialized()
Definition Network.cpp:876
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()
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 ProcessInfo struct provides the operating system process information.
Definition SystemInfo.h:112