Merge pull request #2316 from jwillemsen/jwi-taskcommenttypo
[ACE_TAO.git] / ACE / examples / Logger / client / logging_app.cpp
blobb5a7c99b018901e9b6a5e37520304d2c8291653d
1 // This program sends logging records directly to the server, rather
2 // than going through the client logging daemon.
4 #include "ace/SOCK_Connector.h"
5 #include "ace/Log_Record.h"
6 #include "ace/Log_Msg.h"
7 #include "ace/Truncate.h"
8 #include "ace/CDR_Stream.h"
9 #include "ace/OS_NS_time.h"
10 #include "ace/OS_NS_stdio.h"
11 #include "ace/OS_NS_stdlib.h"
12 #include "ace/OS_NS_unistd.h"
14 static u_short LOGGER_PORT = ACE_DEFAULT_SERVER_PORT;
15 static const ACE_TCHAR *const LOGGER_HOST = ACE_DEFAULT_SERVER_HOST;
16 static const int MAX_ITERATIONS = 10;
18 int
19 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
21 const ACE_TCHAR *logger_host = argc > 1 ? argv[1] : LOGGER_HOST;
22 u_short logger_port = argc > 2 ? ACE_OS::atoi (argv[2]) : LOGGER_PORT;
23 int max_iterations = argc > 3 ? ACE_OS::atoi (argv[3]) : MAX_ITERATIONS;
25 ACE_SOCK_Stream logger;
26 ACE_SOCK_Connector connector;
27 ACE_INET_Addr addr (logger_port, logger_host);
29 if (connector.connect (logger, addr) == -1)
30 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("open")), -1);
32 for (int i = 0; i < max_iterations; i++)
34 ACE_Log_Record log_record (LM_DEBUG,
35 ACE_OS::time ((time_t *) 0),
36 ACE_OS::getpid ());
38 ACE_TCHAR buf[BUFSIZ];
39 ACE_OS::sprintf (buf, ACE_TEXT ("message = %d\n"), i + 1);
40 log_record.msg_data (buf);
42 const size_t max_payload_size =
43 4 // type()
44 + 8 // timestamp
45 + 4 // process id
46 + 4 // data length
47 + ACE_Log_Record::MAXLOGMSGLEN // data
48 + ACE_CDR::MAX_ALIGNMENT; // padding;
50 // Insert contents of <log_record> into payload stream.
51 ACE_OutputCDR payload (max_payload_size);
52 payload << log_record;
54 // Get the number of bytes used by the CDR stream.
55 ACE_CDR::ULong length =
56 ACE_Utils::truncate_cast<ACE_CDR::ULong> (payload.total_length ());
58 // Send a header so the receiver can determine the byte order and
59 // size of the incoming CDR stream.
60 ACE_OutputCDR header (ACE_CDR::MAX_ALIGNMENT + 8);
61 header << ACE_OutputCDR::from_boolean (ACE_CDR_BYTE_ORDER);
63 // Store the size of the payload that follows
64 header << ACE_CDR::ULong (length);
66 // Use an iovec to send both buffer and payload simultaneously.
67 iovec iov[2];
68 iov[0].iov_base = header.begin ()->rd_ptr ();
69 iov[0].iov_len = 8;
70 iov[1].iov_base = payload.begin ()->rd_ptr ();
71 iov[1].iov_len = length;
73 if (logger.sendv_n (iov, 2) == -1)
74 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send"), -1);
77 if (logger.close () == -1)
78 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("close")), -1);
80 return 0;