본문 바로가기
Programming/Android Java

앱에서 Logcat 보여주기 #2

by 개Foot/Dog발?! 2014. 7. 26.

앞의 코드에 이어서

내가 테스트용으로 잠깐 만들었던

주요 코드를 적어본다.



    @Override
    public void run() {
        // TODO Auto-generated method stub
        
        final String result = getLog(this);
        
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (testView != null) {
                        testView.setText(result);
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (testButton != null) {
                        testButton.setText("START LOG");
                    }
                    
                    if (harvestLog != null) {
                        harvestLog.setVisibility(View.GONE);
                        harvestLog.incrementProgressBy(0);
                    }
                    if (testView != null) {
                        testView.setVisibility(View.VISIBLE);
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    public final String getLog(Context context) {
        String[] LOGCAT_CMD = new String[] {
                "logcat",
                "-d",
                "*:V" };
        Process logcatProc = null;
        
        try {
            logcatProc = Runtime.getRuntime().exec(LOGCAT_CMD);
        }
        catch (IOException e) {
            e.printStackTrace();
            return "";
        }

        BufferedReader reader = null;

        String lineSeparatoer = System.getProperty("line.separator");

        StringBuilder strOutput = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), BUFFER_SIZE);

            String line;

            while ((line = reader.readLine()) != null) {
                strOutput.append(line);
                strOutput.append(lineSeparatoer);
                
                if(FORCE_BREAK){
                    break;
                }
            }
            
            reader.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally       {
            if (reader != null) {
                try {
                    reader.close();
                }
                catch (IOException e) {}
            }
            
            logcatProc.destroy();
            logcatProc = null;
        }

        FORCE_BREAK = false;
        return strOutput.toString();
    }



'Programming > Android Java' 카테고리의 다른 글

TLS / SSL (보안소켓) 구현 in Android (1)  (0) 2014.08.10
pc간의 ssl 보안소켓통신  (0) 2014.08.10
앱에서 Logcat 보여주기  (0) 2014.07.26
TextView 줄바꿈을 글자 단위로 하기  (0) 2014.07.26
WakeLock release  (0) 2014.07.25