Coverage Report

Created: 2026-05-16 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/log4cplus/include/log4cplus/spi/loggingevent.h
Line
Count
Source
1
// -*- C++ -*-
2
// Module:  Log4CPLUS
3
// File:    loggingevent.h
4
// Created: 6/2001
5
// Author:  Tad E. Smith
6
//
7
//
8
// Copyright 2001-2017 Tad E. Smith
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License");
11
// you may not use this file except in compliance with the License.
12
// You may obtain a copy of the License at
13
//
14
//     http://www.apache.org/licenses/LICENSE-2.0
15
//
16
// Unless required by applicable law or agreed to in writing, software
17
// distributed under the License is distributed on an "AS IS" BASIS,
18
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
// See the License for the specific language governing permissions and
20
// limitations under the License.
21
22
/** @file */
23
24
#ifndef LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_
25
#define LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_
26
27
#include <log4cplus/config.hxx>
28
29
#if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
30
#pragma once
31
#endif
32
33
#include <memory>
34
#include <log4cplus/loglevel.h>
35
#include <log4cplus/ndc.h>
36
#include <log4cplus/mdc.h>
37
#include <log4cplus/tstring.h>
38
#include <log4cplus/helpers/timehelper.h>
39
#include <log4cplus/thread/threads.h>
40
41
namespace log4cplus {
42
    namespace spi {
43
        /**
44
         * The internal representation of logging events. When an affirmative
45
         * decision is made to log then a <code>InternalLoggingEvent</code>
46
         * instance is created. This instance is passed around to the
47
         * different log4cplus components.
48
         *
49
         * This class is of concern to those wishing to extend log4cplus.
50
         */
51
        class LOG4CPLUS_EXPORT InternalLoggingEvent {
52
        public:
53
          // Ctors
54
             /**
55
              * Instantiate a LoggingEvent from the supplied parameters.
56
              *
57
              * @param logger   The logger of this event.
58
              * @param loglevel The LogLevel of this event.
59
              * @param message  The message of this event.
60
              * @param filename Name of file where this event has occurred,
61
              * can be NULL.
62
              * @param line     Line number in file specified by
63
              *                 the <code>filename</code> parameter.
64
              * @param function Name of function that is logging this event.
65
              */
66
            InternalLoggingEvent(const log4cplus::tstring& logger,
67
                LogLevel loglevel, const log4cplus::tstring& message,
68
                const char* filename, int line, const char * function = nullptr);
69
70
            InternalLoggingEvent(const log4cplus::tstring& logger,
71
                LogLevel loglevel, const log4cplus::tstring& ndc,
72
                MappedDiagnosticContextMap const & mdc,
73
                const log4cplus::tstring& message,
74
                const log4cplus::tstring& thread,
75
                log4cplus::helpers::Time time, const log4cplus::tstring& file,
76
                int line, const log4cplus::tstring & function
77
                    = log4cplus::tstring ()) LOG4CPLUS_ATTRIBUTE_DEPRECATED;
78
79
            InternalLoggingEvent(const log4cplus::tstring& logger,
80
                LogLevel loglevel, const log4cplus::tstring& ndc,
81
                MappedDiagnosticContextMap const & mdc,
82
                const log4cplus::tstring& message,
83
                const log4cplus::tstring& thread,
84
                const log4cplus::tstring& thread2,
85
                log4cplus::helpers::Time time, const log4cplus::tstring& file,
86
                int line, const log4cplus::tstring & function
87
                    = log4cplus::tstring ());
88
89
            InternalLoggingEvent ();
90
91
            InternalLoggingEvent(
92
                const log4cplus::spi::InternalLoggingEvent& rhs);
93
94
            virtual ~InternalLoggingEvent();
95
96
            void setLoggingEvent (const log4cplus::tstring & logger,
97
                LogLevel ll, const log4cplus::tstring & message,
98
                const char * filename, int line,
99
                const char * function = nullptr);
100
101
            void setFunction (char const * func);
102
            void setFunction (log4cplus::tstring const &);
103
104
105
          // public virtual methods
106
            /** The application supplied message of logging event. */
107
            virtual const log4cplus::tstring& getMessage() const;
108
109
            /** Returns the 'type' of InternalLoggingEvent.  Derived classes
110
             *  should override this method.  (NOTE: Values <= 1000 are
111
             *  reserved for log4cplus and should not be used.)
112
             */
113
            virtual unsigned int getType() const;
114
115
           /** Returns a copy of this object.  Derived classes
116
             *  should override this method.
117
             */
118
            virtual std::unique_ptr<InternalLoggingEvent> clone() const;
119
120
121
          // public methods
122
            /** The logger of the logging event. It is set by
123
             *  the LoggingEvent constructor.
124
             */
125
            const log4cplus::tstring& getLoggerName() const
126
697k
            {
127
697k
                return loggerName;
128
697k
            }
129
130
            /** LogLevel of logging event. */
131
            LogLevel getLogLevel() const
132
1.39M
            {
133
1.39M
                return ll;
134
1.39M
            }
135
136
            /** The nested diagnostic context (NDC) of logging event. */
137
            const log4cplus::tstring& getNDC() const
138
0
            {
139
0
                if (!ndcCached)
140
0
                {
141
0
                    ndc = log4cplus::getNDC().get();
142
0
                    ndcCached = true;
143
0
                }
144
0
                return ndc;
145
0
            }
146
147
            MappedDiagnosticContextMap const & getMDCCopy () const
148
0
            {
149
0
                if (!mdcCached)
150
0
                {
151
0
                    mdc = log4cplus::getMDC().getContext ();
152
0
                    mdcCached = true;
153
0
                }
154
0
                return mdc;
155
0
            }
156
157
            tstring const & getMDC (tstring const & key) const;
158
159
            /** The name of thread in which this logging event was generated. */
160
            const log4cplus::tstring& getThread() const
161
697k
            {
162
697k
                if (! threadCached)
163
697k
                {
164
697k
                    thread = thread::getCurrentThreadName ();
165
697k
                    threadCached = true;
166
697k
                }
167
697k
                return thread;
168
697k
            }
169
170
            //! The alternative name of thread in which this logging event
171
            //! was generated.
172
            const log4cplus::tstring& getThread2() const
173
0
            {
174
0
                if (! thread2Cached)
175
0
                {
176
0
                    thread2 = thread::getCurrentThreadName2 ();
177
0
                    thread2Cached = true;
178
0
                }
179
0
                return thread2;
180
0
            }
181
182
183
            /** Time stamp when the event was created. */
184
            const log4cplus::helpers::Time& getTimestamp() const
185
697k
            {
186
697k
                return timestamp;
187
697k
            }
188
189
            /** The is the file where this log statement was written */
190
            const log4cplus::tstring& getFile() const
191
0
            {
192
0
                return file;
193
0
            }
194
195
            /** The is the line where this log statement was written */
196
0
            int getLine() const { return line; }
197
198
            log4cplus::tstring const & getFunction () const
199
0
            {
200
0
                return function;
201
0
            }
202
203
            void gatherThreadSpecificData () const;
204
205
            void swap (InternalLoggingEvent &);
206
207
          // public operators
208
            log4cplus::spi::InternalLoggingEvent&
209
            operator=(const log4cplus::spi::InternalLoggingEvent& rhs);
210
211
          // static methods
212
            static unsigned int getDefaultType();
213
214
        protected:
215
          // Data
216
            log4cplus::tstring message;
217
            log4cplus::tstring loggerName;
218
            LogLevel ll;
219
            mutable log4cplus::tstring ndc;
220
            mutable MappedDiagnosticContextMap mdc;
221
            mutable log4cplus::tstring thread;
222
            mutable log4cplus::tstring thread2;
223
            log4cplus::helpers::Time timestamp;
224
            log4cplus::tstring file;
225
            log4cplus::tstring function;
226
            int line;
227
            /** Indicates whether or not the Threadname has been retrieved. */
228
            mutable bool threadCached;
229
            mutable bool thread2Cached;
230
            /** Indicates whether or not the NDC has been retrieved. */
231
            mutable bool ndcCached;
232
            /** Indicates whether or not the MDC has been retrieved. */
233
            mutable bool mdcCached;
234
        };
235
236
    } // end namespace spi
237
} // end namespace log4cplus
238
239
#endif // LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_