2 ** Copyright 2001 Addison Wesley. All Rights Reserved.
5 #include "ace/OS_NS_sys_time.h"
6 #include "ace/CDR_Stream.h"
7 #include "ace/INET_Addr.h"
8 #include "ace/SOCK_Connector.h"
9 #include "ace/SOCK_Stream.h"
10 #include "ace/Log_Msg.h"
11 #include "ace/Log_Record.h"
12 #include "ace/Truncate.h"
13 #include "ace/OS_NS_unistd.h"
14 #include "ace/OS_NS_stdlib.h"
16 // FUZZ: disable check_for_streams_include
17 #include "ace/streams.h"
19 #if defined (ACE_WIN32) && defined (ACE_USES_OLD_IOSTREAMS)
25 class Logging_Client
{
27 ACE_SOCK_Stream logging_peer_
;
30 ACE_SOCK_Stream
&peer () { return logging_peer_
; }
32 //FUZZ: disable check_for_lack_ACE_OS
33 int send (const ACE_Log_Record
&log_record
) {
34 //FUZZ: enable check_for_lack_ACE_OS
36 // Serialize the log record using a CDR stream, allocate
37 // enough space for the complete <ACE_Log_Record>.
38 const size_t max_payload_size
=
43 + ACE_Log_Record::MAXLOGMSGLEN
// data
44 + ACE_CDR::MAX_ALIGNMENT
; // padding;
46 // Insert contents of <log_record> into payload stream.
47 ACE_OutputCDR
payload (max_payload_size
);
48 payload
<< log_record
;
50 // Get the number of bytes used by the CDR stream.
51 ACE_CDR::ULong length
=
52 ACE_Utils::truncate_cast
<ACE_CDR::ULong
> (payload
.total_length ());
54 // Send a header so the receiver can determine the byte
55 // order and size of the incoming CDR stream.
56 ACE_OutputCDR
header (ACE_CDR::MAX_ALIGNMENT
+ 8);
57 header
<< ACE_OutputCDR::from_boolean (ACE_CDR_BYTE_ORDER
);
59 // Store the size of the payload that follows
60 header
<< ACE_CDR::ULong (length
);
61 // Use an iovec to send both buffer and payload simultaneously.
63 iov
[0].iov_base
= header
.begin ()->rd_ptr ();
65 iov
[1].iov_base
= payload
.begin ()->rd_ptr ();
66 iov
[1].iov_len
= length
;
68 // Send header and payload efficiently using "gather-write".
69 return logging_peer_
.sendv_n (iov
, 2);
72 ~Logging_Client () { logging_peer_
.close (); }
76 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
78 u_short logger_port
= argc
> 1 ? ACE_OS::atoi (argv
[1]) : 0;
79 const char *logger_host
=
80 argc
> 2 ? argv
[2] : ACE_DEFAULT_SERVER_HOST
;
83 ACE_INET_Addr server_addr
;
86 result
= server_addr
.set (logger_port
, logger_host
);
88 result
= server_addr
.set ("ace_logger", logger_host
);
90 ACE_ERROR_RETURN ((LM_ERROR
, "lookup %s, %p\n",
91 logger_port
== 0 ? "ace_logger" : argv
[1],
94 ACE_SOCK_Connector connector
;
95 Logging_Client logging_client
;
97 if (connector
.connect (logging_client
.peer (), server_addr
) < 0)
98 ACE_ERROR_RETURN ((LM_ERROR
, "%p\n", "connect()"), 1);
100 #if defined (ACE_WIN32) && defined (ACE_USES_OLD_IOSTREAMS)
102 char user_input
[ACE_Log_Record::MAXLOGMSGLEN
];
103 if (!gets (user_input
))
106 ACE_Time_Value
now (ACE_OS::gettimeofday ());
107 ACE_Log_Record
log_record (LM_INFO
, now
, ACE_OS::getpid ());
108 log_record
.msg_data (user_input
);
109 if (logging_client
.send (log_record
) == -1)
110 ACE_ERROR_RETURN ((LM_ERROR
,
111 "%p\n", "logging_client.send()"), 1);
114 // Limit the number of characters read on each record
115 cin
.width (ACE_Log_Record::MAXLOGMSGLEN
);
118 std::string user_input
;
119 std::getline (cin
, user_input
, '\n');
121 if (!cin
|| cin
.eof ()) break;
123 ACE_Time_Value
now (ACE_OS::gettimeofday ());
124 ACE_Log_Record
log_record (LM_INFO
, now
, ACE_OS::getpid ());
125 log_record
.msg_data (user_input
.c_str ());
126 if (logging_client
.send (log_record
) == -1)
127 ACE_ERROR_RETURN ((LM_ERROR
,
128 "%p\n", "logging_client.send()"), 1);
132 return 0; // Logging_Client destructor closes TCP connection.