Category.cpp
Go to the documentation of this file.
1 /*
2  * Category.cpp
3  *
4  * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
5  * Copyright 2000, Bastiaan Bakker. All rights reserved.
6  *
7  * See the COPYING file for the terms of usage and distribution.
8  */
9 
10 #include "PortabilityImpl.hh"
11 
12 #ifdef LOG4CPP_HAVE_UNISTD_H
13 # include <unistd.h>
14 #endif
15 
16 #include <log4cpp/Category.hh>
18 #include <log4cpp/NDC.hh>
19 #include "StringUtil.hh"
20 
21 namespace log4cpp {
22 
24  return getInstance("");
25  }
26 
28  getRoot().setPriority(priority);
29  }
30 
32  return getRoot().getPriority();
33  }
34 
35  Category& Category::getInstance(const std::string& name) {
37  }
38 
39  Category* Category::exists(const std::string& name) {
41  }
42 
43  std::vector<Category*>* Category::getCurrentCategories() {
46  }
47 
50  }
51 
52  Category::Category(const std::string& name, Category* parent, Priority::Value priority) :
53  _name(name),
54  _parent(parent),
55  _priority(priority),
56  _isAdditive(true) {
57  }
58 
61  }
62 
63  const std::string& Category::getName() const throw() {
64  return _name;
65  }
66 
68  return _priority;
69  }
70 
72  throw(std::invalid_argument) {
73  if ((priority < Priority::NOTSET) || (getParent() != NULL)) {
74  _priority = priority;
75  } else {
76  /* caller tried to set NOTSET priority to root Category.
77  Bad caller!
78  */
79  throw std::invalid_argument("cannot set priority NOTSET on Root Category");
80  }
81  }
82 
84  // REQUIRE(rootCategory->getPriority() != Priority::NOTSET)
85 
86  const Category* c = this;
87  while(c->getPriority() >= Priority::NOTSET) {
88  c = c->getParent();
89  }
90 
91  return c->getPriority();
92  }
93 
95  throw(std::invalid_argument) {
96  if (appender) {
98  {
99  AppenderSet::iterator i = _appender.find(appender);
100  if (_appender.end() == i) {
101  // not found
102  _appender.insert(appender);
103  _ownsAppender[appender] = true;
104  }
105  }
106  } else {
107  throw std::invalid_argument("NULL appender");
108  }
109  }
110 
111  void Category::addAppender(Appender& appender) {
113  {
114  AppenderSet::iterator i = _appender.find(&appender);
115  if (_appender.end() == i) {
116  _appender.insert(&appender);
117  _ownsAppender[&appender] = false;
118  }
119  }
120  }
121 
124  {
125  AppenderSet::const_iterator i = _appender.begin();
126  return (_appender.end() == i) ? NULL : *i;
127  }
128  }
129 
130  Appender* Category::getAppender(const std::string& name) const {
132  {
133  AppenderSet::const_iterator i = _appender.begin();
134  if (_appender.end() != i) {
135  // found
136  return((*i)->getAppender(name));
137  }
138  else {
139  return(NULL);
140  }
141  }
142  }
143 
146  {
147  return _appender;
148  }
149  }
150 
153  {
154  for (AppenderSet::iterator i = _appender.begin();
155  i != _appender.end(); i++) {
156  // found
157  OwnsAppenderMap::iterator i2;
158  if (ownsAppender(*i, i2)) {
159  delete (*i);
160  }
161  }
162 
163  _ownsAppender.clear();
164  _appender.clear();
165  }
166  }
167 
170  {
171  AppenderSet::iterator i = _appender.find(appender);
172  if (_appender.end() != i) {
173  OwnsAppenderMap::iterator i2;
174  if (ownsAppender(*i, i2)) {
175  _ownsAppender.erase(i2);
176  delete (*i);
177  }
178  _appender.erase(i);
179  } else {
180  // appender not found
181  }
182  }
183  }
184 
185  bool Category::ownsAppender(Appender* appender) const throw() {
186  bool owned = false;
187 
189  {
190  if (NULL != appender) {
191  OwnsAppenderMap::const_iterator i =
192  _ownsAppender.find(appender);
193  if (_ownsAppender.end() != i) {
194  owned = (*i).second;
195  }
196  }
197  }
198 
199  return owned;
200  }
201 
202  /* assume lock is held */
203  bool Category::ownsAppender(Appender* appender,
204  Category::OwnsAppenderMap::iterator& i2) throw() {
205  bool owned = false;
206 
207  if (NULL != appender) {
208  OwnsAppenderMap::iterator i = _ownsAppender.find(appender);
209  if (_ownsAppender.end() != i) {
210  owned = (*i).second;
211  if (owned) {
212  i2 = i;
213  }
214  }
215  }
216 
217  return owned;
218  }
219 
220  void Category::callAppenders(const LoggingEvent& event) throw() {
222  {
223  if (!_appender.empty()) {
224  for(AppenderSet::const_iterator i = _appender.begin();
225  i != _appender.end(); i++) {
226  (*i)->doAppend(event);
227  }
228  }
229  }
230  if (getAdditivity() && (getParent() != NULL)) {
231  getParent()->callAppenders(event);
232  }
233  }
234 
235  void Category::setAdditivity(bool additivity) {
236  _isAdditive = additivity;
237  }
238 
239  bool Category::getAdditivity() const throw() {
240  return _isAdditive;
241  }
242 
244  return _parent;
245  }
246 
247  const Category* Category::getParent() const throw() {
248  return _parent;
249  }
250 
252  const char* format,
253  va_list arguments) throw() {
254  _logUnconditionally2(priority, StringUtil::vform(format, arguments));
255  }
256 
258  const std::string& message) throw() {
259  LoggingEvent event(getName(), message, NDC::get(), priority);
260  callAppenders(event);
261  }
262 
263  bool Category::isPriorityEnabled(Priority::Value priority) const throw() {
264  return(getChainedPriority() >= priority);
265  }
266 
268  const char* stringFormat, ...) throw() {
269  if (isPriorityEnabled(priority)) {
270  va_list va;
271  va_start(va, stringFormat);
272  _logUnconditionally(priority, stringFormat, va);
273  va_end(va);
274  }
275  }
276 
278  const std::string& message) throw() {
279  if (isPriorityEnabled(priority))
280  _logUnconditionally2(priority, message);
281  }
282 
284  const char* stringFormat,
285  va_list va) throw() {
286  if (isPriorityEnabled(priority)) {
287  _logUnconditionally(priority, stringFormat, va);
288  }
289  }
290 
291  void Category::debug(const char* stringFormat, ...) throw() {
293  va_list va;
294  va_start(va,stringFormat);
295  _logUnconditionally(Priority::DEBUG, stringFormat, va);
296  va_end(va);
297  }
298  }
299 
300  void Category::debug(const std::string& message) throw() {
303  }
304 
305  void Category::info(const char* stringFormat, ...) throw() {
307  va_list va;
308  va_start(va,stringFormat);
309  _logUnconditionally(Priority::INFO, stringFormat, va);
310  va_end(va);
311  }
312  }
313 
314  void Category::info(const std::string& message) throw() {
317  }
318 
319  void Category::notice(const char* stringFormat, ...) throw() {
321  va_list va;
322  va_start(va,stringFormat);
323  _logUnconditionally(Priority::NOTICE, stringFormat, va);
324  va_end(va);
325  }
326  }
327 
328  void Category::notice(const std::string& message) throw() {
331  }
332 
333  void Category::warn(const char* stringFormat, ...) throw() {
335  va_list va;
336  va_start(va,stringFormat);
337  _logUnconditionally(Priority::WARN, stringFormat, va);
338  va_end(va);
339  }
340  }
341 
342  void Category::warn(const std::string& message) throw() {
345  }
346 
347  void Category::error(const char* stringFormat, ...) throw() {
349  va_list va;
350  va_start(va,stringFormat);
351  _logUnconditionally(Priority::ERROR, stringFormat, va);
352  va_end(va);
353  }
354  }
355 
356  void Category::error(const std::string& message) throw() {
359  }
360 
361  void Category::crit(const char* stringFormat, ...) throw() {
363  va_list va;
364  va_start(va,stringFormat);
365  _logUnconditionally(Priority::CRIT, stringFormat, va);
366  va_end(va);
367  }
368  }
369 
370  void Category::crit(const std::string& message) throw() {
373  }
374 
375  void Category::alert(const char* stringFormat, ...) throw() {
377  va_list va;
378  va_start(va,stringFormat);
379  _logUnconditionally(Priority::ALERT, stringFormat, va);
380  va_end(va);
381  }
382  }
383 
384  void Category::alert(const std::string& message) throw() {
387  }
388 
389  void Category::emerg(const char* stringFormat, ...) throw() {
391  va_list va;
392  va_start(va,stringFormat);
393  _logUnconditionally(Priority::EMERG, stringFormat, va);
394  va_end(va);
395  }
396  }
397 
398  void Category::emerg(const std::string& message) throw() {
401  }
402 
403  void Category::fatal(const char* stringFormat, ...) throw() {
405  va_list va;
406  va_start(va,stringFormat);
407  _logUnconditionally(Priority::FATAL, stringFormat, va);
408  va_end(va);
409  }
410  }
411 
412  void Category::fatal(const std::string& message) throw() {
415  }
416 
418  return CategoryStream(*this, isPriorityEnabled(priority) ?
419  priority : Priority::NOTSET);
420  }
421 
423  return getStream(priority);
424  }
425 }
426 
void notice(const char *stringFormat,...)
Definition: Category.cpp:319
Category(const std::string &name, Category *parent, Priority::Value priority=Priority::NOTSET)
Definition: Category.cpp:52
virtual void callAppenders(const LoggingEvent &event)
Definition: Category.cpp:220
virtual AppenderSet getAllAppenders() const
Definition: Category.cpp:144
void debug(const char *stringFormat,...)
Definition: Category.cpp:291
OwnsAppenderMap _ownsAppender
Definition: Category.hh:660
virtual Category & getInstance(const std::string &name)
void fatal(const char *stringFormat,...)
Definition: Category.cpp:403
virtual Category * getParent()
Definition: Category.cpp:243
Category * _parent
Definition: Category.hh:634
void warn(const char *stringFormat,...)
Definition: Category.cpp:333
static std::string vform(const char *format, va_list args)
Definition: StringUtil.cpp:37
static Priority::Value getRootPriority()
Definition: Category.cpp:31
virtual CategoryStream operator<<(Priority::Value priority)
Definition: Category.cpp:422
virtual Appender * getAppender() const
Definition: Category.cpp:122
virtual void setAdditivity(bool additivity)
Definition: Category.cpp:235
const std::string _name
Definition: Category.hh:628
virtual void log(Priority::Value priority, const char *stringFormat,...)
Definition: Category.cpp:267
static Category & getInstance(const std::string &name)
Definition: Category.cpp:35
AppenderSet _appender
Definition: Category.hh:652
static void shutdown()
Definition: Category.cpp:48
virtual bool ownsAppender() const
Definition: Category.hh:226
void crit(const char *stringFormat,...)
Definition: Category.cpp:361
virtual Priority::Value getChainedPriority() const
Definition: Category.cpp:83
static const std::string & get()
Definition: NDC.cpp:42
virtual Category * getExistingInstance(const std::string &name)
virtual void logva(Priority::Value priority, const char *stringFormat, va_list va)
Definition: Category.cpp:283
static void setRootPriority(Priority::Value priority)
Definition: Category.cpp:27
virtual void removeAllAppenders()
Definition: Category.cpp:151
threading::Mutex _appenderSetMutex
Definition: Category.hh:653
static Category & getRoot()
Definition: Category.cpp:23
class LOG4CPP_EXPORT CategoryStream
virtual const std::string & getName() const
Definition: Category.cpp:63
volatile bool _isAdditive
Definition: Category.hh:666
virtual void removeAppender(Appender *appender)
Definition: Category.cpp:168
virtual void _logUnconditionally(Priority::Value priority, const char *format, va_list arguments)
Definition: Category.cpp:251
std::set< Appender * > AppenderSet
Definition: Appender.hh:147
virtual bool getAdditivity() const
Definition: Category.cpp:239
virtual Priority::Value getPriority() const
Definition: Category.cpp:67
virtual void setPriority(Priority::Value priority)
Definition: Category.cpp:71
virtual bool isPriorityEnabled(Priority::Value priority) const
Definition: Category.cpp:263
static std::vector< Category * > * getCurrentCategories()
Definition: Category.cpp:43
void alert(const char *stringFormat,...)
Definition: Category.cpp:375
static HierarchyMaintainer & getDefaultMaintainer()
void info(const char *stringFormat,...)
Definition: Category.cpp:305
static Category * exists(const std::string &name)
Definition: Category.cpp:39
virtual ~Category()
Definition: Category.cpp:59
volatile Priority::Value _priority
Definition: Category.hh:639
virtual void addAppender(Appender *appender)
Definition: Category.cpp:94
virtual CategoryStream getStream(Priority::Value priority)
Definition: Category.cpp:417
void error(const char *stringFormat,...)
Definition: Category.cpp:347
void emerg(const char *stringFormat,...)
Definition: Category.cpp:389
virtual void _logUnconditionally2(Priority::Value priority, const std::string &message)
Definition: Category.cpp:257


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:14:17