Skip to content

Commit

Permalink
add PublishLoc destructor, fixing issue #3 (see http://code.google.co…
Browse files Browse the repository at this point in the history
…m/p/rlog).

Cost of initializing PublishLoc is hidden by delaying initialization until
after testing the enable flag.
  • Loading branch information
valient committed Jun 6, 2008
1 parent be42a91 commit 2bbd617
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 35 deletions.
39 changes: 23 additions & 16 deletions rlog/RLogPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,21 @@ using namespace rlog;
is turned approximatly into this:
@code
static PublishLoc _rl = {
enabled: true,
publish: & rlog::RLog_Register ,
pub: 0,
component: "component",
fileName: "myfile.cpp",
functionName: "functionName()",
lineNum: __LINE__,
channel: 0
};
static bool _rl_enabled = true;
if(_rL.enabled)
{
static PublishLoc _rl = {
enabled: & _rl_enabled,
publish: & rlog::RLog_Register ,
pub: 0,
component: "component",
fileName: "myfile.cpp",
functionName: "functionName()",
lineNum: __LINE__,
channel: 0
};
(*_rl.publish)( &_rL, _RLDebugChannel, "hello world" );
}
@endcode
The RLogPublisher instance manages the contents of the static structure
Expand All @@ -68,11 +71,10 @@ using namespace rlog;
enabled flag is set to false.
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..
out to 2 instructions on an x86 computer for the unsubscribed case.
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
Expand Down Expand Up @@ -107,7 +109,12 @@ void
RLogPublisher::setEnabled(bool active)
{
if(src)
src->enabled = active;
{
if(active)
src->enable();
else
src->disable();
}
}

