Android Log.d Print Line Number
Use the method with the Throwable at the end:
catch (Exception e){
Log.d("MyTag", "exception ->:", e);
}
Log Api
Get line number in log handler
There is no LogRecord (JDK1.4 - JDK1.7) method to get the line number. If no thread handoff is occurring in your handler or upstream you can create a new Throwable().getStackTrace() and use the java.lang.StackTraceElement API to get the line number.
Example code can be found in the JavaMail API at MailLogger.inferCaller()/isLoggerImplFrame(). If you have to deal with thread handoffs, then you would have to log the line number as a log record parameter. Keep in mind that computing the callsite is expensive in terms of performance.
Dynamically get the current line number
I was able to use the Thread.currentThread().getStackTrace() method to create a set of functions that work together to produce the line number of the code that called the first method, like so:
/** @return The line number of the code that ran this method
* @author Brian_Entei */
public static int getLineNumber() {
return ___8drrd3148796d_Xaf();
}
/** This methods name is ridiculous on purpose to prevent any other method
* names in the stack trace from potentially matching this one.
*
* @return The line number of the code that called the method that called
* this method(Should only be called by getLineNumber()).
* @author Brian_Entei */
private static int ___8drrd3148796d_Xaf() {
boolean thisOne = false;
int thisOneCountDown = 1;
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
for(StackTraceElement element : elements) {
String methodName = element.getMethodName();
int lineNum = element.getLineNumber();
if(thisOne && (thisOneCountDown == 0)) {
return lineNum;
} else if(thisOne) {
thisOneCountDown--;
}
if(methodName.equals("___8drrd3148796d_Xaf")) {
thisOne = true;
}
}
return -1;
}
Hope this helps! I put these in a utility class so that they are out of the way, but still easily accessible. The second method is private to prevent any other method other than the first method from calling it so that it always works correctly.
Print current line number in intellij
One way to do this is to get the stack trace element and call getLineNumber
.
public static int getCurrentLineNumber() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
Note that I used an index of 2
because that refers to the frame of the method that calls getCurrentLineNumber
. If you are just doing this inline, i.e. like this:
System.out.println(Thread.currentThread().getStackTrace()[1].getLineNumber());
You should use an index of 1
instead.
Note that an index of 0
refers to the frame of the getStackTrace
method.
Related Topics
Replace the Last Part of a String
Calculating Difference in Dates in Java
Spring Boot How to Hide Passwords in Properties File
Easy Way of Running the Same Junit Test Over and Over
How to Run a Class in a War from the Command Line
How to Get the Desktop Path in Java
What Are "Connecting Characters" in Java Identifiers
How to Solve Circular Reference in JSON Serializer Caused by Hibernate Bidirectional Mapping
"Loading Class Com.Mysql.Jdbc.Driver ... Is Deprecated" Message
Format File Size as Mb, Gb, etc
How to Do Union, Intersect, Difference and Reverse Data in Java
Java, Simplified Check If Int Array Contains Int