summaryrefslogtreecommitdiff
path: root/ace/Log_Record.cpp
blob: a463981014f6851485818041e4a67ddb7a001e01 (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
// Log_Record.cpp
// $Id$

#define ACE_BUILD_DLL
#include "ace/Log_Record.h"

ACE_ALLOC_HOOK_DEFINE(ACE_Log_Record)

void
ACE_Log_Record::dump (void) const
{
  // ACE_TRACE ("ACE_Log_Record::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  ACE_DEBUG ((LM_DEBUG, "type_ = %d\n", this->type_));
  ACE_DEBUG ((LM_DEBUG, "\nlength_ = %d\n", this->length_));
  ACE_DEBUG ((LM_DEBUG, "\ntime_stamp_ = (%d, %d)\n", this->time_stamp_.sec (), this->time_stamp_.usec ()));
  ACE_DEBUG ((LM_DEBUG, "\npid_ = %d\n", this->pid_));
  ACE_DEBUG ((LM_DEBUG, "\nmsg_data_ = %s\n", this->msg_data_));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

void 
ACE_Log_Record::msg_data (const char *data)
{
  // ACE_TRACE ("ACE_Log_Record::msg_data");
  ACE_OS::strncpy (this->msg_data_, data, ACE_Log_Record::MAXLOGMSGLEN);
  this->round_up ();
}

ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp, 
				long ts_sec,
				long p)
  : type_ (long (lp)), 
    length_ (0),
    time_stamp_ (ts_sec), 
    pid_ (p)
{
  // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
}

ACE_Log_Record::ACE_Log_Record (ACE_Log_Priority lp, 
				const ACE_Time_Value &ts,
				long p)
  : type_ (long (lp)), 
    length_ (0),
    time_stamp_ (ts), 
    pid_ (p)
{
  // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
}

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

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

ACE_Log_Record::ACE_Log_Record (void)
  : type_ (0),
    length_ (0),
    time_stamp_ (0),
    pid_ (0)
{
  // ACE_TRACE ("ACE_Log_Record::ACE_Log_Record");
}

// Print out the record on the stderr stream with the appropriate
// format.

int
ACE_Log_Record::print (const char host_name[],
		       int verbose, 
		       FILE *fp)
{
  // ACE_TRACE ("ACE_Log_Record::print");

  int ret;

  if (verbose)
    {
      time_t now = this->time_stamp_.sec ();
      char ctp[26]; // 26 is a magic number...

      if (ACE_OS::ctime_r (&now, ctp, sizeof ctp) == 0)
	return -1;

      /* 01234567890123456789012345 */
      /* Wed Oct 18 14:25:36 1989n0 */

      ctp[19] = '\0'; // NUL-terminate after the time.
      ctp[24] = '\0'; // NUL-terminate after the date.

      if (host_name == 0)
	host_name = "<local_host>";

      ret =  ACE_OS::fprintf (fp, "%s.%d%s@%s@%d@%d@%s",
			      ctp + 4, 
			      this->time_stamp_.usec () / 1000,
			      ctp + 20, 
			      host_name, 
			      this->pid_,
			      this->type_, 
			      this->msg_data_);
    }
  else
    ret =  ACE_OS::fprintf (fp, "%s", this->msg_data_);

  if (ret > 0)
    ACE_OS::fflush (fp);
  return ret;
}

int
ACE_Log_Record::print (const char host_name[],
		       int verbose, 
		       ostream &s)
{
  // ACE_TRACE ("ACE_Log_Record::print");

  if (verbose)
    {
      time_t now = this->time_stamp_.sec ();
      char ctp[26]; // 26 is a magic number...

      if (ACE_OS::ctime_r (&now, ctp, sizeof ctp) == 0)
	return -1;

      /* 01234567890123456789012345 */
      /* Wed Oct 18 14:25:36 1989n0 */

      ctp[19] = '\0'; // NUL-terminate after the time.
      ctp[24] = '\0'; // NUL-terminate after the date.

      if (host_name == 0)
	host_name = "<local_host>";

      s << (ctp + 4) << '.'
	// The following line isn't portable, so I've commented it out...
	// << setw (3) << setfill ('0') << this->time_stamp_.usec () / 1000 << ' '
	<< this->time_stamp_.usec () / 1000 << ' '
	<< (ctp + 20)
	<< '@'
	<< host_name
	<< '@'
	<< this->pid_
	<< '@'
	<< this->type_
	<< '@';
    }

  s << this->msg_data_;
  s.flush ();
  return 0;
}