MongoDB C++ Driver  legacy-1.1.2
log_domain-impl.h
1 /* Copyright 2013 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <algorithm>
19 #include <cstdlib>
20 
21 #include "mongo/base/status.h"
22 #include "mongo/logger/message_log_domain.h"
23 
24 /*
25  * Implementation of LogDomain<E>. Include this in cpp files to instantiate new LogDomain types.
26  * See message_log_domain.h, e.g.
27  */
28 
29 namespace mongo {
30 namespace logger {
31 
32 template <typename E>
33 LogDomain<E>::LogDomain()
34  : _abortOnFailure(false) {}
35 
36 template <typename E>
37 LogDomain<E>::~LogDomain() {
38  clearAppenders();
39 }
40 
41 template <typename E>
42 Status LogDomain<E>::append(const E& event) {
43  for (typename AppenderVector::const_iterator iter = _appenders.begin();
44  iter != _appenders.end();
45  ++iter) {
46  if (*iter) {
47  Status status = (*iter)->append(event);
48  if (!status.isOK()) {
49  if (_abortOnFailure) {
50  ::abort();
51  }
52  return status;
53  }
54  }
55  }
56  return Status::OK();
57 }
58 
59 template <typename E>
61  typename LogDomain<E>::AppenderAutoPtr appender) {
62  typename AppenderVector::iterator iter =
63  std::find(_appenders.begin(), _appenders.end(), static_cast<EventAppender*>(NULL));
64 
65  if (iter == _appenders.end()) {
66  _appenders.push_back(appender.release());
67  return AppenderHandle(_appenders.size() - 1);
68  } else {
69  *iter = appender.release();
70  return AppenderHandle(iter - _appenders.begin());
71  }
72 }
73 
74 template <typename E>
75 typename LogDomain<E>::AppenderAutoPtr LogDomain<E>::detachAppender(
76  typename LogDomain<E>::AppenderHandle handle) {
77  EventAppender*& appender = _appenders.at(handle._index);
78  AppenderAutoPtr result(appender);
79  appender = NULL;
80  return result;
81 }
82 
83 template <typename E>
85  for (typename AppenderVector::const_iterator iter = _appenders.begin();
86  iter != _appenders.end();
87  ++iter) {
88  delete *iter;
89  }
90 
91  _appenders.clear();
92 }
93 
94 } // namespace logger
95 } // namespace mongo
Status represents an error state or the absence thereof.
Definition: status.h:50
void clearAppenders()
Destroy all attached appenders, invalidating all handles.
Definition: log_domain-impl.h:84
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
AppenderHandle attachAppender(AppenderAutoPtr appender)
Attaches "appender" to this domain, taking ownership of it.
Definition: log_domain-impl.h:60
AppenderAutoPtr detachAppender(AppenderHandle handle)
Detaches the appender referenced by "handle" from this domain, releasing ownership of it...
Definition: log_domain-impl.h:75
Interface for sinks in a logging system.
Definition: appender.h:31
Opaque handle returned by attachAppender(), which can be subsequently passed to detachAppender() to d...
Definition: log_domain.h:61
Status append(const Event &event)
Receives an event for logging, calling append(event) on all attached appenders.
Definition: log_domain-impl.h:42