Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Reactor / Multicast / Log_Wrapper.cpp
blob108463c42d641482b50a9f905b10dbb6e2ae831a
1 // client.C
3 #include "Log_Wrapper.h"
4 #include "ace/Truncate.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/OS_NS_sys_utsname.h"
7 #include "ace/OS_NS_string.h"
8 #include "ace/OS_NS_netdb.h"
9 #include "ace/OS_NS_stdio.h"
10 #include "ace/OS_NS_time.h"
11 #include "ace/OS_NS_stdlib.h"
14 Log_Wrapper::Log_Wrapper ()
16 sequence_number_ = 0;
17 this->log_msg_.app_id = ACE_OS::getpid ();
20 Log_Wrapper::~Log_Wrapper ()
24 // Set the log_msg_ host address.
26 int
27 Log_Wrapper::open (const int port, const char *mcast_addr)
29 struct hostent *host_info;
30 ACE_utsname host_data;
32 if (ACE_OS::uname (&host_data) < 0)
33 return -1;
35 if ((host_info = ACE_OS::gethostbyname (host_data.nodename)) == 0)
36 return -1;
37 else
38 ACE_OS::memcpy ((char *) &this->log_msg_.host,
39 (char *) host_info->h_addr,
40 host_info->h_length);
42 // This starts out initialized to all zeros!
43 server_ = ACE_INET_Addr (port, mcast_addr);
45 if (logger_.join (server_) == -1)
46 ACE_OS::perror("can't join to multicast group"), ACE_OS::exit(1);
48 // success.
49 return 0;
52 // Send the message to a logger object.
53 // This wrapper fills in all the log_record info for you.
54 // uses iovector stuff to make contiguous header and message.
56 int
57 Log_Wrapper::log_message (Log_Priority type, char *message)
59 sequence_number_++;
61 this->log_msg_.type = type;
62 // Casting time() to long will start causing bad results sometime in 2038
63 // but the receiver isn't looking at the time, so who cares?
64 this->log_msg_.time = (long) ACE_OS::time (0);
65 this->log_msg_.msg_length =
66 ACE_Utils::truncate_cast<ACE_INT32> (ACE_OS::strlen (message) + 1);
67 this->log_msg_.sequence_number = ACE_HTONL(sequence_number_);
69 iovec iovp[2];
70 iovp[0].iov_base = reinterpret_cast<char*> (&log_msg_);
71 iovp[0].iov_len = sizeof (log_msg_);
72 iovp[1].iov_base = message;
73 iovp[1].iov_len = log_msg_.msg_length;
75 logger_.send (iovp, 2);
77 // success.
78 return 0;