00001 // $Id: Log_Msg_IPC.cpp 80826 2008-03-04 14:51:23Z wotte $ 00002 00003 #include "ace/Log_Msg_IPC.h" 00004 #include "ace/Log_Record.h" 00005 #include "ace/CDR_Stream.h" 00006 #include "ace/Truncate.h" 00007 00008 ACE_RCSID(ace, Log_Msg_IPC, "$Id: Log_Msg_IPC.cpp 80826 2008-03-04 14:51:23Z wotte $") 00009 00010 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00011 00012 ACE_Log_Msg_IPC::ACE_Log_Msg_IPC (void) 00013 { 00014 } 00015 00016 ACE_Log_Msg_IPC::~ACE_Log_Msg_IPC (void) 00017 { 00018 (void) this->close (); 00019 } 00020 00021 int 00022 ACE_Log_Msg_IPC::open (const ACE_TCHAR *logger_key) 00023 { 00024 ACE_LOG_MSG_IPC_CONNECTOR con; 00025 return con.connect (this->message_queue_, 00026 ACE_LOG_MSG_IPC_ADDR (logger_key)); 00027 } 00028 00029 int 00030 ACE_Log_Msg_IPC::reset (void) 00031 { 00032 if (this->message_queue_.get_handle () != ACE_INVALID_HANDLE) 00033 { 00034 // If we don't do this, handles aren't reused on Win32 and the 00035 // server eventually crashes! 00036 return this->close (); 00037 } 00038 return 0; 00039 } 00040 00041 int 00042 ACE_Log_Msg_IPC::close (void) 00043 { 00044 return this->message_queue_.close (); 00045 } 00046 00047 ssize_t 00048 ACE_Log_Msg_IPC::log (ACE_Log_Record &log_record) 00049 { 00050 // Serialize the log record using a CDR stream, allocate enough 00051 // space for the complete <ACE_Log_Record>. 00052 size_t const max_payload_size = 00053 4 // type 00054 + 4 // pid 00055 + 12 // timestamp 00056 + 4 // process id 00057 + 4 // data length 00058 #if defined (ACE_USES_WCHAR) 00059 + (log_record.msg_data_len () * ACE_OutputCDR::wchar_maxbytes()) // message 00060 #else 00061 + log_record.msg_data_len () // message 00062 #endif 00063 + ACE_CDR::MAX_ALIGNMENT; // padding; 00064 00065 // Insert contents of <log_record> into payload stream. 00066 ACE_OutputCDR payload (max_payload_size); 00067 payload << log_record; 00068 00069 // Get the number of bytes used by the CDR stream. If it becomes desireable 00070 // to support payloads more than 4GB, this field will need to be changed 00071 // to a 64-bit value. 00072 ACE_CDR::ULong length = 00073 ACE_Utils::truncate_cast<ACE_CDR::ULong> (payload.total_length ()); 00074 00075 // Send a header so the receiver can determine the byte order and 00076 // size of the incoming CDR stream. 00077 ACE_OutputCDR header (ACE_CDR::MAX_ALIGNMENT + 8); 00078 header << ACE_OutputCDR::from_boolean (ACE_CDR_BYTE_ORDER); 00079 00080 // Store the size of the payload that follows 00081 header << ACE_CDR::ULong (length); 00082 00083 // Use an iovec to send both buffer and payload simultaneously. 00084 iovec iov[2]; 00085 iov[0].iov_base = header.begin ()->rd_ptr (); 00086 iov[0].iov_len = 8; 00087 iov[1].iov_base = payload.begin ()->rd_ptr (); 00088 iov[1].iov_len = length; 00089 00090 #if defined (ACE_HAS_STREAM_PIPES) 00091 // Use the <putpmsg> API if supported to ensure correct message 00092 // queueing according to priority. 00093 00094 ACE_Str_Buf header_msg (static_cast<void *> (header.begin ()->rd_ptr ()), 00095 static_cast<int> (8)); 00096 00097 ACE_Str_Buf payload_msg (static_cast<void *> (payload.begin ()->rd_ptr ()), 00098 static_cast<int> (length)); 00099 00100 return this->message_queue_.send (&header_msg, 00101 &payload_msg, 00102 static_cast<int> (log_record.priority ()), 00103 MSG_BAND); 00104 #else 00105 // We're running over sockets, so send header and payload 00106 // efficiently using "gather-write". 00107 return this->message_queue_.sendv_n (iov, 2); 00108 #endif /* ACE_HAS_STREAM_PIPES */ 00109 } 00110 00111 ACE_END_VERSIONED_NAMESPACE_DECL