public class Debug {
public static int line(Exception e) {
StackTraceElement[] trace = e.getStackTrace();
if (trace == null || trace.length == 0)
return -1; //
return trace[0].getLineNumber();
}
public static String fun(Exception e) {
StackTraceElement[] trace = e.getStackTrace();
if (trace == null)
return ""; //
return trace[0].getMethodName();
}
}

  使用场景:

public class test {
public static String DI(Exception e) {
return Debug.line(e)+"|"+Debug.fun(e)+"|";
}
public test() {
Log.d(TAG, DI(new Exception())); //这里就输出我们需要的debug信息了
}
}

  

另一种使用形式:

 public class DebugInfo extends Exception {
public int line() {
StackTraceElement[] trace = getStackTrace();
if (trace == null || trace.length == 0) {
return -1;
}
return trace[0].getLineNumber();
} public String fun() {
StackTraceElement[] trace = getStackTrace();
if (trace == null || trace.length == 0) {
return "";
}
return trace[0].getMethodName();
} public DebugInfo() {
super();
} @Override
public String toString() {
return line() + "|" + fun() + "|";
}
}

使用方法

Log.d(TAG, new DebugInfo() + "hello world!");
 public class DebugInfo {
public static int line(StackTraceElement e) {
return e.getLineNumber();
} public static String method(StackTraceElement e) {
return e.getMethodName();
} public static String info(StackTraceElement e) {
String ret = line(e) + "|" + method(e) + "|";
return ret;
}
}

android开发中怎么通过Log函数输出当前行号和当前函数名的更多相关文章

  1. 【转载】Eclipse:Android开发中如何查看System.out.println的输出内容

    Android开发中在代码中通过System.out.println的输出内容不知道去哪了,在console视图中看不到.而通过Log.i之类的要在Logcat视图中看到,夹杂了太多的其它App及底层 ...

  2. Android开发中,那些让您觉得相见恨晚的方法、类或接口

    Android开发中,那些让你觉得相见恨晚的方法.类或接口本篇文章内容提取自知乎Android开发中,有哪些让你觉得相见恨晚的方法.类或接口?,其实有一部是JAVA的,但是在android开发中也算常 ...

  3. Android开发中常见的设计模式

    对于开发人员来说,设计模式有时候就是一道坎,但是设计模式又非常有用,过了这道坎,它可以让你水平提高一个档次.而在android开发中,必要的了解一些设计模式又是非常有必要的.对于想系统的学习设计模式的 ...

  4. Intent 对象在 Android 开发中的应用

    转自(http://www.ibm.com/developerworks/cn/opensource/os-cn-android-intent/) Android 是一个开放性移动开发平台,运行在该平 ...

  5. 在android开发中使用multdex的方法-IT蓝豹为你整理

    Android系统在安装应用时,往往需要优化Dex,而由于处理工具DexOpt对id数目的限制,导致其处理的数目不能超过65536个,因此在Android开发中,需要使用到MultiDex来解决这个问 ...

  6. android开发中的5种存储数据方式

    数据存储在开发中是使用最频繁的,根据不同的情况选择不同的存储数据方式对于提高开发效率很有帮助.下面笔者在主要介绍Android平台中实现数据存储的5种方式. 1.使用SharedPreferences ...

  7. Android学习探索之Java 8 在Android 开发中的应用

    前言: Java 8推出已经将近2年多了,引入很多革命性变化,加入了函数式编程的特征,使基于行为的编程成为可能,同时减化了各种设计模式的实现方式,是Java有史以来最重要的更新.但是Android上, ...

  8. Android开发中的输入合法性检验

    Why ? 合法性检查对于程序的健壮性具有重要作用.在Android开发中,良好的合法性检查设计机制可以使程序更加清晰,产生bug更少,交互更加友好. What ? 合法性检查的目的在于确定边界.对于 ...

  9. 怎样实现了捕获应用中的日志在android开发中

    怎样实现了捕获应用中的日志在android开发中,大家可研究一下. Process mLogcatProc = null; BufferedReader reader = null; try { mL ...

随机推荐

  1. generate alphanumeric serial number

    generate alphanumeric serial number with the following BADI / Customer Exit: Name of Enhancement : I ...

  2. android IPC通信(上)-sharedUserId&&Messenger

    看了一本书,上面有一章解说了IPC(Inter-Process Communication,进程间通信)通信.决定结合曾经的一篇博客android 两个应用之间的通信与调用和自己的理解来好好整理总结一 ...

  3. dsp-asic-fpga

    DSP是在模拟信号变换成数字信号以后进行高速实时处理的专用处理器,它采用的是哈佛设计,即数据总线和地址总线分开,使程序和数据分别存储在两个分开的空间,允许取指令和执行指令完全重叠,也就是说在执行上一条 ...

  4. stringByAppendingPathComponent和stringByAppendingString 的区别

    stringByAppendingPathComponent和stringByAppendingString 的区别   stringByAppendingPathComponent NSString ...

  5. RuntimeWarning: Parent module 'test_project.test_case' not found while handling absolute

    1.Pycharm2016.3.2,导入unittest框架后,运行脚本总是warming,但不影响脚本具体执行 2.通过网上查询,将"C:\Program Files\JetBrains\ ...

  6. BZOJ 4316: 小C的独立集 仙人掌 + 树形DP

    4316: 小C的独立集 Time Limit: 10 Sec  Memory Limit: 128 MB Description 图论小王子小C经常虐菜,特别是在图论方面,经常把小D虐得很惨很惨. ...

  7. 【NOIP 模拟赛】 道路

    题目描述在二维坐标平面里有 N 个整数点,信息班某一巨佬要访问这 N 个点.刚开始巨佬在点(0,0)处. 每一步,巨佬可以走到上.下.左.右四个点.即假设巨佬当前所在点的坐标是(x,y),那么它下一步 ...

  8. jQuery处理点击父级checkbox所有子级checkbox都选中,取消选中所有子级checkbox都取消

    注意,每个foreach标签内部都加一个div用来区分各个层次关系,模板代码如下: <foreach name='node' item='v'> <div class='a' ali ...

  9. Disruptor学习杂记

    慎入,有点乱,只是学习记录,disruptor_2.10.4 1.Disruptor对象有一个EventProcessorRepository对象   2.EventProcessorReposito ...

  10. 10.19-10.20 test

    2016 10.19-10.20 两天  题目by mzx Day1: T1:loverfinding 题解:hash #include<iostream> #include<cst ...