00001
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
00014
00015 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
00016
00017 # include "ace/streams.h"
00018 #endif
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
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
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
00088
00089
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
00099
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
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
00117 }
00118
00119 void
00120 ACE_Log_Record::msg_data (const ACE_TCHAR *data)
00121 {
00122
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
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
00153 ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
00154 }
00155
00156 void
00157 ACE_Log_Record::round_up (void)
00158 {
00159
00160
00161 size_t len = sizeof (*this) + (sizeof (ACE_TCHAR) * ((ACE_OS::strlen (this->msg_data_) + 1)));
00162
00163
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
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
00187
00188 ACE_TCHAR timestamp[26];
00189
00190
00191
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];
00209
00210 if (ACE_OS::ctime_r (&now, ctp, sizeof ctp) == 0)
00211 return -1;
00212
00213
00214
00215
00216 ctp[19] = '\0';
00217 ctp[24] = '\0';
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
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
00296
00297
00298
00299
00300 ACE_Log_Record &nonconst_record = (const_cast<ACE_Log_Record&> (log_record));
00301
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
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
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
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
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
00371
00372 ACE_END_VERSIONED_NAMESPACE_DECL