Log_Record.cpp

Go to the documentation of this file.
00001 // Log_Record.cpp,v 4.85 2006/05/30 11:38:44 jwillemsen Exp
00002 
00003 #include "ace/Log_Record.h"
00004 
00005 #include "ace/Log_Msg.h"
00006 #include "ace/ACE.h"
00007 #include "ace/OS_NS_stdio.h"
00008 #include "ace/OS_NS_time.h"
00009 #include "ace/CDR_Stream.h"
00010 
00011 #if !defined (__ACE_INLINE__)
00012 # include "ace/Log_Record.inl"
00013 #endif /* __ACE_INLINE__ */
00014 
00015 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
00016 // FUZZ: disable check_for_streams_include
00017 # include "ace/streams.h"
00018 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
00019 
00020 #include "ace/OS_Memory.h"
00021 
00022 ACE_RCSID(ace, Log_Record, "Log_Record.cpp,v 4.85 2006/05/30 11:38:44 jwillemsen Exp")
00023 
00024 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00025 
00026 ACE_ALLOC_HOOK_DEFINE(ACE_Log_Record)
00027 
00028 namespace
00029 {
00030   // Symbolic names for the <ACE_Log_Priority> enumerators.
00031   ACE_TCHAR const * ace_priority_names[] =
00032     {
00033       ACE_LIB_TEXT ("LM_SHUTDOWN"),
00034       ACE_LIB_TEXT ("LM_TRACE"),
00035       ACE_LIB_TEXT ("LM_DEBUG"),
00036       ACE_LIB_TEXT ("LM_INFO"),
00037       ACE_LIB_TEXT ("LM_NOTICE"),
00038       ACE_LIB_TEXT ("LM_WARNING"),
00039       ACE_LIB_TEXT ("LM_STARTUP"),
00040       ACE_LIB_TEXT ("LM_ERROR"),
00041       ACE_LIB_TEXT ("LM_CRITICAL"),
00042       ACE_LIB_TEXT ("LM_ALERT"),
00043       ACE_LIB_TEXT ("LM_EMERGENCY"),
00044       ACE_LIB_TEXT ("LM_UNK(04000)"),
00045       ACE_LIB_TEXT ("LM_UNK(010000)"),
00046       ACE_LIB_TEXT ("LM_UNK(020000)"),
00047       ACE_LIB_TEXT ("LM_UNK(040000)"),
00048       ACE_LIB_TEXT ("LM_UNK(0100000)"),
00049       ACE_LIB_TEXT ("LM_UNK(0200000)"),
00050       ACE_LIB_TEXT ("LM_UNK(0400000)"),
00051       ACE_LIB_TEXT ("LM_UNK(01000000)"),
00052       ACE_LIB_TEXT ("LM_UNK(02000000)"),
00053       ACE_LIB_TEXT ("LM_UNK(04000000)"),
00054       ACE_LIB_TEXT ("LM_UNK(010000000)"),
00055       ACE_LIB_TEXT ("LM_UNK(020000000)"),
00056       ACE_LIB_TEXT ("LM_UNK(040000000)"),
00057       ACE_LIB_TEXT ("LM_UNK(0100000000)"),
00058       ACE_LIB_TEXT ("LM_UNK(0200000000)"),
00059       ACE_LIB_TEXT ("LM_UNK(0400000000)"),
00060       ACE_LIB_TEXT ("LM_UNK(01000000000)"),
00061       ACE_LIB_TEXT ("LM_UNK(02000000000)"),
00062       ACE_LIB_TEXT ("LM_UNK(04000000000)"),
00063       ACE_LIB_TEXT ("LM_UNK(010000000000)"),
00064       ACE_LIB_TEXT ("LM_UNK(020000000000)")
00065     };
00066 }
00067 
00068 const ACE_TCHAR *
00069 ACE_Log_Record::priority_name (ACE_Log_Priority p)
00070 {
00071   return ace_priority_names[ACE::log2 (p)];
00072 }
00073 
00074 void
00075 ACE_Log_Record::priority_name (ACE_Log_Priority p,
00076                                const ACE_TCHAR *name)
00077 {
00078   // Name must be a statically allocated string
00079   ace_priority_names[ACE::log2 (p)] = name;
00080 }
00081 
00082 u_long
00083 ACE_Log_Record::priority (void) const
00084 {
00085   ACE_TRACE ("ACE_Log_Record::priority");
00086 
00087   // Get the priority of the <Log_Record> <type_>.  This is computed
00088   // as the base 2 logarithm of <type_> (which must be a power of 2,
00089   // as defined by the enums in <ACE_Log_Priority>).
00090   return ACE::log2 ((u_long) this->type_);
00091 }
00092 
00093 void
00094 ACE_Log_Record::priority (u_long p)
00095 {
00096   ACE_TRACE ("ACE_Log_Record::priority");
00097 
00098   // Set the priority of the <Log_Record> <type_> (which must be a
00099   // power of 2, as defined by the enums in <ACE_Log_Priority>).
00100   this->type_ = (ACE_UINT32) p;
00101 }
00102 
00103 void
00104 ACE_Log_Record::dump (void) const
00105 {
00106 #if defined (ACE_HAS_DUMP)
00107   // ACE_TRACE ("ACE_Log_Record::dump");
00108 
00109   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00110   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("length_ = %d\n"), this->length_));
00111   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ntype_ = %u\n"), this->type_));
00112   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ntime_stamp_ = (%d, %d)\n"), this->secs_, this->usecs_));
00113   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\npid_ = %u\n"), this->pid_));
00114   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nmsg_data_ = %s\n"), this->msg_data_));
00115   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00116 #endif /* ACE_HAS_DUMP */
00117 }
00118 
00119 void
00120 ACE_Log_Record::msg_data (const ACE_TCHAR *data)
00121 {
00122   // ACE_TRACE ("ACE_Log_Record::msg_data");
00123   ACE_OS::strsncpy (this->msg_data_, data,
00124                     (MAXLOGMSGLEN / sizeof (ACE_TCHAR)));
00125   this->round_up ();
00126 }
00127 
00128 ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp,
00129                                 long ts_sec,
00130                                 long p)
00131   : length_ (0),
00132     type_ (ACE_UINT32 (lp)),
00133     secs_ (ts_sec),
00134     usecs_ (0),
00135     pid_ (ACE_UINT32 (p)),
00136     msg_data_ (0)
00137 {
00138   // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
00139   ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
00140 }
00141 
00142 ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp,
00143                                 const ACE_Time_Value &ts,
00144                                 long p)
00145   : length_ (0),
00146     type_ (ACE_UINT32 (lp)),
00147     secs_ ((ACE_UINT32) ts.sec ()),
00148     usecs_ ((ACE_UINT32) ts.usec ()),
00149     pid_ (ACE_UINT32 (p)),
00150     msg_data_ (0)
00151 {
00152   // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
00153   ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
00154 }
00155 
00156 void
00157 ACE_Log_Record::round_up (void)
00158 {
00159   // ACE_TRACE ("ACE_Log_Record::round_up");
00160   // Determine the length of the payload.
00161   size_t len = sizeof (*this) + (sizeof (ACE_TCHAR) * ((ACE_OS::strlen (this->msg_data_) + 1)));
00162 
00163   // Round up to the alignment.
00164   len = ((len + ACE_Log_Record::ALIGN_WORDB - 1)
00165          & ~(ACE_Log_Record::ALIGN_WORDB - 1));
00166   this->length_ = static_cast<ACE_UINT32> (len);
00167 }
00168 
00169 ACE_Log_Record::ACE_Log_Record (void)
00170   : length_ (0),
00171     type_ (0),
00172     secs_ (0),
00173     usecs_ (0),
00174     pid_ (0),
00175     msg_data_ (0)
00176 {
00177   // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
00178   ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
00179 }
00180 
00181 int
00182 ACE_Log_Record::format_msg (const ACE_TCHAR host_name[],
00183                             u_long verbose_flag,
00184                             ACE_TCHAR *verbose_msg)
00185 {
00186   /* 0123456789012345678901234     */
00187   /* Oct 18 14:25:36.000 1989<nul> */
00188   ACE_TCHAR timestamp[26]; // Only used by VERBOSE and VERBOSE_LITE.
00189 
00190   // The sprintf format needs to be different for Windows and POSIX
00191   // in the wide-char case.
00192 #if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
00193   const ACE_TCHAR *time_fmt =         ACE_LIB_TEXT ("%s.%03ld %s");
00194   const ACE_TCHAR *verbose_fmt =      ACE_LIB_TEXT ("%s@%s@%u@%s@%s");
00195   const ACE_TCHAR *verbose_lite_fmt = ACE_LIB_TEXT ("%s@%s@%s");
00196 #else
00197   const ACE_TCHAR *time_fmt = ACE_LIB_TEXT ("%ls.%03ld %ls");
00198   const ACE_TCHAR *verbose_fmt = ACE_LIB_TEXT ("%ls@%ls@%u@%ls@%ls");
00199   const ACE_TCHAR *verbose_lite_fmt = ACE_LIB_TEXT ("%ls@%ls@%ls");
00200 #endif
00201 
00202   if (ACE_BIT_ENABLED (verbose_flag,
00203                        ACE_Log_Msg::VERBOSE)
00204       || ACE_BIT_ENABLED (verbose_flag,
00205                           ACE_Log_Msg::VERBOSE_LITE))
00206     {
00207       time_t const now = this->secs_;
00208       ACE_TCHAR ctp[26]; // 26 is a magic number...
00209 
00210       if (ACE_OS::ctime_r (&now, ctp, sizeof ctp) == 0)
00211         return -1;
00212 
00213       /* 01234567890123456789012345 */
00214       /* Wed Oct 18 14:25:36 1989n0 */
00215 
00216       ctp[19] = '\0'; // NUL-terminate after the time.
00217       ctp[24] = '\0'; // NUL-terminate after the date.
00218 
00219       ACE_OS::sprintf (timestamp,
00220                        time_fmt,
00221                        ctp + 4,
00222                        ((long) this->usecs_) / 1000,
00223                        ctp + 20);
00224     }
00225 
00226   if (ACE_BIT_ENABLED (verbose_flag,
00227                        ACE_Log_Msg::VERBOSE))
00228     {
00229       const ACE_TCHAR *lhost_name = ((host_name == 0)
00230                                       ? ACE_LIB_TEXT ("<local_host>")
00231                                       : host_name);
00232       ACE_OS::sprintf (verbose_msg,
00233                        verbose_fmt,
00234                        timestamp,
00235                        lhost_name,
00236                        this->pid_,
00237                        ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_)),
00238                        this->msg_data_);
00239     }
00240   else if (ACE_BIT_ENABLED (verbose_flag, ACE_Log_Msg::VERBOSE_LITE))
00241     ACE_OS::sprintf (verbose_msg,
00242                      verbose_lite_fmt,
00243                      timestamp,
00244                      ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_)),
00245                      this->msg_data_);
00246   else
00247     ACE_OS::strcpy (verbose_msg, this->msg_data_);
00248   return 0;
00249 }
00250 
00251 int
00252 ACE_Log_Record::print (const ACE_TCHAR host_name[],
00253                        u_long verbose_flag,
00254                        FILE *fp)
00255 {
00256   if (ACE_LOG_MSG->log_priority_enabled (ACE_Log_Priority (this->type_)))
00257     {
00258       ACE_TCHAR *verbose_msg = 0;
00259       ACE_NEW_RETURN (verbose_msg, ACE_TCHAR[MAXVERBOSELOGMSGLEN], -1);
00260 
00261       int result = this->format_msg (host_name,
00262                                      verbose_flag,
00263                                      verbose_msg);
00264 
00265       if (result == 0)
00266         {
00267           if (fp != 0)
00268             {
00269               int verbose_msg_len =
00270                 static_cast<int> (ACE_OS::strlen (verbose_msg));
00271               int fwrite_result = ACE_OS::fprintf (fp,
00272                                                    ACE_LIB_TEXT ("%s"),
00273                                                    verbose_msg);
00274               // We should have written everything
00275               if (fwrite_result != verbose_msg_len)
00276                 result = -1;
00277               else
00278                 ACE_OS::fflush (fp);
00279             }
00280         }
00281 
00282       delete [] verbose_msg;
00283 
00284       return result;
00285     }
00286   else
00287     return 0;
00288 }
00289 
00290 int
00291 operator<< (ACE_OutputCDR &cdr,
00292             const ACE_Log_Record &log_record)
00293 {
00294   size_t msglen = log_record.msg_data_len ();
00295   // The ACE_Log_Record::msg_data () function is non-const, since it
00296   // returns a non-const pointer to internal class members. Since we
00297   // know that no members are modified here, we can safely const_cast
00298   // the log_record parameter without violating the interface
00299   // contract.
00300   ACE_Log_Record &nonconst_record = (const_cast<ACE_Log_Record&> (log_record));
00301   // Insert each field from <log_record> into the output CDR stream.
00302   cdr << ACE_CDR::Long (log_record.type ());
00303   cdr << ACE_CDR::Long (log_record.pid ());
00304   cdr << ACE_CDR::Long (log_record.time_stamp ().sec ());
00305   cdr << ACE_CDR::Long (log_record.time_stamp ().usec ());
00306   cdr << ACE_CDR::ULong (msglen);
00307 #if defined (ACE_USES_WCHAR)
00308   cdr.write_wchar_array (nonconst_record.msg_data (), msglen);
00309 #else
00310   cdr.write_char_array (nonconst_record.msg_data (), msglen);
00311 #endif /* ACE_USES_WCHAR */
00312   return cdr.good_bit ();
00313 }
00314 
00315 int
00316 operator>> (ACE_InputCDR &cdr,
00317             ACE_Log_Record &log_record)
00318 {
00319   ACE_CDR::Long type;
00320   ACE_CDR::Long pid;
00321   ACE_CDR::Long sec, usec;
00322   ACE_CDR::ULong buffer_len;
00323 
00324   // Extract each field from input CDR stream into <log_record>.
00325   if ((cdr >> type) && (cdr >> pid) && (cdr >> sec) && (cdr >> usec)
00326       && (cdr >> buffer_len)) {
00327     ACE_TCHAR *log_msg = new ACE_TCHAR[buffer_len + 1];
00328     log_record.type (type);
00329     log_record.pid (pid);
00330     log_record.time_stamp (ACE_Time_Value (sec, usec));
00331 #if defined (ACE_USES_WCHAR)
00332     cdr.read_wchar_array (log_msg, buffer_len);
00333 #else
00334     cdr.read_char_array (log_msg, buffer_len);
00335 #endif /* ACE_USES_WCHAR */
00336     log_msg[buffer_len] = '\0';
00337     log_record.set_msg_data_ptr (log_msg);
00338   }
00339   return cdr.good_bit ();
00340 }
00341 
00342 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
00343 
00344 int
00345 ACE_Log_Record::print (const ACE_TCHAR host_name[],
00346                        u_long verbose_flag,
00347                        ACE_OSTREAM_TYPE &s)
00348 {
00349   if (ACE_LOG_MSG->log_priority_enabled (ACE_Log_Priority (this->type_)))
00350     {
00351       ACE_TCHAR* verbose_msg = 0;
00352       ACE_NEW_RETURN (verbose_msg, ACE_TCHAR[MAXVERBOSELOGMSGLEN], -1);
00353 
00354       int const result = this->format_msg (host_name, verbose_flag, verbose_msg);
00355 
00356       if (result == 0)
00357         {
00358           // Since ostream expects only chars, we cannot pass wchar_t's
00359           s << ACE_TEXT_ALWAYS_CHAR (verbose_msg);
00360           s.flush ();
00361         }
00362 
00363       delete [] verbose_msg;
00364 
00365       return result;
00366     }
00367   return 0;
00368 }
00369 
00370 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
00371 
00372 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:53 2006 for ACE by doxygen 1.3.6