Without further ado, let’s get straight to the code.
Copy the code code as follows:
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Java implementation is similar to __FILE__, __FUNC__, __LINE__, etc. in C/C++, which are mainly used for logging and other functions.
*
* @version 1.0
*
*/
public abstract class CommonFunction {
/**
* When printing logs, obtain the current program file name, line number, and method name. The output format is: [FileName | LineNumber | MethodName]
*
* @return
*/
public static String getFileLineMethod() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
StringBuffer toStringBuffer = new StringBuffer("[").append(
traceElement.getFileName()).append(" | ").append(
traceElement.getLineNumber()).append(" | ").append(
traceElement.getMethodName()).append("]");
return toStringBuffer.toString();
}
//Current file name
public static String _FILE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getFileName();
}
//Current method name
public static String _FUNC_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getMethodName();
}
//Current line number
public static int _LINE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getLineNumber();
}
// current time
public static String _TIME_() {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return sdf.format(now);
}
}