OpenDDS  Snapshot(2023/04/28-20:55)
LogAddr.cpp
Go to the documentation of this file.
1 #include "DCPS/DdsDcps_pch.h"
2 
3 #include "LogAddr.h"
4 #include "Definitions.h"
5 #include "SafetyProfileStreams.h"
6 #include "debug.h"
7 
9 
10 namespace OpenDDS {
11 namespace DCPS {
12 
13 const size_t LogAddr::BufSize = AddrToStringSize;
14 
15 const String LogAddr::ip(const ACE_INET_Addr& addr)
16 {
17  char s[BufSize] = {'\0'};
18  return String(addr.get_host_addr(s, BufSize));
19 }
20 
22 {
23  return to_dds_string(addr.get_port_number());
24 }
25 
27 {
28  char s[BufSize] = {'\0'};
29  if (addr.get_host_name(s, BufSize) < 0 && log_level >= LogLevel::Warning) {
30  ACE_ERROR((LM_WARNING, "(%P|%t) WARNING: LogAddr::host: "
31  "could not get hostname: %m\n"));
32  }
33  return String(s);
34 }
35 
37 {
38  init_addr(addr, opt);
39 }
40 
42 {
43  init_addr(addr.to_addr(), opt);
44 }
45 
46 void LogAddr::init_addr(const ACE_INET_Addr& addr, Option opt)
47 {
48  switch (opt) {
49  case IpPort:
50  addr_ = ip(addr) + ':' + port(addr);
51  break;
52  case HostPort:
53  addr_ = host(addr) + ':' + port(addr);
54  break;
55  case Ip:
56  addr_ = ip(addr);
57  break;
58  case Port:
59  addr_ = port(addr);
60  break;
61  case Host:
62  addr_ = host(addr);
63  break;
64  case IpPortHost:
65  addr_ = ip(addr) + ':' + port(addr) + " (" + host(addr) + ')';
66  default:
67  break;
68  }
69 }
70 
71 } // namespace DCPS
72 } // namespace OpenDDS
73 
const char * get_host_addr(char *addr, int addr_size) const
#define ACE_ERROR(X)
LogAddr(const ACE_INET_Addr &addr, Option opt=IpPort)
Definition: LogAddr.cpp:36
std::string String
ACE_INET_Addr to_addr() const
String to_dds_string(unsigned short to_convert)
static const String port(const ACE_INET_Addr &addr)
Definition: LogAddr.cpp:21
int get_host_name(char hostname[], size_t hostnamelen) const
static const String host(const ACE_INET_Addr &addr)
Definition: LogAddr.cpp:26
static const size_t BufSize
Definition: LogAddr.h:23
void init_addr(const ACE_INET_Addr &addr, Option opt)
Definition: LogAddr.cpp:46
LM_WARNING
const size_t AddrToStringSize
Size of TCHAR buffer for use with addr_to_string.
Definition: Definitions.h:149
OpenDDS_Dcps_Export LogLevel log_level
u_short get_port_number(void) const
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
static const String ip(const ACE_INET_Addr &addr)
Definition: LogAddr.cpp:15
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28