[go: up one dir, main page]

File: RLogPublisher.cpp

package info (click to toggle)
rlog 1.3.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,632 kB
  • ctags: 519
  • sloc: sh: 11,005; cpp: 1,508; makefile: 127
file content (215 lines) | stat: -rw-r--r-- 5,422 bytes parent folder | download
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
/*****************************************************************************
 * Author:   Valient Gough <vgough@pobox.com>
 *
 *****************************************************************************
 * Copyright (c) 2004, Valient Gough
 *
 * This library is free software; you can distribute it and/or modify it under
 * the terms of the GNU Lesser General Public License (LGPL), as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the LGPL in the file COPYING for more
 * details.
 *
 */
		                                                                                
                             

#include "RLogPublisher.h"

#include "RLogChannel.h"
#include "rloglocation.h"
#include "rlog.h"

#include <stdio.h>
#include <stdarg.h>
#include <string.h> // in case we need memcpy

using namespace rlog;


/*! @class rlog::RLogPublisher <rlog/RLogPublisher.h>
  @brief RLog publisher used by rLog macros.

  This derives from RLogNode and interfaces to the static PublishLoc logging
  data allowing them to be enabled or disabled depending on subscriber
  status.

  An instance of this class is created for every error message location.
  Normally this class is not used directly.  

  For example, this
  @code
     rDebug( "hello world" );
  @endcode

  is turned approximatly into this:
  @code
     static PublishLoc _rl = {
	 publish:  & rlog::RLog_Register ,
	 pub: 0,
	 component: "component",
	 fileName: "myfile.cpp",
	 functionName: "functionName()",
	 lineNum: __LINE__,
	 channel: 0
     };
     if(_rL.publish != 0)
	 (*_rl.publish)( &_rL, _RLDebugChannel, "hello world" );
  @endcode

  The RLogPublisher instance manages the contents of the static structure
  _rL.  When someone subscribes to it's message, then _rL.publish is set to
  point to the publishing function, and when there are no subscribers then
  it is set to 0.

  The code produced contains one if statement, and with optimization comes
  out to about 3 instructions on an x86 computer (not including the
  function call).  If there are no subscribers to this message then that
  is all the overhead, plus the memory usage for the structures
  involved and the initial registration when the statement is first
  encountered..

  @see RLogChannel
  @author Valient Gough
 */


RLogPublisher::RLogPublisher()
{
}

RLogPublisher::RLogPublisher(PublishLoc *loc) 
    : RLogNode()
    , src( loc )
{
    // link to channel for channel based subscriptions
    // Lookup the componentized version
    RLogNode *channelNode = GetComponentChannel( src->component, 
	    src->channel->name().c_str(), src->channel->logLevel() );
    channelNode->addPublisher( this );

    // link to file
    RLogNode *fileNode = FileNode::Lookup( src->component, src->fileName );
    fileNode->addPublisher( this );
}

RLogPublisher::~RLogPublisher()
{
    clear();
}

void
RLogPublisher::setEnabled(bool active)
{
    if(src)
	src->publish = active ? RLogPublisher::Publish : 0;
}

void RLogPublisher::Publish( PublishLoc *loc, RLogChannel *,
	const char *format, ...)
{
    // check just in case it got set to 0 before our call... unlikely
    if(unlikely(loc->publish == 0))
	return;

    RLogData data;

    data.publisher = loc;
    data.time = time(0);
    data.msg = 0;

    char msgBuf[64];
    va_list args;
    va_start( args, format );

    // glibc since 2.1 returns the size the buffer needs to be.
    // TODO: should check in configure or defines for the version number.
    int ncpy = vsnprintf( msgBuf, sizeof(msgBuf), format, args );

    va_end( args );

    char *buf = 0;
    if(ncpy < (int)sizeof(msgBuf))
    {
	if(ncpy > 0)
	{
	    data.msg = msgBuf;
	} else
	    data.msg = "[FMT ERROR]: failed to format msg";
    } else
    {
	// the buffer wasn't large enough, allocate a temporary
	int len = ncpy + 1;

	buf = new char[len];

	va_start( args, format );
	vsnprintf( buf, len, format, args );
	va_end( args );

	data.msg = buf;
    }

    loc->pub->publish( data );

    if(buf)
	delete[] buf;
}

void RLogPublisher::PublishVA( PublishLoc *loc, RLogChannel *,
	const char *format, va_list ap )
{
    RLogData data;

    data.publisher = loc;
    data.time = time(0);
    data.msg = 0;

    char msgBuf[64];
    va_list args;

    // va_copy() is defined in C99, __va_copy() in earlier definitions, and
    // automake says to fall back on memcpy if neither exist...
#if defined( va_copy )
    va_copy( args, ap );
#elif defined( __va_copy )
    __va_copy( args, ap );
#else
    memcpy( &args, &ap, sizeof(va_list) );
#endif

    // glibc since 2.1 returns the size the buffer needs to be.
    // TODO: should check in configure or defines for the version number.
    int ncpy = vsnprintf( msgBuf, sizeof(msgBuf), format, args );

    va_end( args );

    char *buf = 0;
    if(ncpy < (int)sizeof(msgBuf))
    {
	if(ncpy > 0)
	{
	    data.msg = msgBuf;
	} else
	    data.msg = "[FMT ERROR]: failed to format msg";
    } else
    {
	// the buffer wasn't large enough, allocate a temporary
	int len = ncpy + 1;

	buf = new char[len];

	vsnprintf( buf, len, format, ap );

	data.msg = buf;
    }

    loc->pub->publish( data );

    if(buf)
	delete[] buf;
}