void RLogPublisher::Publish( PublishLoc *loc, RLogChannel *channel,
Expand Down
30 changes: 22 additions & 8 deletions rlog/rlog-c99.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,40 @@
*/


/*! @def _rTriggerDef
Defines a static RLogPublisher and points it to the registration function for
the first call.
@internal
*/
#define _rTriggerDef(ID) \
static bool ID ## _enabled = true;

/*! @def _rMessageDef
Defines a static RLogPublisher and points it to the registration function for
the first call.
@internal
*/
#define _rMessageDef(ID, COMPONENT) \
static rlog::PublishLoc ID ={true, &rlog::RLog_Register, 0, STR(COMPONENT), \
__FILE__, __FUNCTION__, __LINE__, 0};
static rlog::PublishLoc ID RLOG_SECTION = {& ID ## _enabled, \
&rlog::RLog_Register, 0, STR(COMPONENT), __FILE__, \
__FUNCTION__, __LINE__, 0};

/*! @def _rMessageCall
Checks if the RLogPublisher is enabled and publishes the message if so.
@internal
*/
#if HAVE_PRINTF_FP || !HAVE_PRINTF_ATTR
# define _rMessageCall(ID, CHANNEL, ...) \
if(unlikely(ID.enabled)) (*ID.publish)( &ID, CHANNEL, ##__VA_ARGS__ );
# define _rMessageCall(ID, COMPONENT, CHANNEL, ...) \
if(unlikely(ID ## _enabled)) \
{ \
_rMessageDef(ID, COMPONENT) \
(*ID.publish)( &ID, CHANNEL, ##__VA_ARGS__ ); \
}
#else // no PRINTF attributes..
# define _rMessageCall(ID, CHANNEL, ...) \
if(unlikely(ID.enabled)) \
# define _rMessageCall(ID, COMPONENT, CHANNEL, ...) \
if(unlikely(ID ## _enabled)) \
{ \
_rMessageDef(ID, COMPONENT) \
(*ID.publish)( &ID, CHANNEL, ##__VA_ARGS__ ); \
rlog::__checkArgs( 0, ##__VA_ARGS__ ); \
}
Expand All @@ -52,8 +66,8 @@
@internal
*/
#define _rMessage(ID, CHANNEL, ... ) \
do { _rMessageDef(ID, RLOG_COMPONENT) \
_rMessageCall(ID, CHANNEL, ##__VA_ARGS__ ) } while(0)
do { _rTriggerDef(ID) \
_rMessageCall(ID, RLOG_COMPONENT, CHANNEL, ##__VA_ARGS__ ) } while(0)

/*! @addtogroup RLogMacros
These macros are the primary interface for logging messages:
Expand Down
6 changes: 4 additions & 2 deletions rlog/rlog-novariadic.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ class RLOG_DECL _rMessageProxy
};

#define _rMessageDef(ID, COMPONENT) \
static rlog::PublishLoc ID ={true, &rlog::RLog_Register, 0, STR(COMPONENT), \
__FILE__, __FUNCTION__, __LINE__, 0}
static bool ID ## _enabled = true; \
static rlog::PublishLoc ID RLOG_SECTION = {& ID ## _enabled, \
&rlog::RLog_Register, 0, STR(COMPONENT), __FILE__, \
__FUNCTION__, __LINE__, 0}

#define _rMessage(ID, CHANNEL, COMPONENT) \
_rMessageDef(ID, COMPONENT); \
Expand Down
10 changes: 6 additions & 4 deletions rlog/rlog-prec99.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
*/

#define _rMessageDef(ID, COMPONENT) \
static rlog::PublishLoc ID ={true, &rlog::RLog_Register, 0, STR(COMPONENT), \
__FILE__, __FUNCTION__, __LINE__, 0};
static bool ID ## _enabled = true; \
static rlog::PublishLoc ID RLOG_SECTION = {& ID ## _enabled, \
&rlog::RLog_Register, 0, STR(COMPONENT), __FILE__, \
__FUNCTION__, __LINE__, 0};


#if HAVE_PRINTF_FP || !HAVE_PRINTF_ATTR
# define _rMessageCall(ID, CHANNEL, ARGS...) \
if(unlikely(ID.enabled)) (*ID.publish)( &ID, CHANNEL, ##ARGS );
if(unlikely(ID ## _enabled)) (*ID.publish)( &ID, CHANNEL, ##ARGS );
#else
# define _rMessageCall(ID, CHANNEL, ARGS...) \
if(unlikely(ID.enabled)) \
if(unlikely(ID ## _enabled)) \
{ \
(*ID.publish)( &ID, CHANNEL, ##ARGS ); \
rlog::__checkArgs( 0, ##ARGS ); \
Expand Down
21 changes: 17 additions & 4 deletions rlog/rlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ int RLogVersion()
return CURRENT_RLOG_VERSION;
}

PublishLoc::~PublishLoc()
{
disable();
if(pub != NULL)
{
delete pub;
pub = NULL;
}
}

void rlog::RLog_Register(PublishLoc *loc, RLogChannel *channel,
const char *format, ... )
{
Expand All @@ -55,7 +65,7 @@ void rlog::RLog_Register(PublishLoc *loc, RLogChannel *channel,

if(pub->enabled())
{
loc->enabled = true;
loc->enable();

// pass through to the publication function since it is active at
// birth.
Expand All @@ -64,7 +74,7 @@ void rlog::RLog_Register(PublishLoc *loc, RLogChannel *channel,
RLogPublisher::PublishVA( loc, channel, format, args );
va_end( args );
} else
loc->enabled = false;
loc->disable();
}

/*
Expand Down Expand Up @@ -106,10 +116,13 @@ void _rMessageProxy::doLog(const char *format, va_list ap)
RLogPublisher *pub = new RLogPublisher(loc);
loc->pub = pub;

loc->enabled = pub->enabled();
if(pub->enabled())
loc->enable();
else
loc->disable();
}

if(loc->enabled)
if(loc->isEnabled())
RLogPublisher::PublishVA( loc, loc->channel, format, ap );
}

Expand Down
8 changes: 7 additions & 1 deletion rlog/rlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ namespace rlog
*/
struct PublishLoc
{
bool enabled;
bool *enabled;
// If the compiler supports printf attribute specification on function
// pointers, we'll use it here so that the compiler knows to check for
// proper printf formatting. If it doesn't support it, then we'll
Expand All @@ -166,6 +166,12 @@ namespace rlog
const char *functionName;
int lineNum;
RLogChannel *channel;

inline void enable() { *enabled = true; }
inline void disable() { *enabled = false; }
inline bool isEnabled() { return *enabled; }

~PublishLoc();
};

/*! @fn RLog_Register
Expand Down

0 comments on commit 2bbd617

Please sign in to comment.