YARP
Yet Another Robot Platform
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 
16 #include <sstream>
17 
18 bool yarp::os::impl::LogForwarder::started{false};
19 
21 {
22  static LogForwarder instance;
23  return instance;
24 }
25 
27 
28 yarp::os::impl::LogForwarder::LogForwarder()
29 {
30  char hostname[HOST_NAME_MAX];
31  yarp::os::gethostname(hostname, HOST_NAME_MAX);
32 
34 
35  outputPort.setWriteOnly();
36  std::string logPortName = "/log/" + std::string(hostname) + "/" + processInfo.name.substr(processInfo.name.find_last_of("\\/") + 1) + "/" + std::to_string(processInfo.pid);
37  if (!outputPort.open(logPortName)) {
38  printf("LogForwarder error while opening port %s\n", logPortName.c_str());
39  }
40  outputPort.enableBackgroundWrite(true);
41  outputPort.addOutput("/yarplogger", "fast_tcp");
42 
43  started = true;
44 }
45 
46 void yarp::os::impl::LogForwarder::forward(const std::string& message)
47 {
48  mutex.lock();
49  static Bottle b;
50  b.clear();
51  std::string port = "[" + outputPort.getName() + "]";
52  b.addString(port);
53  b.addString(message);
54  outputPort.write(b);
55  mutex.unlock();
56 }
57 
59 {
60  if (started) {
61  std::ostringstream ost;
62  auto systemtime = yarp::os::SystemClock::nowSystem();
63  auto networktime = (!yarp::os::NetworkBase::isNetworkInitialized() ? 0.0 : (yarp::os::Time::isSystemClock() ? systemtime : yarp::os::Time::now()));
64 
65  ost << "(level INFO)";
66  ost << " (systemtime " << yarp::conf::numeric::to_string(systemtime) << ")";
67  ost << " (networktime " << yarp::conf::numeric::to_string(networktime) << ")";
68 
69  yarp::os::impl::LogForwarder& fw = getInstance();
70  fw.forward(ost.str());
71  while (fw.outputPort.isWriting()) {
73  }
74  fw.outputPort.interrupt();
75  fw.outputPort.close();
76  }
77 }
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
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
static bool isNetworkInitialized()
Definition: Network.cpp:876
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Port.cpp:374
void close() override
Stop port activity.
Definition: Port.cpp:354
bool isWriting() override
Report whether the port is currently writing data.
Definition: Port.cpp:531
static double nowSystem()
Definition: SystemClock.cpp:34
static void delaySystem(double seconds)
Definition: SystemClock.cpp:29
static ProcessInfo getProcessInfo(int pid=0)
gets the operating system process information given by its PID.
Definition: SystemInfo.cpp:805
void forward(const std::string &message)
static LogForwarder & getInstance()
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:262
void gethostname(char *hostname, size_t size)
Portable wrapper for the gethostname() function.
Definition: Os.cpp:97
The ProcessInfo struct provides the operating system process information.
Definition: SystemInfo.h:113