1 // Module: Log4CPLUS
2 // File: loggingevent.h
3 // Created: 6/2001
4 // Author: Tad E. Smith
5 //
6 //
7 // Copyright (C) Tad E. Smith All rights reserved.
8 //
9 // This software is published under the terms of the Apache Software
10 // License version 1.1, a copy of which has been included with this
11 // distribution in the LICENSE.APL file.
12 //
13
14 /** @file */
15
16 #ifndef _LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_
17 #define _LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_
18
19 #include <log4cplus/config.h>
20 #include <log4cplus/loglevel.h>
21 #include <log4cplus/ndc.h>
22 #include <log4cplus/tstring.h>
23 #include <log4cplus/helpers/timehelper.h>
24 #include <log4cplus/helpers/threads.h>
25
26 namespace log4cplus {
27 namespace spi {
28 /**
29 * The internal representation of logging events. When an affirmative
30 * decision is made to log then a <code>InternalLoggingEvent</code>
31 * instance is created. This instance is passed around to the
32 * different log4cplus components.
33 *
34 * <p>This class is of concern to those wishing to extend log4cplus.
35 */
36 class LOG4CPLUS_EXPORT InternalLoggingEvent {
37 public:
38 // Ctors
39 /**
40 * Instantiate a LoggingEvent from the supplied parameters.
41 * <p>
42 * @param logger The logger of this event.
43 * @param ll The LogLevel of this event.
44 * @param message The message of this event.
45 */
46 InternalLoggingEvent(const log4cplus::tstring& logger,
47 LogLevel ll,
48 const log4cplus::tstring& message,
49 const char* filename,
50 int line)
51 : message(message),
52 loggerName(logger),
53 ll(ll),
54 ndc(),
55 thread(),
56 timestamp(log4cplus::helpers::Time::gettimeofday()),
57 file( ( filename
58 ? LOG4CPLUS_C_STR_TO_TSTRING(filename)
59 : log4cplus::tstring()) ),
60 line(line),
61 threadCached(false),
62 ndcCached(false)
63 {
64 }
65
66 InternalLoggingEvent(const log4cplus::tstring& logger,
67 LogLevel ll,
68 const log4cplus::tstring& ndc,
69 const log4cplus::tstring& message,
70 const log4cplus::tstring& thread,
71 log4cplus::helpers::Time time,
72 const log4cplus::tstring& file,
73 int line)
74 : message(message),
75 loggerName(logger),
76 ll(ll),
77 ndc(ndc),
78 thread(thread),
79 timestamp(time),
80 file(file),
81 line(line),
82 threadCached(true),
83 ndcCached(true)
84 {
85 }
86
87 InternalLoggingEvent(const log4cplus::spi::InternalLoggingEvent& rhs)
88 : message(rhs.getMessage()),
89 loggerName(rhs.getLoggerName()),
90 ll(rhs.getLogLevel()),
91 ndc(rhs.getNDC()),
92 thread(rhs.getThread()),
93 timestamp(rhs.getTimestamp()),
94 file(rhs.getFile()),
95 line(rhs.getLine()),
96 threadCached(true),
97 ndcCached(true)
98 {
99 }
100
101 virtual ~InternalLoggingEvent();
102
103
104 // public virtual methods
105 /** The application supplied message of logging event. */
106 virtual const log4cplus::tstring& getMessage() const;
107
108 /** Returns the 'type' of InternalLoggingEvent. Derived classes
109 * should override this method. (NOTE: Values <= 1000 are
110 * reserved for log4cplus and should not be used.)
111 */
112 virtual unsigned int getType() const;
113
114 /** Returns a copy of this object. Derived classes
115 * should override this method.
116 */
117 virtual std::auto_ptr<InternalLoggingEvent> clone() const;
118
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 { return loggerName; }
126
127 /** LogLevel of logging event. */
128 LogLevel getLogLevel() const { return ll; }
129
130 /** The nested diagnostic context (NDC) of logging event. */
131 const log4cplus::tstring& getNDC() const {
132 if(!ndcCached) {
133 ndc = log4cplus::getNDC().get();
134 ndcCached = true;
135 }
136 return ndc;
137 }
138
139 /** The name of thread in which this logging event was generated. */
140 const log4cplus::tstring& getThread() const {
141 if(!threadCached) {
142 thread = LOG4CPLUS_GET_CURRENT_THREAD_NAME;
143 threadCached = true;
144 }
145 return thread;
146 }
147
148 /** The number of milliseconds elapsed from 1/1/1970 until logging event
149 * was created. */
150 const log4cplus::helpers::Time& getTimestamp() const { return timestamp; }
151
152 /** The is the file where this log statement was written */
153 const log4cplus::tstring& getFile() const { return file; }
154
155 /** The is the line where this log statement was written */
156 int getLine() const { return line; }
157
158 // public operators
159 log4cplus::spi::InternalLoggingEvent&
160 operator=(const log4cplus::spi::InternalLoggingEvent& rhs);
161
162 // static methods
163 static unsigned int getDefaultType();
164
165 protected:
166 // Data
167 log4cplus::tstring message;
168
169 private:
170 log4cplus::tstring loggerName;
171 LogLevel ll;
172 mutable log4cplus::tstring ndc;
173 mutable log4cplus::tstring thread;
174 log4cplus::helpers::Time timestamp;
175 log4cplus::tstring file;
176 int line;
177 /** Indicates whether or not the Threadname has been retrieved. */
178 mutable bool threadCached;
179 /** Indicates whether or not the NDC has been retrieved. */
180 mutable bool ndcCached;
181 };
182
183 } // end namespace spi
184 } // end namespace log4cplus
185
186 #endif // _LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_
syntax highlighted by Code2HTML, v. 0.9.1