1 #include "ace/Log_Record.h"
3 #include "ace/Log_Msg.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"
22 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
24 ACE_ALLOC_HOOK_DEFINE(ACE_Log_Record
)
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"),
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)")
67 ACE_Log_Record::priority_name (ACE_Log_Priority p
)
69 return ace_priority_names
[ACE::log2 (p
)];
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
;
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_
);
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
;
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);
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
);
143 ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp
,
147 type_ (ACE_UINT32 (lp
)),
150 pid_ (ACE_UINT32 (p
)),
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
)));
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
,
172 type_ (ACE_UINT32 (lp
)),
174 usecs_ ((ACE_UINT32
) ts
.usec ()),
175 pid_ (ACE_UINT32 (p
)),
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
)));
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';
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 ()
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
)));
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
[],
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
,
246 sizeof (timestamp
) / sizeof (ACE_TCHAR
)))
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>")
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
,
269 ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_
)),
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
,
278 ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_
)),
281 ACE_OS::strcpy (verbose_msg
, this->msg_data_
);
286 log_priority_enabled(ACE_Log_Category_TSS
* category
, ACE_Log_Priority priority
)
288 if (category
&& !category
->log_priority_enabled (priority
))
290 return ACE_LOG_MSG
->log_priority_enabled (priority
);
294 ACE_Log_Record::print (const ACE_TCHAR host_name
[],
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);
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
);
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
,
319 // We should have written everything
320 if (fwrite_result
!= verbose_msg_len
)
327 #if defined (ACE_HAS_ALLOC_HOOKS)
328 ACE_Allocator::instance()->free(verbose_msg
);
330 delete [] verbose_msg
;
331 #endif /* ACE_HAS_ALLOC_HOOKS */
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 ());
354 #if defined (ACE_USES_WCHAR)
355 cdr
.write_wchar_array (log_record
.msg_data (), u_msglen
);
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
)
368 ACE_CDR::LongLong sec
;
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
)) {
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);
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
),
386 #if defined (ACE_USES_WCHAR)
387 cdr
.read_wchar_array (log_msg
, buffer_len
);
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
))
395 return cdr
.good_bit ();
398 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
401 ACE_Log_Record::print (const ACE_TCHAR host_name
[],
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
);
415 // Since ostream expects only chars, we cannot pass wchar_t's
416 s
<< ACE_TEXT_ALWAYS_CHAR (verbose_msg
);
420 delete [] verbose_msg
;
427 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
429 ACE_END_VERSIONED_NAMESPACE_DECL