YARP
Yet Another Robot Platform
LogForwarder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
11 #include <yarp/os/Log.h>
12 #include <yarp/os/NetType.h>
13 #include <yarp/os/Network.h>
14 #include <yarp/os/Os.h>
15 #include <yarp/os/SystemInfo.h>
16 #include <yarp/os/Time.h>
18 
19 #include <sstream>
20 
21 bool yarp::os::impl::LogForwarder::started{false};
22 
24 {
25  static LogForwarder instance;
26  return instance;
27 }
28 
30 
31 yarp::os::impl::LogForwarder::LogForwarder()
32 {
33  char hostname[HOST_NAME_MAX];
34  yarp::os::gethostname(hostname, HOST_NAME_MAX);
35 
37 
38  outputPort.setWriteOnly();
39  std::string logPortName = "/log/" + std::string(hostname) + "/" + processInfo.name.substr(processInfo.name.find_last_of("\\/") + 1) + "/" + std::to_string(processInfo.pid);
40  if (!outputPort.open(logPortName)) {
41  printf("LogForwarder error while opening port %s\n", logPortName.c_str());
42  }
43  outputPort.enableBackgroundWrite(true);
44  outputPort.addOutput("/yarplogger", "fast_tcp");
45 
46  started = true;
47 }
48 
49 void yarp::os::impl::LogForwarder::forward(const std::string& message)
50 {
51  mutex.lock();
52  static Bottle b;
53  b.clear();
54  std::string port = "[" + outputPort.getName() + "]";
55  b.addString(port);
56  b.addString(message);
57  outputPort.write(b);
58  mutex.unlock();
59 }
60 
62 {
63  if (started) {
64  std::ostringstream ost;
65  auto systemtime = yarp::os::SystemClock::nowSystem();
66  auto networktime = (!yarp::os::NetworkBase::isNetworkInitialized() ? 0.0 : (yarp::os::Time::isSystemClock() ? systemtime : yarp::os::Time::now()));
67 
68  ost << "(level INFO)";
69  ost << " (systemtime " << yarp::os::NetType::toString(systemtime) << ")";
70  ost << " (networktime " << yarp::os::NetType::toString(networktime) << ")";
71 
72  yarp::os::impl::LogForwarder& fw = getInstance();
73  fw.forward(ost.str());
74  while (fw.outputPort.isWriting()) {
76  }
77  fw.outputPort.interrupt();
78  fw.outputPort.close();
79  }
80 }
yarp::os::Port::close
void close() override
Stop port activity.
Definition: Port.cpp:357
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Bottle::clear
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:124
Network.h
yarp::os::SystemInfo::ProcessInfo::pid
int pid
Definition: SystemInfo.h:121
yarp::os::impl::LogForwarder::getInstance
static LogForwarder & getInstance()
Definition: LogForwarder.cpp:23
yarp::os::impl::LogForwarder::~LogForwarder
~LogForwarder()
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
yarp::os::SystemClock::nowSystem
static double nowSystem()
Definition: SystemClock.cpp:37
PlatformLimits.h
NetType.h
Log.h
yarp::os::SystemInfo::ProcessInfo::name
std::string name
Definition: SystemInfo.h:117
yarp::os::SystemClock::delaySystem
static void delaySystem(double seconds)
Definition: SystemClock.cpp:32
Os.h
yarp::os::SystemInfo::getProcessInfo
static ProcessInfo getProcessInfo(int pid=0)
gets the operating system process information given by its PID.
Definition: SystemInfo.cpp:808
yarp::os::gethostname
void gethostname(char *hostname, size_t size)
Portable wrapper for the gethostname() function.
Definition: Os.cpp:100
yarp::os::impl::LogForwarder
Definition: LogForwarder.h:24
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
yarp::os::NetworkBase::isNetworkInitialized
static bool isNetworkInitialized()
Definition: Network.cpp:879
yarp::os::impl::LogForwarder::forward
void forward(const std::string &message)
Definition: LogForwarder.cpp:49
yarp::os::NetType::toString
static std::string toString(int x)
Definition: NetType.cpp:138
yarp::os::SystemInfo::ProcessInfo
The ProcessInfo struct provides the operating system process information.
Definition: SystemInfo.h:116
yarp::os::Time::isSystemClock
bool isSystemClock()
Check if YARP is providing system time.
Definition: Time.cpp:265
yarp::os::Port::interrupt
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Port.cpp:377
Time.h
LogForwarder.h
yarp::os::impl::LogForwarder::shutdown
static void shutdown()
Definition: LogForwarder.cpp:61
yarp::os::Port::isWriting
bool isWriting() override
Report whether the port is currently writing data.
Definition: Port.cpp:534
SystemInfo.h