Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Log_Record.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Log_Record.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:21 chad Exp $
00003 
00004 #include "ace/Log_Record.h"
00005 #include "ace/Log_Msg.h"
00006 #include "ace/ACE.h"
00007 
00008 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
00009 # include "ace/streams.h"
00010 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
00011 
00012 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00013 # include "ace/Log_Record.i"
00014 #endif
00015 
00016 ACE_RCSID(ace, Log_Record, "$Id: Log_Record.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:21 chad Exp $")
00017 
00018 ACE_ALLOC_HOOK_DEFINE(ACE_Log_Record)
00019 
00020 const ACE_TCHAR *ACE_Log_Record::priority_names_[] =
00021 {
00022   ACE_LIB_TEXT ("<none>"),
00023   ACE_LIB_TEXT ("LM_SHUTDOWN"),
00024   ACE_LIB_TEXT ("LM_TRACE"),
00025   ACE_LIB_TEXT ("LM_DEBUG"),
00026   ACE_LIB_TEXT ("LM_INFO"),
00027   ACE_LIB_TEXT ("LM_NOTICE"),
00028   ACE_LIB_TEXT ("LM_WARNING"),
00029   ACE_LIB_TEXT ("LM_STARTUP"),
00030   ACE_LIB_TEXT ("LM_ERROR"),
00031   ACE_LIB_TEXT ("LM_CRITICAL"),
00032   ACE_LIB_TEXT ("LM_ALERT"),
00033   ACE_LIB_TEXT ("LM_EMERGENCY"),
00034   ACE_LIB_TEXT ("LM_UNK(04000)"),
00035   ACE_LIB_TEXT ("LM_UNK(010000)"),
00036   ACE_LIB_TEXT ("LM_UNK(020000)"),
00037   ACE_LIB_TEXT ("LM_UNK(040000)"),
00038   ACE_LIB_TEXT ("LM_UNK(0100000)"),
00039   ACE_LIB_TEXT ("LM_UNK(0200000)"),
00040   ACE_LIB_TEXT ("LM_UNK(0400000)"),
00041   ACE_LIB_TEXT ("LM_UNK(01000000)"),
00042   ACE_LIB_TEXT ("LM_UNK(02000000)"),
00043   ACE_LIB_TEXT ("LM_UNK(04000000)"),
00044   ACE_LIB_TEXT ("LM_UNK(010000000)"),
00045   ACE_LIB_TEXT ("LM_UNK(020000000)"),
00046   ACE_LIB_TEXT ("LM_UNK(040000000)"),
00047   ACE_LIB_TEXT ("LM_UNK(0100000000)"),
00048   ACE_LIB_TEXT ("LM_UNK(0200000000)"),
00049   ACE_LIB_TEXT ("LM_UNK(0400000000)"),
00050   ACE_LIB_TEXT ("LM_UNK(01000000000)"),
00051   ACE_LIB_TEXT ("LM_UNK(02000000000)"),
00052   ACE_LIB_TEXT ("LM_UNK(04000000000)"),
00053   ACE_LIB_TEXT ("LM_UNK(010000000000)"),
00054   ACE_LIB_TEXT ("LM_UNK(020000000000)")
00055 };
00056 
00057 const ACE_TCHAR *
00058 ACE_Log_Record::priority_name (ACE_Log_Priority p)
00059 {
00060   return ACE_Log_Record::priority_names_[ACE::log2 (p)];
00061 }
00062 
00063 void
00064 ACE_Log_Record::priority_name (ACE_Log_Priority p,
00065                                const ACE_TCHAR *name)
00066 {
00067   // Name must be a statically allocated string
00068   ACE_Log_Record::priority_names_[ACE::log2 (p)] = name;
00069 }
00070 
00071 u_long
00072 ACE_Log_Record::priority (void) const
00073 {
00074   ACE_TRACE ("ACE_Log_Record::priority");
00075 
00076   // Get the priority of the <Log_Record> <type_>.  This is computed
00077   // as the base 2 logarithm of <type_> (which must be a power of 2,
00078   // as defined by the enums in <ACE_Log_Priority>).
00079   return ACE::log2 ((u_long) this->type_);
00080 }
00081 
00082 void
00083 ACE_Log_Record::priority (u_long p)
00084 {
00085   ACE_TRACE ("ACE_Log_Record::priority");
00086 
00087   // Set the priority of the <Log_Record> <type_> (which must be a
00088   // power of 2, as defined by the enums in <ACE_Log_Priority>).
00089   this->type_ = (ACE_UINT32) p;
00090 }
00091 
00092 void
00093 ACE_Log_Record::dump (void) const
00094 {
00095   // ACE_TRACE ("ACE_Log_Record::dump");
00096 
00097   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00098   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("length_ = %d\n"), this->length_));
00099   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ntype_ = %u\n"), this->type_));
00100   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ntime_stamp_ = (%d, %d)\n"), this->secs_, this->usecs_));
00101   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\npid_ = %u\n"), this->pid_));
00102   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\nmsg_data_ = %s\n"), this->msg_data_));
00103   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00104 }
00105 
00106 void
00107 ACE_Log_Record::msg_data (const ACE_TCHAR *data)
00108 {
00109   // ACE_TRACE ("ACE_Log_Record::msg_data");
00110   ACE_OS::strsncpy (this->msg_data_, data,
00111                     (sizeof this->msg_data_ / sizeof (ACE_TCHAR)));
00112   this->round_up ();
00113 }
00114 
00115 ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp,
00116                                 long ts_sec,
00117                                 long p)
00118   : length_ (0),
00119     type_ (ACE_UINT32 (lp)),
00120     secs_ (ts_sec),
00121     usecs_ (0),
00122     pid_ (ACE_UINT32 (p))
00123 {
00124   // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
00125 }
00126 
00127 ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp,
00128                                 const ACE_Time_Value &ts,
00129                                 long p)
00130   : length_ (0),
00131     type_ (ACE_UINT32 (lp)),
00132     secs_ ((ACE_UINT32) ts.sec ()),
00133     usecs_ ((ACE_UINT32) ts.usec ()),
00134     pid_ (ACE_UINT32 (p))
00135 {
00136   // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
00137 }
00138 
00139 void
00140 ACE_Log_Record::round_up (void)
00141 {
00142   // ACE_TRACE ("ACE_Log_Record::round_up");
00143   // Determine the length of the payload.
00144   size_t len = (sizeof (*this) - sizeof (this->msg_data_))
00145     + (sizeof (ACE_TCHAR) * ((ACE_OS::strlen (this->msg_data_) + 1)));
00146 
00147   // Round up to the alignment.
00148   len = ((len + ACE_Log_Record::ALIGN_WORDB - 1)
00149          & ~(ACE_Log_Record::ALIGN_WORDB - 1));
00150   this->length_ = ACE_static_cast (ACE_UINT32, len);
00151 }
00152 
00153 ACE_Log_Record::ACE_Log_Record (void)
00154   : length_ (0),
00155     type_ (0),
00156     secs_ (0),
00157     usecs_ (0),
00158     pid_ (0)
00159 {
00160   // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
00161 }
00162 
00163 int
00164 ACE_Log_Record::format_msg (const ACE_TCHAR host_name[],
00165                             u_long verbose_flag,
00166                             ACE_TCHAR *verbose_msg)
00167 {
00168   /* 0123456789012345678901234     */
00169   /* Oct 18 14:25:36.000 1989<nul> */
00170   ACE_TCHAR timestamp[26]; // Only used by VERBOSE and VERBOSE_LITE.
00171 
00172   if (ACE_BIT_ENABLED (verbose_flag,
00173                        ACE_Log_Msg::VERBOSE)
00174       || ACE_BIT_ENABLED (verbose_flag,
00175                           ACE_Log_Msg::VERBOSE_LITE))
00176     {
00177       time_t now = this->secs_;
00178       ACE_TCHAR ctp[26]; // 26 is a magic number...
00179 
00180       if (ACE_OS::ctime_r (&now, ctp, sizeof ctp) == 0)
00181         return -1;
00182 
00183       /* 01234567890123456789012345 */
00184       /* Wed Oct 18 14:25:36 1989n0 */
00185 
00186       ctp[19] = '\0'; // NUL-terminate after the time.
00187       ctp[24] = '\0'; // NUL-terminate after the date.
00188 
00189       ACE_OS::sprintf (timestamp,
00190                        ACE_LIB_TEXT ("%s.%03ld %s"),
00191                        ctp + 4,
00192                        ((long) this->usecs_) / 1000,
00193                        ctp + 20);
00194     }
00195 
00196   if (ACE_BIT_ENABLED (verbose_flag,
00197                        ACE_Log_Msg::VERBOSE))
00198     {
00199 # if defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS)
00200       const ACE_TCHAR *lhost_name =  (const ACE_TCHAR *) ((host_name == 0)
00201                                                             ? ((char *) ACE_LIB_TEXT ("<local_host>"))
00202                                                             : ((char *) host_name));
00203 # else /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
00204       const ACE_TCHAR *lhost_name = ((host_name == 0)
00205                                       ? ACE_LIB_TEXT ("<local_host>")
00206                                       : host_name);
00207 # endif /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
00208       ACE_OS::sprintf (verbose_msg,
00209                        ACE_LIB_TEXT ("%s@%s@%u@%s@%s"),
00210                        timestamp,
00211                        lhost_name,
00212                        this->pid_,
00213                        ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_)),
00214                        this->msg_data_);
00215     }
00216   else if (ACE_BIT_ENABLED (verbose_flag, ACE_Log_Msg::VERBOSE_LITE))
00217     ACE_OS::sprintf (verbose_msg,
00218                      ACE_LIB_TEXT ("%s@%s@%s"),
00219                      timestamp,
00220                      ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_)),
00221                      this->msg_data_);
00222   else
00223     ACE_OS::sprintf (verbose_msg,
00224                      ACE_LIB_TEXT ("%s"),
00225                      this->msg_data_);
00226   return 0;
00227 }
00228 
00229 int
00230 ACE_Log_Record::print (const ACE_TCHAR host_name[],
00231                        u_long verbose_flag,
00232                        FILE *fp)
00233 {
00234   ACE_TCHAR verbose_msg [MAXVERBOSELOGMSGLEN];
00235   int result = this->format_msg (host_name,
00236                                  verbose_flag,
00237                                  verbose_msg);
00238 
00239   if (result == 0)
00240     {
00241       if (fp != 0)
00242         {
00243           int verbose_msg_len = ACE_static_cast (int,
00244                                                  ACE_OS::strlen (verbose_msg));
00245           int fwrite_result = ACE_OS::fprintf (fp, ACE_LIB_TEXT ("%s"), verbose_msg);
00246 
00247           // We should have written everything
00248           if (fwrite_result != verbose_msg_len)
00249             result = -1;
00250           else
00251             ACE_OS::fflush (fp);
00252         }
00253     }
00254 
00255   return result;
00256 }
00257 
00258 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
00259 
00260 int
00261 ACE_Log_Record::print (const ACE_TCHAR host_name[],
00262                        u_long verbose_flag,
00263                        ostream &s)
00264 {
00265   ACE_TCHAR verbose_msg [MAXVERBOSELOGMSGLEN];
00266   int result = this->format_msg (host_name, verbose_flag, verbose_msg);
00267 
00268   if (result == 0)
00269     {
00270       // Since ostream expects only chars, we cannot pass wchar_t's
00271       s << ACE_TEXT_ALWAYS_CHAR (verbose_msg);
00272       s.flush ();
00273     }
00274 
00275   return result;
00276 }
00277 
00278 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */

Generated on Mon Jun 16 11:20:01 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002