UdpLogOutput.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
24 
25 #include <netdb.h>
26 #include <boost/regex.hpp>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 
30 #include "icl_core/StringHelper.h"
31 #include "icl_core_config/Config.h"
33 
34 namespace icl_core {
35 namespace logging {
36 
38 
39 LogOutputStream *UdpLogOutput::create(const icl_core::String& name, const icl_core::String& config_prefix,
40  icl_core::logging::LogLevel log_level)
41 {
42  return new UdpLogOutput(name, config_prefix, log_level);
43 }
44 
47  : LogOutputStream(name, config_prefix, log_level),
48  m_socket(-1)
49 {
50  // Get the server configuration.
51  icl_core::String server_host;
52  if (!icl_core::config::get<icl_core::String>(config_prefix + "/Host", server_host))
53  {
54  std::cerr << "No Host specified for UDP log output stream " << config_prefix << std::endl;
55  }
56 
57  icl_core::String server_port =
58  icl_core::config::getDefault<icl_core::String>(config_prefix + "/Port", "60000");
59 
60  if (!icl_core::config::get<icl_core::String>(config_prefix + "/SystemName", m_system_name))
61  {
62  std::cerr << "No SystemName specified for UDP log output stream " << config_prefix << std::endl;
63  }
64 
65  // Open the UDP socket.
66  struct addrinfo hints;
67  memset (&hints, 0, sizeof(hints));
68  hints.ai_family = AF_INET;
69  hints.ai_socktype = SOCK_DGRAM;
70 
71  struct addrinfo *res = 0, *res0 = 0;
72  int n = getaddrinfo(server_host.c_str (), server_port.c_str (), &hints, &res0);
73  if (n == 0)
74  {
75  for (res = res0; res != NULL && m_socket < 0; res = res->ai_next)
76  {
77  m_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
78  if (m_socket >= 0)
79  {
80  if (connect(m_socket, res->ai_addr, res->ai_addrlen) < 0)
81  {
82  close(m_socket);
83  m_socket = -1;
84  }
85  }
86  }
87 
88  freeaddrinfo(res0);
89  }
90 }
91 
93 {
94  if (m_socket >= 0)
95  {
96  close(m_socket);
97  }
98 }
99 
100 void UdpLogOutput::pushImpl(const LogMessage& log_message)
101 {
102  if (m_socket >= 0)
103  {
104  std::stringstream ss;
105  ss << "'" << m_system_name << "',"
106  << "'" << log_message.timestamp.formatIso8601() << "'," << log_message.timestamp.tsNSec() << ","
107  << "'" << logLevelDescription(log_message.log_level) << "',"
108  << "'" << log_message.log_stream << "',"
109  << "'" << log_message.filename << "'," << log_message.line << ","
110  << "'" << log_message.class_name << "',"
111  << "'" << escape(log_message.object_name) << "',"
112  << "'" << log_message.function_name << "',"
113  << "'" << escape(log_message.message_text) << "'";
114  std::string str = ss.str();
115  int res = write(m_socket, str.c_str(), str.length());
116  if (res < 0)
117  {
118  perror("UdpLogOutput::pushImpl()");
119  }
120  }
121 }
122 
124 {
125  // TODO: Which characters have to be escaped to ensure that a
126  // correct SQL statement is created?
127  str = boost::regex_replace(str, boost::regex("'"), "\\'");
128  //str = boost::regex_replace(str, boost::regex("\\n"), "\\\\n");
129  return str;
130 }
131 
132 }
133 }
Contains helper functions for dealing with String.
char filename[cMAX_DESCRIPTION_LENGTH+1]
UdpLogOutput(const icl_core::String &name, const icl_core::String &config_prefix, icl_core::logging::LogLevel log_level)
Base header file for the configuration framework.
uint32_t tsNSec() const
Definition: TimeStamp.h:283
Defines logging macros.
#define REGISTER_LOG_OUTPUT_STREAM(name, factory)
static LogOutputStream * create(const icl_core::String &name, const icl_core::String &config_prefix, icl_core::logging::LogLevel log_level=cDEFAULT_LOG_LEVEL)
Contains icl_logging::UdpLogOutput.
ThreadStream & endl(ThreadStream &stream)
Definition: ThreadStream.h:249
char object_name[cMAX_DESCRIPTION_LENGTH+1]
This is an output stream class for log messages.
virtual void pushImpl(const LogMessage &log_message)
std::string String
Definition: BaseTypes.h:43
char class_name[cMAX_IDENTIFIER_LENGTH+1]
icl_core::String m_system_name
Definition: UdpLogOutput.h:61
void * memset(void *dest, int c, size_t count)
Definition: os_mem.h:46
char function_name[cMAX_IDENTIFIER_LENGTH+1]
char log_stream[cMAX_IDENTIFIER_LENGTH+1]
const char * logLevelDescription(LogLevel log_level)
Definition: LogLevel.cpp:41
Defines an entry for the message queue.
String formatIso8601() const
Definition: TimeStamp.cpp:221
icl_core::String escape(icl_core::String str) const


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58