728x90
반응형
SMALL

안드로이드에서 디버깅을 할 때 Logcat에 출력되는 내용들을 구분하기 위해 별도의 TAG를 작성합니다.

다음은 파일마다 별도의 TAG를 지정하지 않아도 Logcat에 출력된 클래스와 메소드, 줄 번호를 표시하는 소스코드입니다.

참조한 곳으로 이동(stackoverflow.com)


 

Logcat - Line number 출력하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class Debug {
 
    public static boolean D = true;
 
    private static final int LOG_V = 0;
    private static final int LOG_D = 1;
    private static final int LOG_I = 2;
    private static final int LOG_W = 3;
    private static final int LOG_E = 4;
 
    private static void log(int type, Exception e, String message)
    {
        if (D)
        {
            StackTraceElement l = e.getStackTrace()[0];
 
            String fullClassName = l.getClassName();
            String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
            String methodName = l.getMethodName();
            int lineNumber = l.getLineNumber();
 
            String tag = className + "." + methodName + "():" + lineNumber;
 
            switch (type) {
            case LOG_V:
                Log.v(tag, message);
                break;
            case LOG_D:
                Log.d(tag, message);
                break;
            case LOG_I:
                Log.i(tag, message);
                break;
            case LOG_W:
                Log.w(tag, message);
                break;
            case LOG_E:
                Log.e(tag, message);
                break;
            }
 
            e = null;
        }
    }
 
    public static void logv(Exception e, String message) {
        log(LOG_V, e, message);
    }
 
    public static void logd(Exception e, String message) {
        log(LOG_D, e,  message);
    }
 
    public static void logi(Exception e, String message) {
        log(LOG_I, e,  message);
    }
 
    public static void logw(Exception e, String message) {
        log(LOG_W, e,  message);
    }
 
    public static void loge(Exception e, String message) {
        log(LOG_E, e,  message);
    }
}

로그를 출력하는 부분에서 다음과 같이 사용하면 됩니다.

1
2
3
4
5
Debug.logv(new Exception(), "Something to print");
Debug.logd(new Exception(), "Something to print");
Debug.logi(new Exception(), "Something to print");
Debug.logw(new Exception(), "Something to print");
Debug.loge(new Exception(), "Something to print");

참고로 Debug 클래스 내에서 new Exception()을 생성하면 Logcat에 Debug 클래스가 TAG로 출력됩니다. 

 

 

 

출처 : http://www.florentio.com/android/logcat%EC%97%90-%EC%A4%84-%EB%B2%88%ED%98%B8-%EC%B6%9C%EB%A0%A5%ED%95%98%EA%B8%B0/

728x90
반응형
LIST

+ Recent posts