summaryrefslogtreecommitdiff
path: root/ACE/ace/Log_Record.cpp
blob: cae0e3b2c2cc216d7627c3c916a53dfaca9bddef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include "ace/Log_Record.h"

#include "ace/Log_Msg.h"
#include "ace/ACE.h"
#include "ace/OS_NS_stdio.h"
#include "ace/CDR_Stream.h"
#include "ace/Auto_Ptr.h"
#include "ace/Truncate.h"
#include "ace/Log_Category.h"

#if !defined (__ACE_INLINE__)
# include "ace/Log_Record.inl"
#endif /* __ACE_INLINE__ */

#if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
// FUZZ: disable check_for_streams_include
# include "ace/streams.h"
#endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */

#include "ace/OS_Memory.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

ACE_ALLOC_HOOK_DEFINE(ACE_Log_Record)

namespace
{
  // Symbolic names for the <ACE_Log_Priority> enumerators.
  ACE_TCHAR const * ace_priority_names[] =
    {
      ACE_TEXT ("LM_SHUTDOWN"),
      ACE_TEXT ("LM_TRACE"),
      ACE_TEXT ("LM_DEBUG"),
      ACE_TEXT ("LM_INFO"),
      ACE_TEXT ("LM_NOTICE"),
      ACE_TEXT ("LM_WARNING"),
      ACE_TEXT ("LM_STARTUP"),
      ACE_TEXT ("LM_ERROR"),
      ACE_TEXT ("LM_CRITICAL"),
      ACE_TEXT ("LM_ALERT"),
      ACE_TEXT ("LM_EMERGENCY"),
      ACE_TEXT ("LM_UNK(04000)"),
      ACE_TEXT ("LM_UNK(010000)"),
      ACE_TEXT ("LM_UNK(020000)"),
      ACE_TEXT ("LM_UNK(040000)"),
      ACE_TEXT ("LM_UNK(0100000)"),
      ACE_TEXT ("LM_UNK(0200000)"),
      ACE_TEXT ("LM_UNK(0400000)"),
      ACE_TEXT ("LM_UNK(01000000)"),
      ACE_TEXT ("LM_UNK(02000000)"),
      ACE_TEXT ("LM_UNK(04000000)"),
      ACE_TEXT ("LM_UNK(010000000)"),
      ACE_TEXT ("LM_UNK(020000000)"),
      ACE_TEXT ("LM_UNK(040000000)"),
      ACE_TEXT ("LM_UNK(0100000000)"),
      ACE_TEXT ("LM_UNK(0200000000)"),
      ACE_TEXT ("LM_UNK(0400000000)"),
      ACE_TEXT ("LM_UNK(01000000000)"),
      ACE_TEXT ("LM_UNK(02000000000)"),
      ACE_TEXT ("LM_UNK(04000000000)"),
      ACE_TEXT ("LM_UNK(010000000000)"),
      ACE_TEXT ("LM_UNK(020000000000)")
    };
}

const ACE_TCHAR *
ACE_Log_Record::priority_name (ACE_Log_Priority p)
{
  return ace_priority_names[ACE::log2 (p)];
}

void
ACE_Log_Record::priority_name (ACE_Log_Priority p,
                               const ACE_TCHAR *name)
{
  // Name must be a statically allocated string
  ace_priority_names[ACE::log2 (p)] = name;
}

u_long
ACE_Log_Record::priority () const
{
  ACE_TRACE ("ACE_Log_Record::priority");

  // Get the priority of the <Log_Record> <type_>.  This is computed
  // as the base 2 logarithm of <type_> (which must be a power of 2,
  // as defined by the enums in <ACE_Log_Priority>).
  return ACE::log2 ((u_long) this->type_);
}

void
ACE_Log_Record::priority (u_long p)
{
  ACE_TRACE ("ACE_Log_Record::priority");

  // Set the priority of the <Log_Record> <type_> (which must be a
  // power of 2, as defined by the enums in <ACE_Log_Priority>).
  this->type_ = (ACE_UINT32) p;
}

