/
SourceLocation.java
185 lines (159 loc) · 5.7 KB
/
SourceLocation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright 2016 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.logging;
import static com.google.common.base.Preconditions.checkElementIndex;
import com.google.common.base.MoreObjects;
import com.google.logging.v2.LogEntrySourceLocation;
import java.io.Serializable;
import java.util.Objects;
/** Additional information about the source code location that produced the log entry. */
public final class SourceLocation implements Serializable {
private static final long serialVersionUID = 8502955858162387374L;
private final String file;
private final Long line;
private final String function;
public static Builder newBuilder() {
return new Builder();
}
/** A builder for {@code SourceLocation} objects. */
public static final class Builder {
private String file;
private Long line;
private String function;
Builder() {}
Builder(SourceLocation sourceLocation) {
this.file = sourceLocation.file;
this.line = sourceLocation.line;
this.function = sourceLocation.function;
}
/**
* Sets the source file name. Depending on the runtime environment, this might be a simple name
* or a fully-qualified name.
*/
public Builder setFile(String file) {
this.file = file;
return this;
}
/** Sets the line within the source file. 1-based; 0 indicates no line number available. */
public Builder setLine(Long line) {
this.line = line;
return this;
}
/**
* Sets the human-readable name of the function or method being invoked, with optional context
* such as the class or package name. This information may be used in contexts such as the logs
* viewer, where a file and line number are less meaningful. The format can vary by language.
*/
public Builder setFunction(String function) {
this.function = function;
return this;
}
public SourceLocation build() {
return new SourceLocation(this);
}
}
SourceLocation(Builder builder) {
this.file = builder.file;
this.line = builder.line;
this.function = builder.function;
}
/**
* Returns the source file name. Depending on the runtime environment, this might be a simple name
* or a fully-qualified name.
*/
public String getFile() {
return file;
}
/** Returns the line within the source file. 1-based; 0 indicates no line number available. */
public Long getLine() {
return line;
}
/**
* Returns the human-readable name of the function or method being invoked, with optional context
* such as the class or package name. This information may be used in contexts such as the logs
* viewer, where a file and line number are less meaningful. The format can vary by language.
*/
public String getFunction() {
return function;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof SourceLocation)) {
return false;
}
SourceLocation other = (SourceLocation) obj;
return Objects.equals(file, other.file)
&& Objects.equals(line, other.line)
&& Objects.equals(function, other.function);
}
@Override
public int hashCode() {
return Objects.hash(file, line, function);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("file", file)
.add("line", line)
.add("function", function)
.toString();
}
public Builder toBuilder() {
return new Builder(this);
}
LogEntrySourceLocation toPb() {
LogEntrySourceLocation.Builder builder = LogEntrySourceLocation.newBuilder();
builder.setFile(file);
builder.setLine(line);
builder.setFunction(function);
return builder.build();
}
static SourceLocation fromPb(LogEntrySourceLocation sourceLocationPb) {
return new Builder()
.setFile(sourceLocationPb.getFile())
.setLine(sourceLocationPb.getLine())
.setFunction(sourceLocationPb.getFunction())
.build();
}
/**
* Creates instance of {@link SourceLocation} based on stack trace information. Caller should
* provide the level in the stack where the information can be located. The stack trace level
* should be {@code 0} to display information for the caller of the method.
*
* @param level Zero-based non-negative integer defining the level in the stack trace where {@code
* 0} is topmost element.
* @return a new instance of {@link SourceLocation} populated with file name, method and line
* number information.
* @throws IndexOutOfBoundsException if the provided {@link level} is negative or greater than the
* current call stack.
*/
static SourceLocation fromCurrentContext(int level) {
StackTraceElement[] stackTrace = (new Exception()).getStackTrace();
Builder builder = newBuilder();
// need to take info from 1 level down the stack to compensate the call to this
// method
int indexPlus = checkElementIndex(level, stackTrace.length - 1) + 1;
StackTraceElement ste = stackTrace[indexPlus];
return builder
.setFile(ste.getFileName())
.setLine(Long.valueOf(ste.getLineNumber()))
.setFunction(ste.getMethodName())
.build();
}
}