Debian Codegraph @

netsvcs/clients/Logger/direct_logging.cpp

raw symbols SCIP
1 // This program sends logging records directly to the server, rather
2 // than going through the client logging daemon.
3
4 #include "ace/SOCK_Connector.h"
5 #include "ace/Log_Record.h"
6 #include "ace/Log_Msg.h"
7 #include "ace/OS_NS_time.h"
8 #include "ace/OS_NS_stdlib.h"
9 #include "ace/OS_NS_unistd.h"
10 #include "ace/CDR_Stream.h"
11
12 static u_short LOGGER_PORT = ACE_DEFAULT_SERVER_PORT;
13 static const ACE_TCHAR *const LOGGER_HOST = ACE_DEFAULT_SERVER_HOST;
14 static const ACE_TCHAR *const DATA = ACE_TEXT ("hello world\n");
15
16 int
17 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
18 {
19 u_short logger_port = argc > 1 ? ACE_OS::atoi (argv[1]) : LOGGER_PORT;
20 const ACE_TCHAR *logger_host = argc > 2 ? argv[2] : LOGGER_HOST;
21
22 ACE_SOCK_Stream logger;
23 ACE_SOCK_Connector connector;
24 ACE_INET_Addr addr (logger_port, logger_host);
25 ACE_Log_Record log_record (LM_DEBUG,
26 ACE_OS::time ((time_t *) 0),
27 ACE_OS::getpid ());
28
29 if (connector.connect (logger, addr) == -1)
30 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), -1);
31
32 log_record.msg_data (DATA);
33 const size_t max_payload_size =
34 4 // type()
35 + 8 // timestamp
36 + 4 // process id
37 + 4 // data length
38 + ACE_Log_Record::MAXLOGMSGLEN // data
39 + ACE_CDR::MAX_ALIGNMENT; // padding;
40
41 // Insert contents of <log_record> into payload stream.
42 ACE_OutputCDR payload (max_payload_size);
43 payload << log_record;
44
45 // Get the number of bytes used by the CDR stream.
46 ACE_CDR::ULong length = payload.total_length ();
47
48 // Send a header so the receiver can determine the byte order and
49 // size of the incoming CDR stream.
50 ACE_OutputCDR header (ACE_CDR::MAX_ALIGNMENT + 8);
51 header << ACE_OutputCDR::from_boolean (ACE_CDR_BYTE_ORDER);
52
53 // Store the size of the payload that follows
54 header << ACE_CDR::ULong (length);
55
56 // Use an iovec to send both buffer and payload simultaneously.
57 iovec iov[2];
58 iov[0].iov_base = header.begin ()->rd_ptr ();
59 iov[0].iov_len = 8;
60 iov[1].iov_base = payload.begin ()->rd_ptr ();
61 iov[1].iov_len = length;
62
63 if (logger.sendv_n (iov, 2) == -1)
64 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send"), -1);
65 else if (logger.close () == -1)
66 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "close"), -1);
67
68 #if defined (ACE_WIN32)
69 // !!Important, Winsock is broken in that if you don't close
70 // down the connection before exiting main, you'll lose data.
71 // More over, your server might get "Access Violation" from
72 // within Winsock functions.
73
74 // Here we close down the connection to Logger by redirecting
75 // the logging destination back to stderr.
76 ACE_LOG_MSG->open (0, ACE_Log_Msg::STDERR, 0);
77 #endif /* ACE_WIN32 */
78
79 return 0;
80 }