1 //  Copyright (C) 2010-2013, Vaclav Haisman. All rights reserved.
2 //
3 //  Redistribution and use in source and binary forms, with or without modifica-
4 //  tion, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of  source code must  retain the above copyright  notice,
7 //     this list of conditions and the following disclaimer.
8 //
9 //  2. Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //
13 //  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
14 //  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15 //  FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
16 //  APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
17 //  INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
18 //  DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
19 //  OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
20 //  ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
21 //  (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
22 //  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 #include <log4cplus/mdc.h>
25 #include <log4cplus/internal/internal.h>
26 
27 
28 namespace log4cplus
29 {
30 
31 
MDC()32 MDC::MDC ()
33 { }
34 
35 
~MDC()36 MDC::~MDC ()
37 { }
38 
39 
40 MappedDiagnosticContextMap *
getPtr()41 MDC::getPtr ()
42 {
43     return &internal::get_ptd ()->mdc_map;
44 }
45 
46 
47 void
clear()48 MDC::clear()
49 {
50     MappedDiagnosticContextMap * const dc = getPtr ();
51     MappedDiagnosticContextMap ().swap (*dc);
52 }
53 
54 
55 void
put(tstring const & key,tstring const & value)56 MDC::put (tstring const & key, tstring const & value)
57 {
58     MappedDiagnosticContextMap * const dc = getPtr ();
59     (*dc)[key] = value;
60 }
61 
62 
63 bool
get(tstring * value,tstring const & key) const64 MDC::get (tstring * value, tstring const & key) const
65 {
66     assert (value);
67 
68     MappedDiagnosticContextMap * const dc = getPtr ();
69     MappedDiagnosticContextMap::const_iterator it = dc->find (key);
70     if (it != dc->end ())
71     {
72         *value = it->second;
73         return true;
74     }
75     else
76         return false;
77 }
78 
79 
80 void
remove(tstring const & key)81 MDC::remove (tstring const & key)
82 {
83     MappedDiagnosticContextMap * const dc = getPtr ();
84     dc->erase (key);
85 }
86 
87 
88 MappedDiagnosticContextMap const &
getContext() const89 MDC::getContext () const
90 {
91     return *getPtr ();
92 }
93 
94 
95 } // namespace log4cplus
96