void
ACE_Log_Record::dump () const
{
#if defined (ACE_HAS_DUMP)
  // ACE_TRACE ("ACE_Log_Record::dump");

  ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("length_ = %d\n"), this->length_));
  ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\ntype_ = %u\n"), this->type_));
  ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\ntime_stamp_ = (%:, %d)\n"),
              this->secs_, this->usecs_));
  ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\npid_ = %u\n"), this->pid_));
  ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\nmsg_data_ (0x%@) = %s\n"),
              this->msg_data_, this->msg_data_));
  ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\nmsg_data_size_ = %B\n"),
              this->msg_data_size_));
  ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}

int
ACE_Log_Record::msg_data (const ACE_TCHAR *data)
{
  // ACE_TRACE ("ACE_Log_Record::msg_data");
  size_t const newlen = ACE_OS::strlen (data) + 1;  // Will need room for '\0'
  if (newlen > this->msg_data_size_)
    {
      this->msg_data_size_ = 0;
#if defined (ACE_HAS_ALLOC_HOOKS)
      ACE_Allocator::instance()->free(this->msg_data_);
      ACE_ALLOCATOR_RETURN (this->msg_data_, static_cast<ACE_TCHAR*>(ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * newlen)), -1);
#else
      delete [] this->msg_data_;
      ACE_NEW_RETURN (this->msg_data_, ACE_TCHAR[newlen], -1);
#endif /* ACE_HAS_ALLOC_HOOKS */
      this->msg_data_size_ = newlen;
    }
  ACE_OS::strcpy (this->msg_data_, data);
  this->round_up ();
  return 0;
}

ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp,
                                time_t ts_sec,
                                long p)
  : length_ (0),
    type_ (ACE_UINT32 (lp)),
    secs_ (ts_sec),
    usecs_ (0),
    pid_ (ACE_UINT32 (p)),
    msg_data_ (0),
    msg_data_size_ (0),
    category_(0)
{
  // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
#if defined (ACE_HAS_ALLOC_HOOKS)
  ACE_ALLOCATOR_NORETURN (this->msg_data_, static_cast<ACE_TCHAR*> (ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * MAXLOGMSGLEN)));
#else
  ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
#endif /* ACE_HAS_ALLOC_HOOKS */
  if (0 != this->msg_data_)
    {
      this->msg_data_size_ = MAXLOGMSGLEN;
      this->msg_data_[0] = '\0';
    }
}

ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp,
                                const ACE_Time_Value &ts,
                                long p)
  : length_ (0),
    type_ (ACE_UINT32 (lp)),
    secs_ (ts.sec ()),
    usecs_ ((ACE_UINT32) ts.usec ()),
    pid_ (ACE_UINT32 (p)),
    msg_data_ (0),
    msg_data_size_ (0),
    category_(0)
{
  // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
#if defined (ACE_HAS_ALLOC_HOOKS)
  ACE_ALLOCATOR_NORETURN (this->msg_data_, static_cast<ACE_TCHAR*> (ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * MAXLOGMSGLEN)));
#else
  ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
#endif /* ACE_HAS_ALLOC_HOOKS */
  if (0 != this->msg_data_)
    {
      this->msg_data_size_ = MAXLOGMSGLEN;
      this->msg_data_[0] = '\0';
    }
}

void
ACE_Log_Record::round_up ()
{
  // ACE_TRACE ("ACE_Log_Record::round_up");
  // Determine the length of the payload.
  size_t len = sizeof (*this) + (sizeof (ACE_TCHAR) * ((ACE_OS::strlen (this->msg_data_) + 1)));

  // Round up to the alignment.
  len = ((len + ACE_Log_Record::ALIGN_WORDB - 1)
         & ~(ACE_Log_Record::ALIGN_WORDB - 1));
  this->length_ = static_cast<ACE_UINT32> (len);
}

ACE_Log_Record::ACE_Log_Record ()
  : length_ (0),
    type_ (0),
    secs_ (0),
    usecs_ (0),
    pid_ (0),
    msg_data_ (0),
    msg_data_size_ (0),
    category_(0)
{
  // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
#if defined (ACE_HAS_ALLOC_HOOKS)
  ACE_ALLOCATOR_NORETURN (this->msg_data_, static_cast<ACE_TCHAR*> (ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * MAXLOGMSGLEN)));
#else
  ACE_NEW_NORETURN (this->msg_data_, ACE_TCHAR[MAXLOGMSGLEN]);
