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
|
/*****************************************************************************
* 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 *channel,
const char *format, ...)
{
va_list args;
va_start( args, format );
PublishVA( loc, channel, format, args );
va_end( args );
}
void RLogPublisher::PublishVA( PublishLoc *loc, RLogChannel *,
const char *format, va_list ap )
{
// 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];
char *buf = msgBuf;
size_t bufSize = sizeof(msgBuf);
// loop until we have allocated enough space for the message
for(int numTries = 10; numTries; --numTries)
{
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...
//
// FIXME: memcpy doesn't work for compilers which use array type for
// va_list such as Watcom
#if defined( va_copy )
va_copy( args, ap );
#elif defined( __va_copy )
__va_copy( args, ap );
#else
memcpy( &args, &ap, sizeof(va_list) );
#endif
int ncpy = vsnprintf( buf , bufSize, format, args );
va_end( args );
// if it worked, then return the buffer
if( ncpy > -1 && ncpy < bufSize )
{
data.msg = buf;
break;
} else
{
// newer implementations of vsnprintf return # bytes needed..
if(ncpy > 0)
bufSize = ncpy + 1;
else
bufSize *= 2; // try twice as much space as before
if(buf != msgBuf)
delete[] buf;
buf = new char[bufSize];
}
}
loc->pub->publish( data );
if(buf != msgBuf)
delete[] buf;
}
|