Skip to content

Commit 1fa3a6e

Browse files
feat: add population of the SourceLocation from context (#721)
* feat: add population of the SourceLocation from context * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 0f215d7 commit 1fa3a6e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/SourceLocation.java

+28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.logging;
1818

19+
import static com.google.common.base.Preconditions.checkElementIndex;
20+
1921
import com.google.common.base.MoreObjects;
2022
import com.google.logging.v2.LogEntrySourceLocation;
2123
import java.io.Serializable;
@@ -154,4 +156,30 @@ static SourceLocation fromPb(LogEntrySourceLocation sourceLocationPb) {
154156
.setFunction(sourceLocationPb.getFunction())
155157
.build();
156158
}
159+
160+
/**
161+
* Creates instance of {@link SourceLocation} based on stack trace information. Caller should
162+
* provide the level in the stack where the information can be located. The stack trace level
163+
* should be {@code 0} to display information for the caller of the method.
164+
*
165+
* @param level Zero-based non-negative integer defining the level in the stack trace where {@code
166+
* 0} is topmost element.
167+
* @return a new instance of {@link SourceLocation} populated with file name, method and line
168+
* number information.
169+
* @throws IndexOutOfBoundsException if the provided {@link level} is negative or greater than the
170+
* current call stack.
171+
*/
172+
static SourceLocation fromCurrentContext(int level) {
173+
StackTraceElement[] stackTrace = (new Exception()).getStackTrace();
174+
Builder builder = newBuilder();
175+
// need to take info from 1 level down the stack to compensate the call to this
176+
// method
177+
int indexPlus = checkElementIndex(level, stackTrace.length - 1) + 1;
178+
StackTraceElement ste = stackTrace[indexPlus];
179+
return builder
180+
.setFile(ste.getFileName())
181+
.setLine(Long.valueOf(ste.getLineNumber()))
182+
.setFunction(ste.getMethodName())
183+
.build();
184+
}
157185
}

google-cloud-logging/src/test/java/com/google/cloud/logging/SourceLocationTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ public void testToAndFromPb() {
5858
compareSourceLocation(SOURCE_LOCATION, SourceLocation.fromPb(SOURCE_LOCATION.toPb()));
5959
}
6060

61+
@Test
62+
public void testFromCurrentContext() {
63+
StackTraceElement expectedData = (new Exception()).getStackTrace()[0];
64+
SourceLocation data = SourceLocation.fromCurrentContext(0);
65+
assertEquals(expectedData.getFileName(), data.getFile());
66+
assertEquals(expectedData.getMethodName(), data.getFunction());
67+
// mind the assertion is vs (expectedData.lineNumber + 1). it is because the source location
68+
// info of the expectedData is one line above the source location of the tested data.
69+
assertEquals(Long.valueOf(expectedData.getLineNumber() + 1), data.getLine());
70+
}
71+
72+
@Test(expected = IndexOutOfBoundsException.class)
73+
public void testFromCurrentContextWithNegativeLevel() {
74+
SourceLocation.fromCurrentContext(-1);
75+
}
76+
77+
@Test(expected = IndexOutOfBoundsException.class)
78+
public void testFromCurrentContextWithVeryLargeLevel() {
79+
SourceLocation.fromCurrentContext(10000);
80+
}
81+
6182
private void compareSourceLocation(SourceLocation expected, SourceLocation value) {
6283
assertEquals(expected, value);
6384
assertEquals(expected.getFile(), value.getFile());

0 commit comments

Comments
 (0)