#endif /* ACE_HAS_ALLOC_HOOKS */
  if (0 != this->msg_data_)
    {
      this->msg_data_size_ = MAXLOGMSGLEN;
      this->msg_data_[0] = '\0';
    }
}

int
ACE_Log_Record::format_msg (const ACE_TCHAR host_name[],
                            u_long verbose_flag,
                            ACE_TCHAR *verbose_msg, size_t verbose_msg_size)
{
  /* 012345678901234567890123456     */
  /* yyyy-mm-dd hh:mm:ss.mmmmmm<nul> */
  ACE_TCHAR timestamp[27]; // Only used by VERBOSE and VERBOSE_LITE.

  // The sprintf format needs to be different for Windows and POSIX
  // in the wide-char case.
#if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR)
  const ACE_TCHAR *verbose_fmt =      ACE_TEXT ("%s@%s@%u@%s@%s");
  const ACE_TCHAR *verbose_lite_fmt = ACE_TEXT ("%s@%s@%s");
#else
  const ACE_TCHAR *verbose_fmt = ACE_TEXT ("%ls@%ls@%u@%ls@%ls");
  const ACE_TCHAR *verbose_lite_fmt = ACE_TEXT ("%ls@%ls@%ls");
#endif

  if (ACE_BIT_ENABLED (verbose_flag,
                       ACE_Log_Msg::VERBOSE)
      || ACE_BIT_ENABLED (verbose_flag,
                          ACE_Log_Msg::VERBOSE_LITE))
    {
      ACE_Time_Value reftime (this->secs_, this->usecs_);
      if (0 == ACE::timestamp (reftime,
                               timestamp,
                               sizeof (timestamp) / sizeof (ACE_TCHAR)))
        return -1;

      // Historical timestamp in VERBOSE[_LITE] used 3 places for partial sec.
      // 012345678901234567890123456
      // 1989-10-18 14:25:36.123<nul>
      timestamp[23] = '\0';
    }

  if (ACE_BIT_ENABLED (verbose_flag,
                       ACE_Log_Msg::VERBOSE))
    {
      const ACE_TCHAR *lhost_name = ((host_name == 0)
                                      ? ACE_TEXT ("<local_host>")
                                      : host_name);
      ACE_OS::snprintf (verbose_msg, verbose_msg_size,
                       verbose_fmt,
                       timestamp,
                       lhost_name,
                       this->pid_,
                       ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_)),
                       this->msg_data_);
    }
  else if (ACE_BIT_ENABLED (verbose_flag, ACE_Log_Msg::VERBOSE_LITE))
    ACE_OS::snprintf (verbose_msg, verbose_msg_size,
                     verbose_lite_fmt,
                     timestamp,
                     ACE_Log_Record::priority_name (ACE_Log_Priority (this->type_)),
                     this->msg_data_);
  else
    ACE_OS::strcpy (verbose_msg, this->msg_data_);
  return 0;
}

inline bool
log_priority_enabled(ACE_Log_Category_TSS* category, ACE_Log_Priority priority)
{
  if (category && !category->log_priority_enabled (priority))
    return false;
  return ACE_LOG_MSG->log_priority_enabled (priority);
}

int
ACE_Log_Record::print (const ACE_TCHAR host_name[],
                       u_long verbose_flag,
                       FILE *fp)
{
  if ( log_priority_enabled(this->category(), ACE_Log_Priority (this->type_)) )
    {
      ACE_TCHAR *verbose_msg = 0;
#if defined (ACE_HAS_ALLOC_HOOKS)
      ACE_ALLOCATOR_RETURN (verbose_msg, static_cast<ACE_TCHAR *>(ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * MAXVERBOSELOGMSGLEN)), -1);
#else
      ACE_NEW_RETURN (verbose_msg, ACE_TCHAR[MAXVERBOSELOGMSGLEN], -1);
#endif /* ACE_HAS_ALLOC_HOOKS */

      int result = this->format_msg (host_name, verbose_flag, verbose_msg,
                                     MAXVERBOSELOGMSGLEN);

      if (result == 0)
        {
          if (fp != 0)
            {
              int const verbose_msg_len =
                static_cast<int> (ACE_OS::strlen (verbose_msg));
#if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
              int const fwrite_result = ACE_OS::fprintf (fp,
                                                         ACE_TEXT ("%ls"),
                                                         verbose_msg);
#else
              int const fwrite_result = ACE_OS::fprintf (fp,
                                                         ACE_TEXT ("%s"),
                                                         verbose_msg);
#endif
              // We should have written everything
              if (fwrite_result != verbose_msg_len)
                result = -1;
              else
                ACE_OS::fflush (fp);
            }
        }

#if defined (ACE_HAS_ALLOC_HOOKS)
      ACE_Allocator::instance()->free(verbose_msg);
#else
      delete [] verbose_msg;
#endif /* ACE_HAS_ALLOC_HOOKS */

      return result;
    }
  else
    return 0;
}

