[go: up one dir, main page]

File: RLogChannel.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 (242 lines) | stat: -rw-r--r-- 6,745 bytes parent folder | download | duplicates (2)
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
/*****************************************************************************
 * Author:   Valient Gough <vgough@pobox.com>
 *
 *****************************************************************************
 * Copyright (c) 2002-2003, 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 "RLogChannel.h"
#include "rlog.h"
#include "Lock.h"

using namespace std;
using namespace rlog;

const char GlobalComponent[] = "/";

namespace rlog
{
    static RLogChannel *gRootChannel =0;

    // big lock around channel lookups..
    static Mutex gChannelLock;
  
    // Use GetComponentChannel here because we want to reference the global
    // versions, not the componentized versions..
    // We'll use 
    RLogChannel *_RLDebugChannel = GetGlobalChannel( "debug", Log_Debug );
    RLogChannel *_RLInfoChannel = GetGlobalChannel( "info", Log_Info );
    RLogChannel *_RLWarningChannel = GetGlobalChannel( "warning", Log_Warning );
    RLogChannel *_RLErrorChannel = GetGlobalChannel( "error", Log_Error );
}


/*! @class rlog::RLogChannel <rlog/RLogChannel.h>
  @brief Implements a hierarchical logging channel

  You should not need to use RLogChannel directly under normal
  circumstances. See RLOG_CHANNEL() macro, GetComponentChannel() and
  GetGlobalChannel()

  RLogChannel implements channel logging support.  A channel is a named
  logging location which is global to the program.  Channels are
  hierarchically related.
  
  For example, if somewhere in your program a message is logged to
  "debug/foo/bar", then it will be delived to any subscribers to
  "debug/foo/bar", or subscribers to "debug/foo", or subscribers to
  "debug".   Subscribing to a channel means you will receive anything
  published on that channel or sub-channels.

  As a special case, subscribing to the channel "" means you will receive
  all messages - as every message has a channel and the empty string "" is
  considered to mean the root of the channel tree.

  In addition, componentized channels are all considered sub channels of
  the global channel hierarchy.  All rDebug(), rWarning(), and rError()
  macros publish to the componentized channels (component defined by
  RLOG_COMPONENT).

  @code
      // get the "debug" channel for our component.  This is the same as
      // what rDebug() publishes to.
      RLogChannel *node = RLOG_CHANNEL( "debug", Log_Debug );
      // equivalent to
      RLogChannel *node = GetComponentChannel( RLOG_COMPONENT, "debug" );

      // Or, get the global "debug" channel, which will have messages from
      // *all* component's "debug" channels.
      RLogChannel *node = GetGlobalChannel( "debug", Log_Debug );
  @endcode

  @author Valient Gough
  @see RLOG_CHANNEL()
  @see GetComponentChannel()
  @see GetGlobalChannel()
*/



RLogChannel::RLogChannel( const string &n, LogLevel level )
    : RLogNode()
    , _name( n )
    , _level( level )
{
}

RLogChannel::~RLogChannel()
{
}

const std::string &RLogChannel::name() const
{
    return _name;
}

LogLevel RLogChannel::logLevel() const
{
    return _level;
}

void RLogChannel::setLogLevel(LogLevel level)
{
    _level = level;
}

RLogChannel *RLogChannel::getComponent( RLogChannel *parent,
	const char *component )
{
    ComponentMap::const_iterator it = components.find( component );
    if(it == components.end())
    {
	RLogChannel *ch = new RLogChannel( _name, _level );
	components.insert( make_pair( component, ch ) );

	// connect to its parent
	if(parent)
	    parent->addPublisher( ch );

	// connect to globalized version
	addPublisher( ch );

	return ch;
    } else
    {
	return it->second;
    }
}

/*! @relates RLogChannel
  @brief Return the named channel across all components.

  Channels are hierarchical.  See RLogChannel for more detail.
  The global channel contains messages for all component channels.

  For example, subscribing to the global "debug" means the subscriber would
  also get messages from <Component , "debug">, and <Component-B, "debug">, and
  <Component-C, "debug/foo">, etc.

  @author Valient Gough
*/
RLogChannel *rlog::GetGlobalChannel( const char *path, LogLevel level )
{
    return GetComponentChannel( GlobalComponent, path, level );
}

/*!  @relates RLogChannel
  @brief Return the named channel for a particular component.

  @author Valient Gough
*/
RLogChannel *rlog::GetComponentChannel( const char *component, 
	const char *path, LogLevel level )
{
    // since we much with the globally visible channel tree, hold a lock..
    Lock lock( &gChannelLock );

    string currentPath;

    if(!gRootChannel)
	gRootChannel = new RLogChannel( "", level );

    RLogChannel *current = gRootChannel;
    RLogChannel *currentComponent = 0;
    if(strcmp( component, GlobalComponent ) != 0)
	currentComponent = gRootChannel->getComponent( 0, component );

    while( *path )
    {
	// if log level is currently undefined but we now know what it is, then
	// define it..
	if((current->logLevel() == Log_Undef) && (level != Log_Undef))
	    current->setLogLevel( level );

	char *next = strchr( path , '/' );
	size_t len = next ? next - path : strlen( path );

	if(len > 1)
	{
	    string pathEl( path, len );

	    if(!currentPath.empty())
		currentPath += '/';
	    currentPath += pathEl;

	    ComponentMap::const_iterator it = 
		current->subChannels.find( pathEl );
	    if(it != current->subChannels.end())
	    {
		// found.  possibly creating sub-map
		current = it->second;
	    } else
	    {
		// create
		RLogChannel *nm = new RLogChannel( currentPath, level );
		current->subChannels.insert( make_pair(pathEl, nm) );

		current->addPublisher( nm );

		current = nm;
	    }
	    // track componentized version
	    if(currentComponent)
    		currentComponent = current->getComponent( currentComponent,
			component );

	    path += len;
	} else
	{
	    // skip separator character
	    ++path;
	}
    }

    if(currentComponent)
	return currentComponent;
    else
	return current;
}

void
RLogChannel::publish(const RLogData &data)
{
    set< RLogNode* >::const_iterator it = data.seen.find( this );
    if(it == data.seen.end())
    {
	const_cast<RLogData&>(data).seen.insert( this );
	RLogNode::publish( data );
    }
}