Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / C++NPv1 / Logging_Handler.cpp
blob02f8a78768a01cc0df5df69ff79351775f3b5ddc
1 /*
2 ** Copyright 2001 Addison Wesley. All Rights Reserved.
3 */
5 #include "ace/ACE.h"
6 #include "ace/CDR_Stream.h"
7 #include "ace/INET_Addr.h"
8 #include "ace/Log_Record.h"
9 #include "ace/Truncate.h"
10 #include "ace/Message_Block.h"
11 #include "ace/OS_NS_string.h"
13 #include "Logging_Handler.h"
15 // FUZZ: disable check_for_streams_include
16 #include "ace/streams.h"
18 #include "ace/os_include/os_netdb.h"
20 int Logging_Handler::recv_log_record (ACE_Message_Block *&mblk)
22 // Put <logging_peer>'s hostname in new message block.
23 ACE_INET_Addr peer_addr;
24 logging_peer_.get_remote_addr (peer_addr);
25 mblk = new ACE_Message_Block (MAXHOSTNAMELEN + 1);
26 peer_addr.get_host_name (mblk->wr_ptr (), MAXHOSTNAMELEN);
27 mblk->wr_ptr (ACE_OS::strlen (mblk->wr_ptr ()) + 1); // Go past name
29 // Allocate a message block for the payload; initially at least
30 // large enough to hold the header, but needs some room for
31 // alignment.
32 ACE_Message_Block *payload =
33 new ACE_Message_Block (ACE_DEFAULT_CDR_BUFSIZE);
34 // Align the Message Block for a CDR stream
35 ACE_CDR::mb_align (payload);
36 if (logging_peer_.recv_n (payload->wr_ptr (), 8) == 8) {
37 payload->wr_ptr (8); // Reflect addition of 8 bytes
39 // Create a CDR stream to parse the 8-byte header.
40 ACE_InputCDR cdr (payload);
42 // Extract the byte-order and use helper methods to
43 // disambiguate octet, booleans, and chars.
44 ACE_CDR::Boolean byte_order;
45 cdr >> ACE_InputCDR::to_boolean (byte_order);
47 // Set the byte-order on the stream...
48 cdr.reset_byte_order (byte_order);
50 // Extract the length
51 ACE_CDR::ULong length;
52 cdr >> length;
54 // Ensure there's sufficient room for log record payload.
55 ACE_CDR::grow (payload, 8 + ACE_CDR::MAX_ALIGNMENT + length);
57 // Use <recv_n> to obtain the contents.
58 if (logging_peer_.recv_n (payload->wr_ptr (), length) > 0) {
59 payload->wr_ptr (length); // Reflect additional bytes
60 // Chain the payload to mblk via the contination field.
61 mblk->cont (payload);
62 return length;
65 // Error cases end up here, so we need to release the memory to
66 // prevent a leak.
67 payload->release ();
68 payload = 0;
69 mblk->release ();
70 mblk = 0;
71 return -1;
74 int
75 Logging_Handler::write_log_record (ACE_Message_Block *mblk)
77 // Peer hostname is in the <mblk> and the log record data
78 // is in its continuation.
79 if (log_file_.send_n (mblk) == -1)
80 return -1;
81 if (ACE::debug ()) {
82 // Build a CDR stream from the log record data.
83 ACE_InputCDR cdr (mblk->cont ());
84 ACE_CDR::Boolean byte_order;
85 ACE_CDR::ULong length;
86 // Extract the byte-order and length, ending up at the start
87 // of the log record itself. Use the byte order to properly
88 // set the CDR stream for extracting the contents.
89 cdr >> ACE_InputCDR::to_boolean (byte_order);
90 cdr.reset_byte_order (byte_order);
91 cdr >> length;
92 ACE_Log_Record log_record;
93 cdr >> log_record; // Finally extract the <ACE_log_record>.
94 log_record.print (mblk->rd_ptr (), 1, cerr);
96 return ACE_Utils::truncate_cast<int> (mblk->total_length ());
100 int Logging_Handler::log_record ()
102 ACE_Message_Block *mblk = 0;
103 if (recv_log_record (mblk) == -1)
104 return -1;
105 else {
106 int result = write_log_record (mblk);
107 mblk->release (); // Free up the contents.
108 return result == -1 ? -1 : 0;