int
operator<< (ACE_OutputCDR &cdr,
            const ACE_Log_Record &log_record)
{
  // The written message length can't be more than 32 bits (ACE_CDR::ULong)
  // so reduce it here if needed.
  ACE_CDR::ULong u_msglen =
    ACE_Utils::truncate_cast<ACE_CDR::ULong> (log_record.msg_data_len ());

  // Insert each field from <log_record> into the output CDR stream.
  cdr << ACE_CDR::Long (log_record.type ());
  cdr << ACE_CDR::Long (log_record.pid ());
  cdr << ACE_CDR::LongLong (log_record.time_stamp ().sec ());
  cdr << ACE_CDR::Long (log_record.time_stamp ().usec ());
  cdr << u_msglen;
#if defined (ACE_USES_WCHAR)
  cdr.write_wchar_array (log_record.msg_data (), u_msglen);
#else
  cdr.write_char_array (log_record.msg_data (), u_msglen);
#endif /* ACE_USES_WCHAR */
  return cdr.good_bit ();
}

int
operator>> (ACE_InputCDR &cdr,
            ACE_Log_Record &log_record)
{
  ACE_CDR::Long type;
  ACE_CDR::Long pid;
  ACE_CDR::LongLong sec;
  ACE_CDR::Long usec;
  ACE_CDR::ULong buffer_len;

  // Extract each field from input CDR stream into <log_record>.
  if ((cdr >> type) && (cdr >> pid) && (cdr >> sec) && (cdr >> usec)
      && (cdr >> buffer_len)) {
    ACE_TCHAR *log_msg;
#if defined (ACE_HAS_ALLOC_HOOKS)
    ACE_ALLOCATOR_RETURN (log_msg, static_cast<ACE_TCHAR *> (ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * (buffer_len + 1))), -1);
#else
    ACE_NEW_RETURN (log_msg, ACE_TCHAR[buffer_len + 1], -1);
#endif /* ACE_HAS_ALLOC_HOOKS */
    ACE_Auto_Basic_Array_Ptr<ACE_TCHAR> log_msg_p (log_msg);
    log_record.type (type);
    log_record.pid (pid);
    log_record.time_stamp (ACE_Time_Value (ACE_Utils::truncate_cast<time_t> (sec),
                                           usec));
#if defined (ACE_USES_WCHAR)
    cdr.read_wchar_array (log_msg, buffer_len);
#else
    cdr.read_char_array (log_msg, buffer_len);
#endif /* ACE_USES_WCHAR */
    log_msg[buffer_len] = '\0';
    if (-1 == log_record.msg_data (log_msg))
      return -1;
  }
  return cdr.good_bit ();
}

#if !defined (ACE_LACKS_IOSTREAM_TOTALLY)

int
ACE_Log_Record::print (const ACE_TCHAR host_name[],
                       u_long verbose_flag,
                       ACE_OSTREAM_TYPE &s)
{
  if ( log_priority_enabled(this->category(), ACE_Log_Priority (this->type_)) )
    {
      ACE_TCHAR* verbose_msg = 0;
      ACE_NEW_RETURN (verbose_msg, ACE_TCHAR[MAXVERBOSELOGMSGLEN], -1);

      int const result = this->format_msg (host_name, verbose_flag, verbose_msg,
                                           MAXVERBOSELOGMSGLEN);

      if (result == 0)
        {
          // Since ostream expects only chars, we cannot pass wchar_t's
          s << ACE_TEXT_ALWAYS_CHAR (verbose_msg);
          s.flush ();
        }

      delete [] verbose_msg;

      return result;
    }
  return 0;
}

#endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */

ACE_END_VERSIONED_NAMESPACE_DECL