/src/logging-log4cxx/src/main/cpp/locationinfo.cpp
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 | | #include <log4cxx/spi/location/locationinfo.h> |
19 | | #include <log4cxx/helpers/pool.h> |
20 | | #include <log4cxx/helpers/widelife.h> |
21 | | |
22 | | using namespace ::LOG4CXX_NS::spi; |
23 | | using namespace LOG4CXX_NS::helpers; |
24 | | |
25 | | /** |
26 | | When location information is not available the constant |
27 | | <code>NA</code> is returned. Current value of this string |
28 | | constant is <b>?</b>. */ |
29 | | const char* const LocationInfo::NA = "?"; |
30 | | const char* const LocationInfo::NA_METHOD = "?::?"; |
31 | | |
32 | | const LocationInfo& LocationInfo::getLocationUnavailable() |
33 | 0 | { |
34 | 0 | static const WideLife<LocationInfo> unavailable; |
35 | 0 | return unavailable; |
36 | 0 | } |
37 | | |
38 | | /** |
39 | | * Constructor. |
40 | | * @remarks Used by LOG4CXX_LOCATION to generate |
41 | | * location info for current code site |
42 | | */ |
43 | | LocationInfo::LocationInfo( const char* const fileName1, |
44 | | const char* const shortFileName1, |
45 | | const char* const methodName1, |
46 | | int lineNumber1 ) |
47 | 418k | : lineNumber( lineNumber1 ), |
48 | 418k | fileName( fileName1 ? fileName1 : LocationInfo::NA ), |
49 | 418k | shortFileName(shortFileName1 ? shortFileName1 : LocationInfo::NA ), |
50 | 418k | methodName( methodName1 ? methodName1 : LocationInfo::NA_METHOD ) |
51 | 418k | { |
52 | 418k | } |
53 | | |
54 | | /** |
55 | | * Default constructor. |
56 | | */ |
57 | | LocationInfo::LocationInfo() |
58 | 0 | : lineNumber( -1 ), |
59 | 0 | fileName(LocationInfo::NA), |
60 | 0 | shortFileName(LocationInfo::NA), |
61 | 0 | methodName(LocationInfo::NA_METHOD) |
62 | 0 | { |
63 | 0 | } |
64 | | |
65 | | /** |
66 | | * Copy constructor. |
67 | | * @param src source location |
68 | | */ |
69 | | LocationInfo::LocationInfo( const LocationInfo& src ) |
70 | 418k | : lineNumber( src.lineNumber ), |
71 | 418k | fileName( src.fileName ), |
72 | 418k | shortFileName( src.shortFileName ), |
73 | 418k | methodName( src.methodName ) |
74 | 418k | { |
75 | 418k | } |
76 | | |
77 | | /** |
78 | | * Assignment operator. |
79 | | * @param src source location |
80 | | */ |
81 | | LocationInfo& LocationInfo::operator = ( const LocationInfo& src ) |
82 | 0 | { |
83 | 0 | fileName = src.fileName; |
84 | 0 | shortFileName = src.shortFileName; |
85 | 0 | methodName = src.methodName; |
86 | 0 | lineNumber = src.lineNumber; |
87 | 0 | return * this; |
88 | 0 | } |
89 | | |
90 | | /** |
91 | | * Resets location info to default state. |
92 | | */ |
93 | | void LocationInfo::clear() |
94 | 0 | { |
95 | 0 | fileName = NA; |
96 | 0 | shortFileName = NA; |
97 | 0 | methodName = NA_METHOD; |
98 | 0 | lineNumber = -1; |
99 | 0 | } |
100 | | |
101 | | |
102 | | /** |
103 | | * Return the file name of the caller. |
104 | | * @returns file name, may be null. |
105 | | */ |
106 | | const char* LocationInfo::getFileName() const |
107 | 0 | { |
108 | 0 | return fileName; |
109 | 0 | } |
110 | | |
111 | 418k | const char* LocationInfo::getShortFileName() const{ |
112 | 418k | return shortFileName; |
113 | 418k | } |
114 | | |
115 | | /** |
116 | | * Returns the line number of the caller. |
117 | | * @returns line number, -1 if not available. |
118 | | */ |
119 | | int LocationInfo::getLineNumber() const |
120 | 418k | { |
121 | 418k | return lineNumber; |
122 | 418k | } |
123 | | |
124 | | /** Returns the method name of the caller. */ |
125 | | const std::string LocationInfo::getMethodName() const |
126 | 0 | { |
127 | 0 | std::string tmp(methodName); |
128 | 0 | size_t parenPos = tmp.find('('); |
129 | |
|
130 | 0 | if (parenPos != std::string::npos) |
131 | 0 | { |
132 | 0 | tmp.erase(parenPos); |
133 | 0 | } |
134 | |
|
135 | 0 | size_t colonPos = tmp.rfind("::"); |
136 | |
|
137 | 0 | if (colonPos != std::string::npos) |
138 | 0 | { |
139 | 0 | tmp.erase(0, colonPos + 2); |
140 | 0 | } |
141 | 0 | else |
142 | 0 | { |
143 | 0 | size_t spacePos = tmp.find(' '); |
144 | |
|
145 | 0 | if (spacePos != std::string::npos) |
146 | 0 | { |
147 | 0 | tmp.erase(0, spacePos + 1); |
148 | 0 | } |
149 | 0 | } |
150 | |
|
151 | 0 | return tmp; |
152 | 0 | } |
153 | | |
154 | | |
155 | | const std::string LocationInfo::getClassName() const |
156 | 0 | { |
157 | 0 | std::string tmp(methodName); |
158 | 0 | size_t parenPos = tmp.find('('); |
159 | |
|
160 | 0 | if (parenPos != std::string::npos) |
161 | 0 | { |
162 | 0 | tmp.erase(parenPos); |
163 | 0 | } |
164 | |
|
165 | 0 | size_t colonPos = tmp.rfind("::"); |
166 | |
|
167 | 0 | if (colonPos != std::string::npos) |
168 | 0 | { |
169 | 0 | tmp.erase(colonPos); |
170 | 0 | size_t spacePos = tmp.find_last_of(' '); |
171 | |
|
172 | 0 | if (spacePos != std::string::npos) |
173 | 0 | { |
174 | 0 | tmp.erase(0, spacePos + 1); |
175 | 0 | } |
176 | |
|
177 | 0 | return tmp; |
178 | 0 | } |
179 | | |
180 | 0 | tmp.erase(0, tmp.length() ); |
181 | 0 | return tmp; |
182 | 0 | } |
183 | | |
184 | | |