Coverage Report

Created: 2025-07-11 07:00

/src/logging-log4cxx/src/main/include/log4cxx/spi/loggingevent.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
#ifndef _LOG4CXX_SPI_LOGGING_EVENT_H
19
#define _LOG4CXX_SPI_LOGGING_EVENT_H
20
21
#include <log4cxx/logstring.h>
22
#include <time.h>
23
#include <log4cxx/logger.h>
24
#include <log4cxx/mdc.h>
25
#include <log4cxx/spi/location/locationinfo.h>
26
#include <vector>
27
#include <chrono>
28
29
30
namespace LOG4CXX_NS
31
{
32
33
namespace spi
34
{
35
LOG4CXX_LIST_DEF(KeySet, LogString);
36
37
/**
38
The data recorded for each logging request.
39
Each logging request instantiates a <code>LoggingEvent</code> instance,
40
which Log4cxx provides to [filters](@ref log4cxx.spi.Filter),
41
[layouts](@ref log4cxx.Layout) and [appenders](@ref log4cxx.Appender).
42
43
<p>This class is of concern to those wishing to extend log4cxx.
44
*/
45
class LOG4CXX_EXPORT LoggingEvent :
46
  public virtual helpers::Object
47
{
48
  public:
49
    DECLARE_LOG4CXX_OBJECT(LoggingEvent)
50
0
    BEGIN_LOG4CXX_CAST_MAP()
51
0
    LOG4CXX_CAST_ENTRY(LoggingEvent)
52
0
    END_LOG4CXX_CAST_MAP()
53
54
    typedef spi::KeySet KeySet;
55
56
    /** An empty event.
57
    */
58
    LoggingEvent();
59
60
    /**
61
    An event composed using the supplied parameters.
62
63
    @param logger The logger used to make the logging request.
64
    @param level The severity of this event.
65
    @param location The source code location of the logging request.
66
    @param message  The text to add to this event.
67
    */
68
    LoggingEvent
69
      ( const LogString& logger
70
      , const LevelPtr& level
71
      , const LocationInfo& location
72
      , LogString&& message
73
      );
74
75
    /**
76
    An event composed using the supplied parameters.
77
78
    @param logger The logger used to make the logging request.
79
    @param level The severity of this event.
80
    @param message  The text to add to this event.
81
    @param location The source code location of the logging request.
82
    */
83
    LoggingEvent
84
      ( const LogString& logger
85
      , const LevelPtr&  level
86
      , const LogString& message
87
      , const LocationInfo& location
88
      );
89
90
    ~LoggingEvent();
91
92
    /** The severity level of the logging request that generated this event. */
93
    const LevelPtr& getLevel() const;
94
95
    /**  The name of the logger used to make the logging request. */
96
    const LogString& getLoggerName() const;
97
98
    /** The message provided in the logging request. */
99
    const LogString& getMessage() const;
100
101
    /** The message provided in the logging request. */
102
    const LogString& getRenderedMessage() const;
103
104
    /** The number of microseconds elapsed since 1970-01-01
105
     *  at the time the application started.
106
     */
107
    static log4cxx_time_t getStartTime();
108
109
    /** The identity of the thread in which this logging event was created. */
110
    const LogString& getThreadName() const;
111
112
    /**
113
     * The name you gave to the thread in which this logging event was created.
114
     * You can create a named thread using log4cxx::helpers::ThreadUtility::createThread.
115
     * If Log4cxx is unable to retrieve the thread name using a platform-specific call,
116
     * the value is the same as the thread identity.
117
     */
118
    const LogString& getThreadUserName() const;
119
120
    /** The number of microseconds elapsed since 1970-01-01
121
     *  at the time this logging event was created.
122
     */
123
    log4cxx_time_t getTimeStamp() const;
124
125
    /** The value of the system clock at the time this logging event was created.
126
     */
127
    std::chrono::time_point<std::chrono::system_clock> getChronoTimeStamp() const;
128
129
    /** The source code location where the logging request was made. */
130
    const LocationInfo& getLocationInformation() const;
131
132
    /**
133
    * Add the current nested diagnostic context to the end of \c dest.
134
    * The diagnostic context must have been loaded into this LoggingEvent using LoadDC,
135
    * to obtain the correct content if the event was generated in a different thread.
136
    *
137
    * @param dest the string to be added to.
138
    * @return true if \c dest is changed.
139
    */
140
    bool getNDC(LogString& dest) const;
141
142
    /**
143
    * Add the value associated with \c key to the end of \c dest.
144
    * The diagnostic context must have been loaded into this LoggingEvent using LoadDC,
145
    * to obtain the correct content if the event was generated in a different thread.
146
    *
147
    * @param key mapped diagnostic context key value.
148
    * @param dest the string to be added to.
149
    * @return true if \c dest is changed.
150
    */
151
    bool getMDC(const LogString& key, LogString& dest) const;
152
153
    /**
154
    * The keys in the mapped diagnostic context for the event.
155
    *
156
    * @return the mapped diagnostic context keys.
157
    *
158
    */
159
    KeySet getMDCKeySet() const;
160
161
#if LOG4CXX_ABI_VERSION <= 15
162
    /**
163
    Obtain a copy of the current thread's diagnostic context data.
164
    */
165
    [[ deprecated( "Use LoadDC instead" ) ]]
166
    void getMDCCopy() const;
167
#endif
168
169
    /**
170
    * Obtain a copy of the current thread's diagnostic context data.
171
    * The diagnostic context must be loaded to ensure the
172
    * correct diagnostic context data is available
173
    * when the event is stored for later use
174
    * (for example, when the appender uses a different thread to process this event).
175
    */
176
    void LoadDC() const;
177
178
    /**
179
    * Append onto \c dest the value associated with the property \c key.
180
    * @param key the property name.
181
    * @param dest the string onto which to associated value is appended.
182
    * @return true if \c dest was changed.
183
    */
184
    bool getProperty(const LogString& key, LogString& dest) const;
185
186
    /**
187
    * The set of of the key values in the properties
188
    * for the event.
189
    * @return the keys from properties in this event.
190
    */
191
    KeySet getPropertyKeySet() const;
192
193
    /**
194
    * Associate \c value with the property \c key.
195
    */
196
    void setProperty(const LogString& key, const LogString& value);
197
198
  private:
199
    LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(LoggingEventPrivate, m_priv)
200
201
    //
202
    //   prevent copy and assignment
203
    //
204
    LoggingEvent(const LoggingEvent&);
205
    LoggingEvent& operator=(const LoggingEvent&);
206
207
};
208
209
LOG4CXX_PTR_DEF(LoggingEvent);
210
LOG4CXX_LIST_DEF(LoggingEventList, LoggingEventPtr);
211
}
212
}
213
214
#endif //_LOG4CXX_SPI_LOGGING_EVENT_H