Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1872202
  • 博文数量: 1000
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 7921
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-20 09:23
个人简介

storage R&D guy.

文章分类

全部博文(1000)

文章存档

2019年(5)

2017年(47)

2016年(38)

2015年(539)

2014年(193)

2013年(178)

分类: 服务器与存储

2015-10-20 18:57:41

import java.text.SimpleDateFormat; 
import java.util.Date; 

/** 
* Java实现类似C/C++中的__FILE__、__FUNC__、__LINE__等,主要用于日志等功能中。 

* @version 1.0 2011-07-13 

*/ 
public abstract class CommonFunction { 

/** 
* 打印日志时获取当前的程序文件名、行号、方法名 输出格式为:[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(); 


// 当前文件名 
public static String _FILE_() { 
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1]; 
return traceElement.getFileName(); 


// 当前方法名 
public static String _FUNC_() { 
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1]; 
return traceElement.getMethodName(); 


// 当前行号 
public static int _LINE_() { 
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1]; 
return traceElement.getLineNumber(); 


// 当前时间 
public static String _TIME_() { 
Date now = new Date(); 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
return sdf.format(now); 


public static void printCallStatck() {
    Throwable ex = new Throwable();
    StackTraceElement[] stackElements = ex.getStackTrace();
    if (stackElements != null) {
        for (int i = 0; i < stackElements.length; i++) {
            System.out.print(stackElements[i].getClassName()+"/t");
            System.out.print(stackElements[i].getFileName()+"/t");
            System.out.print(stackElements[i].getLineNumber()+"/t");
            System.out.println(stackElements[i].getMethodName());
            System.out.println("-----------------------------------");
        }
    }
 
}




public static void printzkStatck() {
Exception e = new Exception("---stack print---");
e.printStackTrace();
}

 

阅读(1013) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~