Apache Log4cxx  Version 1.3.1
Loading...
Searching...
No Matches
locationinfo.h
Go to the documentation of this file.
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_LOCATION_LOCATIONINFO_H
19#define _LOG4CXX_SPI_LOCATION_LOCATIONINFO_H
20
21#include <log4cxx/log4cxx.h>
22#include <string>
23#include <string.h>
24
25#if defined(_MSC_VER)
26#define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR '\\'
27#else
28#define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR '/'
29#endif
30
31namespace LOG4CXX_NS
32{
33namespace spi
34{
39class LOG4CXX_EXPORT LocationInfo
40{
41 public:
42
47 static const char* const NA;
48 static const char* const NA_METHOD;
49
51
57#if 201304L <= __cpp_constexpr
58 static constexpr const char* calcShortFileName(const char* fileName){
59#else
60 static const char* calcShortFileName(const char* fileName){
61#endif
62 if (fileName == nullptr) return nullptr;
63#if defined(_MSC_VER)
64 // As at 2024, the MSVC optimizer does not inline a function that calls another function
65 const char* location = nullptr;
66 for (auto p = fileName; *p; ++p)
68 location = p;
69#else
70 const char* location = strrchr(fileName, LOG4CXX_SHORT_FILENAME_SPLIT_CHAR);
71#endif
72 return location == nullptr ? fileName : location + 1;
73 }
74
80 LocationInfo( const char* const fileName,
81 const char* const shortFileName,
82 const char* const functionName,
83 int lineNumber);
84
89
95
100 LocationInfo& operator = ( const LocationInfo& src );
101
105 void clear();
106
107
109 const std::string getClassName() const;
110
115 const char* getFileName() const;
116
122 const char* getShortFileName() const;
123
128 int getLineNumber() const;
129
131 const std::string getMethodName() const;
132
133
134 private:
136 int lineNumber;
137
139 const char* fileName;
140
142 const char* shortFileName;
143
145 const char* methodName;
146
147
148};
149}
150}
151
152#if !defined(LOG4CXX_LOCATION) && !LOG4CXX_DISABLE_LOCATION_INFO
153#if defined(_MSC_VER)
154 #if _MSC_VER >= 1300
155 #define __LOG4CXX_FUNC__ __FUNCSIG__
156 #endif
157#else
158 #if defined(__GNUC__)
159 #define __LOG4CXX_FUNC__ __PRETTY_FUNCTION__
160 #else
161 #if defined(__BORLANDC__)
162 #define __LOG4CXX_FUNC__ __FUNC__
163 #endif
164 #endif
165#endif
166#if !defined(__LOG4CXX_FUNC__)
167 #define __LOG4CXX_FUNC__ nullptr
168#endif
169
170
171#define LOG4CXX_LOCATION ::LOG4CXX_NS::spi::LocationInfo(__FILE__, \
172 ::LOG4CXX_NS::spi::LocationInfo::calcShortFileName(__FILE__), \
173 __LOG4CXX_FUNC__, \
174 __LINE__)
175
176#else
177#define LOG4CXX_LOCATION ::LOG4CXX_NS::spi::LocationInfo::getLocationUnavailable()
178#endif // LOG4CXX_LOCATION
179
180#endif //_LOG4CXX_SPI_LOCATION_LOCATIONINFO_H
This class represents the location of a logging statement.
Definition: locationinfo.h:40
LocationInfo(const LocationInfo &src)
Copy constructor.
void clear()
Resets location info to default state.
static const char * calcShortFileName(const char *fileName)
The part of fileName after the path.
Definition: locationinfo.h:60
const std::string getClassName() const
Return the class name of the call site.
LocationInfo()
Default constructor.
LocationInfo(const char *const fileName, const char *const shortFileName, const char *const functionName, int lineNumber)
Constructor.
const char * getShortFileName() const
Return the short file name of the caller.
const std::string getMethodName() const
Returns the method name of the caller.
static const char *const NA_METHOD
Definition: locationinfo.h:48
static const LocationInfo & getLocationUnavailable()
const char * getFileName() const
Return the file name of the caller.
int getLineNumber() const
Returns the line number of the caller.
static const char *const NA
When location information is not available the constant NA is returned.
Definition: locationinfo.h:47
#define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR
Definition: locationinfo.h:28