Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Log_Record.cpp
blobea4a782ffd66da08b7656c29a44ae51ba90a7411
1 #include "ace/Log_Record.h"
3 #include "ace/Log_Msg.h"
4 #include "ace/ACE.h"
5 #include "ace/OS_NS_stdio.h"
6 #include "ace/CDR_Stream.h"
7 #include "ace/Truncate.h"
8 #include "ace/Log_Category.h"
10 #if !defined (__ACE_INLINE__)
11 # include "ace/Log_Record.inl"
12 #endif /* __ACE_INLINE__ */
14 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
15 // FUZZ: disable check_for_streams_include
16 # include "ace/streams.h"
17 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
19 #include "ace/OS_Memory.h"
20 #include <memory>
22 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
24 ACE_ALLOC_HOOK_DEFINE(ACE_Log_Record)
26 namespace
28 // Symbolic names for the <ACE_Log_Priority> enumerators.
29 ACE_TCHAR const * ace_priority_names[] =
31 ACE_TEXT ("LM_SHUTDOWN"),
32 ACE_TEXT ("LM_TRACE"),
33 ACE_TEXT ("LM_DEBUG"),
34 ACE_TEXT ("LM_INFO"),
35 ACE_TEXT ("LM_NOTICE"),
36 ACE_TEXT ("LM_WARNING"),
37 ACE_TEXT ("LM_STARTUP"),
38 ACE_TEXT ("LM_ERROR"),
39 ACE_TEXT ("LM_CRITICAL"),
40 ACE_TEXT ("LM_ALERT"),
41 ACE_TEXT ("LM_EMERGENCY"),
42 ACE_TEXT ("LM_UNK(04000)"),
43 ACE_TEXT ("LM_UNK(010000)"),
44 ACE_TEXT ("LM_UNK(020000)"),
45 ACE_TEXT ("LM_UNK(040000)"),
46 ACE_TEXT ("LM_UNK(0100000)"),
47 ACE_TEXT ("LM_UNK(0200000)"),
48 ACE_TEXT ("LM_UNK(0400000)"),
49 ACE_TEXT ("LM_UNK(01000000)"),
50 ACE_TEXT ("LM_UNK(02000000)"),
51 ACE_TEXT ("LM_UNK(04000000)"),
52 ACE_TEXT ("LM_UNK(010000000)"),
53 ACE_TEXT ("LM_UNK(020000000)"),
54 ACE_TEXT ("LM_UNK(040000000)"),
55 ACE_TEXT ("LM_UNK(0100000000)"),
56 ACE_TEXT ("LM_UNK(0200000000)"),
57 ACE_TEXT ("LM_UNK(0400000000)"),
58 ACE_TEXT ("LM_UNK(01000000000)"),
59 ACE_TEXT ("LM_UNK(02000000000)"),
60 ACE_TEXT ("LM_UNK(04000000000)"),
61 ACE_TEXT ("LM_UNK(010000000000)"),
62 ACE_TEXT ("LM_UNK(020000000000)")
66 const ACE_TCHAR *
67 ACE_Log_Record::priority_name (ACE_Log_Priority p)
69 return ace_priority_names[ACE::log2 (p)];
72 void
73 ACE_Log_Record::priority_name (ACE_Log_Priority p,
74 const ACE_TCHAR *name)
76 // Name must be a statically allocated string
77 ace_priority_names[ACE::log2 (p)] = name;
80 u_long
81 ACE_Log_Record::priority () const
83 ACE_TRACE ("ACE_Log_Record::priority");
85 // Get the priority of the <Log_Record> <type_>. This is computed
86 // as the base 2 logarithm of <type_> (which must be a power of 2,
87 // as defined by the enums in <ACE_Log_Priority>).
88 return ACE::log2 ((u_long) this->type_);
91 void
92 ACE_Log_Record::priority (u_long p)
94 ACE_TRACE ("ACE_Log_Record::priority");
96 // Set the priority of the <Log_Record> <type_> (which must be a
97 // power of 2, as defined by the enums in <ACE_Log_Priority>).
98 this->type_ = (ACE_UINT32) p;
101 void
102 ACE_Log_Record::dump () const
104 #if defined (ACE_HAS_DUMP)
105 // ACE_TRACE ("ACE_Log_Record::dump");
107 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
108 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("length_ = %d\n"), this->length_));
109 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\ntype_ = %u\n"), this->type_));
110 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\ntime_stamp_ = (%:, %d)\n"),
111 this->secs_, this->usecs_));
112 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\npid_ = %u\n"), this->pid_));
113 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\nmsg_data_ (0x%@) = %s\n"),
114 this->msg_data_, this->msg_data_));
115 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\nmsg_data_size_ = %B\n"),
116 this->msg_data_size_));
117 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
118 #endif /* ACE_HAS_DUMP */
122 ACE_Log_Record::msg_data (const ACE_TCHAR *data)
124 // ACE_TRACE ("ACE_Log_Record::msg_data");
125 size_t const newlen = ACE_OS::strlen (data) + 1; // Will need room for '\0'
126 if (newlen > this->msg_data_size_)
128 this->msg_data_size_ = 0;
129 #if defined (ACE_HAS_ALLOC_HOOKS)
130 ACE_Allocator::instance()->free(this->msg_data_);
131 ACE_ALLOCATOR_RETURN (this->msg_data_, static_cast<ACE_TCHAR*>(ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * newlen)), -1);
132 #else
133 delete [] this->msg_data_;
134 ACE_NEW_RETURN (this->msg_data_, ACE_TCHAR[newlen], -1);
135 #endif /* ACE_HAS_ALLOC_HOOKS */
136 this->msg_data_size_ = newlen;
138 ACE_OS::strcpy (this->msg_data_, data);
139 this->round_up ();
140 return 0;
143 ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp,
144 time_t ts_sec,
145 long p)
146 : length_ (0),
147 type_ (ACE_UINT32 (lp)),
148 secs_ (ts_sec),
149 usecs_ (0),
150 pid_ (ACE_UINT32 (p)),
151 msg_data_ (0),
152 msg_data_size_ (0),
153 category_(0)
155 // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
156 #if defined (ACE_HAS_ALLOC_HOOKS)
157 ACE_ALLOCATOR_NORETURN (this->msg_data_, static_cast<ACE_TCHAR*> (ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * MAXLOGMSGLEN)));
158 #else
159 ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
160 #endif /* ACE_HAS_ALLOC_HOOKS */
161 if (0 != this->msg_data_)
163 this->msg_data_size_ = MAXLOGMSGLEN;
164 this->msg_data_[0] = '\0';
168 ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp,
169 const ACE_Time_Value &ts,
170 long p)
171 : length_ (0),
172 type_ (ACE_UINT32 (lp)),
173 secs_ (ts.sec ()),
174 usecs_ ((ACE_UINT32) ts.usec ()),
175 pid_ (ACE_UINT32 (p)),
176 msg_data_ (0),
177 msg_data_size_ (0),
178 category_(0)
180 // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
181 #if defined (ACE_HAS_ALLOC_HOOKS)
182 ACE_ALLOCATOR_NORETURN (this->msg_data_, static_cast<ACE_TCHAR*> (ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * MAXLOGMSGLEN)));
183 #else
184 ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
185 #endif /* ACE_HAS_ALLOC_HOOKS */
186 if (0 != this->msg_data_)
188 this->msg_data_size_ = MAXLOGMSGLEN;
189 this->msg_data_[0] = '\0';
193 void
194 ACE_Log_Record::round_up ()
196 // ACE_TRACE ("ACE_Log_Record::round_up");
197 // Determine the length of the payload.
198 size_t len = sizeof (*this) + (sizeof (ACE_TCHAR) * ((ACE_OS::strlen (this->msg_data_) + 1)));
200 // Round up to the alignment.
201 len = ((len + ACE_Log_Record::ALIGN_WORDB - 1)
202 & ~(ACE_Log_Record::ALIGN_WORDB - 1));
203 this->length_ = static_cast<ACE_UINT32> (len);
206 ACE_Log_Record::ACE_Log_Record ()
207 : length_ (0),
208 type_ (0),
209 secs_ (0),
210 usecs_ (0),
211 pid_ (0),
212 msg_data_ (0),
213 msg_data_size_ (0),
214 category_(0)
216 // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
217 #if defined (ACE_HAS_ALLOC_HOOKS)
218 ACE_ALLOCATOR_NORETURN (this->msg_data_, static_cast<ACE_TCHAR*> (ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * MAXLOGMSGLEN)));
219 #else
220 ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
221 #endif /* ACE_HAS_ALLOC_HOOKS */
222 if (0 != this->msg_data_)
224 this->msg_data_size_ = MAXLOGMSGLEN;
225 this->msg_data_[0] = '\0';
230 ACE_Log_Record::format_msg (const ACE_TCHAR host_name[],
231 u_long verbose_flag,
232 ACE_TCHAR *verbose_msg, size_t verbose_msg_size)
234 /* 012345678901234567890123456 */
235 /* yyyy-mm-dd hh:mm:ss.mmmmmm<nul> */
236 ACE_TCHAR timestamp[27]; // Only used by VERBOSE and VERBOSE_LITE.
238 if (ACE_BIT_ENABLED (verbose_flag,
239 ACE_Log_Msg::VERBOSE)
240 || ACE_BIT_ENABLED (verbose_flag,
241 ACE_Log_Msg::VERBOSE_LITE))
243 ACE_Time_Value reftime (this->secs_, this->usecs_);
244 if (0 == ACE::timestamp (reftime,
245 timestamp,
246 sizeof (timestamp) / sizeof (ACE_TCHAR)))
247 return -1;
249 // Historical timestamp in VERBOSE[_LITE] used 3 places for partial sec.
250 // 012345678901234567890123456
251 // 1989-10-18 14:25:36.123<nul>
252 timestamp[23] = '\0';
255 if (ACE_BIT_ENABLED (verbose_flag,
256 ACE_Log_Msg::VERBOSE))
258 const ACE_TCHAR *lhost_name = ((host_name == 0)
259 ? ACE_TEXT ("<local_host>")
260 : host_name);
261 ACE_OS::snprintf (verbose_msg, verbose_msg_size,
262 ACE_TEXT ("%") ACE_TEXT_PRIs
263 ACE_TEXT ("@%") ACE_TEXT_PRIs
264 ACE_TEXT ("@%u@%") ACE_TEXT_PRIs
265 ACE_TEXT ("@%") ACE_TEXT_PRIs,
266 timestamp,
267 lhost_name,
268 this->pid_,
269 ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_)),
270 this->msg_data_);
272 else if (ACE_BIT_ENABLED (verbose_flag, ACE_Log_Msg::VERBOSE_LITE))
273 ACE_OS::snprintf (verbose_msg, verbose_msg_size,
274 ACE_TEXT ("%") ACE_TEXT_PRIs
275 ACE_TEXT ("@%") ACE_TEXT_PRIs
276 ACE_TEXT ("@%") ACE_TEXT_PRIs,
277 timestamp,
278 ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_)),
279 this->msg_data_);
280 else
281 ACE_OS::strcpy (verbose_msg, this->msg_data_);
282 return 0;
285 inline bool
286 log_priority_enabled(ACE_Log_Category_TSS* category, ACE_Log_Priority priority)
288 if (category && !category->log_priority_enabled (priority))
289 return false;
290 return ACE_LOG_MSG->log_priority_enabled (priority);
294 ACE_Log_Record::print (const ACE_TCHAR host_name[],
295 u_long verbose_flag,
296 FILE *fp)
298 if ( log_priority_enabled(this->category(), ACE_Log_Priority (this->type_)) )
300 ACE_TCHAR *verbose_msg = 0;
301 #if defined (ACE_HAS_ALLOC_HOOKS)
302 ACE_ALLOCATOR_RETURN (verbose_msg, static_cast<ACE_TCHAR *>(ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * MAXVERBOSELOGMSGLEN)), -1);
303 #else
304 ACE_NEW_RETURN (verbose_msg, ACE_TCHAR[MAXVERBOSELOGMSGLEN], -1);
305 #endif /* ACE_HAS_ALLOC_HOOKS */
307 int result = this->format_msg (host_name, verbose_flag, verbose_msg,
308 MAXVERBOSELOGMSGLEN);
310 if (result == 0)
312 if (fp != 0)
314 int const verbose_msg_len =
315 static_cast<int> (ACE_OS::strlen (verbose_msg));
316 int const fwrite_result = ACE_OS::fprintf (fp,
317 ACE_TEXT ("%") ACE_TEXT_PRIs,
318 verbose_msg);
319 // We should have written everything
320 if (fwrite_result != verbose_msg_len)
321 result = -1;
322 else
323 ACE_OS::fflush (fp);
327 #if defined (ACE_HAS_ALLOC_HOOKS)
328 ACE_Allocator::instance()->free(verbose_msg);
329 #else
330 delete [] verbose_msg;
331 #endif /* ACE_HAS_ALLOC_HOOKS */
333 return result;
335 else
336 return 0;
340 operator<< (ACE_OutputCDR &cdr,
341 const ACE_Log_Record &log_record)
343 // The written message length can't be more than 32 bits (ACE_CDR::ULong)
344 // so reduce it here if needed.
345 ACE_CDR::ULong u_msglen =
346 ACE_Utils::truncate_cast<ACE_CDR::ULong> (log_record.msg_data_len ());
348 // Insert each field from <log_record> into the output CDR stream.
349 cdr << ACE_CDR::Long (log_record.type ());
350 cdr << ACE_CDR::Long (log_record.pid ());
351 cdr << ACE_CDR::LongLong (log_record.time_stamp ().sec ());
352 cdr << ACE_CDR::Long (log_record.time_stamp ().usec ());
353 cdr << u_msglen;
354 #if defined (ACE_USES_WCHAR)
355 cdr.write_wchar_array (log_record.msg_data (), u_msglen);
356 #else
357 cdr.write_char_array (log_record.msg_data (), u_msglen);
358 #endif /* ACE_USES_WCHAR */
359 return cdr.good_bit ();
363 operator>> (ACE_InputCDR &cdr,
364 ACE_Log_Record &log_record)
366 ACE_CDR::Long type;
367 ACE_CDR::Long pid;
368 ACE_CDR::LongLong sec;
369 ACE_CDR::Long usec;
370 ACE_CDR::ULong buffer_len;
372 // Extract each field from input CDR stream into <log_record>.
373 if ((cdr >> type) && (cdr >> pid) && (cdr >> sec) && (cdr >> usec)
374 && (cdr >> buffer_len)) {
375 ACE_TCHAR *log_msg;
376 #if defined (ACE_HAS_ALLOC_HOOKS)
377 ACE_ALLOCATOR_RETURN (log_msg, static_cast<ACE_TCHAR *> (ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * (buffer_len + 1))), -1);
378 #else
379 ACE_NEW_RETURN (log_msg, ACE_TCHAR[buffer_len + 1], -1);
380 #endif /* ACE_HAS_ALLOC_HOOKS */
381 std::unique_ptr<ACE_TCHAR[]> log_msg_p (log_msg);
382 log_record.type (type);
383 log_record.pid (pid);
384 log_record.time_stamp (ACE_Time_Value (ACE_Utils::truncate_cast<time_t> (sec),
385 usec));
386 #if defined (ACE_USES_WCHAR)
387 cdr.read_wchar_array (log_msg, buffer_len);
388 #else
389 cdr.read_char_array (log_msg, buffer_len);
390 #endif /* ACE_USES_WCHAR */
391 log_msg[buffer_len] = '\0';
392 if (-1 == log_record.msg_data (log_msg))
393 return -1;
395 return cdr.good_bit ();
398 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
401 ACE_Log_Record::print (const ACE_TCHAR host_name[],
402 u_long verbose_flag,
403 ACE_OSTREAM_TYPE &s)
405 if ( log_priority_enabled(this->category(), ACE_Log_Priority (this->type_)) )
407 ACE_TCHAR* verbose_msg = 0;
408 ACE_NEW_RETURN (verbose_msg, ACE_TCHAR[MAXVERBOSELOGMSGLEN], -1);
410 int const result = this->format_msg (host_name, verbose_flag, verbose_msg,
411 MAXVERBOSELOGMSGLEN);
413 if (result == 0)
415 // Since ostream expects only chars, we cannot pass wchar_t's
416 s << ACE_TEXT_ALWAYS_CHAR (verbose_msg);
417 s.flush ();
420 delete [] verbose_msg;
422 return result;
424 return 0;
427 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
429 ACE_END_VERSIONED_NAMESPACE_DECL