Apache Log4cxx  Version 1.1.0
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 
24 #if __cpp_lib_string_view || (_MSVC_LANG >= 201703L)
25 #include <string_view>
26 #define LOG4CXX_HAS_STRING_VIEW
27 #else
28 #include <string.h>
29 #endif
30 
31 #if defined(_WIN32)
32 #define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR '\\'
33 #else
34 #define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR '/'
35 #endif
36 
37 namespace log4cxx
38 {
39 namespace spi
40 {
45 class LOG4CXX_EXPORT LocationInfo
46 {
47  public:
48 
49 
50 
55  static const char* const NA;
56  static const char* const NA_METHOD;
57 
59 
60 #ifdef LOG4CXX_HAS_STRING_VIEW
61  static constexpr const char* calcShortFileName(const char* fileName){
62  std::string_view view(fileName);
63  // If the separator is not found, rfind will return -1. Adding 1 to
64  // that will have it pointing at fileName, which is a good fallback.
65  return fileName + view.rfind(LOG4CXX_SHORT_FILENAME_SPLIT_CHAR) + 1;
66  }
67 #else
68  static const char* calcShortFileName(const char* fileName){
69  const char* location = strrchr(fileName, LOG4CXX_SHORT_FILENAME_SPLIT_CHAR);
70  return location == nullptr ? fileName : location + 1;
71  }
72 #endif
73 
79  LocationInfo( const char* const fileName,
80  const char* const shortFileName,
81  const char* const functionName,
82  int lineNumber);
83 
88 
93  LocationInfo( const LocationInfo& src );
94 
99  LocationInfo& operator = ( const LocationInfo& src );
100 
104  void clear();
105 
106 
108  const std::string getClassName() const;
109 
114  const char* getFileName() const;
115 
121  const char* getShortFileName() const;
122 
127  int getLineNumber() const;
128 
130  const std::string getMethodName() const;
131 
132 
133  private:
135  int lineNumber;
136 
138  const char* fileName;
139 
141  const char* shortFileName;
142 
144  const char* methodName;
145 
146 
147 };
148 }
149 }
150 
151 #if !defined(LOG4CXX_LOCATION) && !LOG4CXX_DISABLE_LOCATION_INFO
152 #if defined(_MSC_VER)
153  #if _MSC_VER >= 1300
154  #define __LOG4CXX_FUNC__ __FUNCSIG__
155  #endif
156 #else
157  #if defined(__GNUC__)
158  #define __LOG4CXX_FUNC__ __PRETTY_FUNCTION__
159  #else
160  #if defined(__BORLANDC__)
161  #define __LOG4CXX_FUNC__ __FUNC__
162  #endif
163  #endif
164 #endif
165 #if !defined(__LOG4CXX_FUNC__)
166  #define __LOG4CXX_FUNC__ ""
167 #endif
168 
169 
170 #define LOG4CXX_LOCATION ::log4cxx::spi::LocationInfo(__FILE__, \
171  ::log4cxx::spi::LocationInfo::calcShortFileName(__FILE__), \
172  __LOG4CXX_FUNC__, \
173  __LINE__)
174 
175 #else
176 #define LOG4CXX_LOCATION ::log4cxx::spi::LocationInfo::getLocationUnavailable()
177 #endif // LOG4CXX_LOCATION
178 
179 #endif //_LOG4CXX_SPI_LOCATION_LOCATIONINFO_H
This class represents the location of a logging statement.
Definition: locationinfo.h:46
LocationInfo(const LocationInfo &src)
Copy constructor.
static const char * calcShortFileName(const char *fileName)
Definition: locationinfo.h:68
void clear()
Resets location info to default state.
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 char * getFileName() const
Return the 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:56
int getLineNumber() const
Returns the line number of the caller.
static const LocationInfo & getLocationUnavailable()
static const char *const NA
When location information is not available the constant NA is returned.
Definition: locationinfo.h:55
#define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR
Definition: locationinfo.h:34
Definition: appender.h:27