2016-04-24 14 views
1

私はAndroidアプリケーションでメッセージをログに記録するためにlogcatを使用しています。これらのメッセージにアクセスしてDialogFragmentのTextViewに表示したいと思います。メッセージのリストは非常に矛盾しており、ダイアログを開いたり閉じたりするたびに変更されます。それは完全な履歴を一度表示し、次に消去するときに表示され、時にはさらにメッセージを表示します。ダイアログを開くたびにすべてのメッセージを表示するために何かできることはありますか?間違った時間などでプロセスを実行していますか?または、バッファを別の場所で処理する必要がありますか?ありがとう。AndroidのTextViewに完全なlogcat履歴を印刷するにはどうすればよいですか?

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_view_logs: 
      // View Logs MenuItem tapped 
      if (logsDialogFragment == null) { 
       logsDialogFragment = new LogsDialogFragment(); 
      } 
      logsDialogFragment.show(getFragmentManager(), "dialog"); 
      return true; 
     case R.id.action_exit: 
      // Exit MenuItem tapped 
      finish(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

public class LogsDialogFragment extends DialogFragment { 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    Log.i(WifiDirectHandler.LOG_TAG, "Viewing Logs"); 
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()) 
     .setTitle(getString(R.string.title_logs)) 
     .setNegativeButton(getString(R.string.action_close), 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        dialog.dismiss(); 
       } 
      } 
     ); 

    LayoutInflater i = getActivity().getLayoutInflater(); 
    View rootView = i.inflate(R.layout.fragment_logs_dialog, null); 

    TextView logTextView = (TextView) rootView.findViewById(R.id.logTextView); 
    logTextView.setMovementMethod(new ScrollingMovementMethod()); 

    try { 
     Process process = Runtime.getRuntime().exec("logcat -d"); 
     BufferedReader bufferedReader = new BufferedReader(
       new InputStreamReader(process.getInputStream())); 

     StringBuilder log = new StringBuilder(); 
     String line; 
     while ((line = bufferedReader.readLine()) != null) { 
      if (line.contains(WifiDirectHandler.LOG_TAG)){ 
       // Removes log tag and PID from the log line 
       log.append(line.substring(line.indexOf(": ") + 2)).append("\n"); 
      } 
     } 
     logTextView.setText(log.toString()); 
    } catch (IOException e) { 
    } 

    dialogBuilder.setView(rootView); 
    return dialogBuilder.create(); 
} 
} 
+0

は結果を示す 'logTextView'ですか? –

+0

はい、ダイアログを開いて閉じるたびに結果が異なります。 – Brendan

+0

結果は同じではありません、その自然な、ログの猫は違う、冗長を参照してください、あなたは同じを得ることはできません.. –

答えて

0

メンバー変数に履歴を保存し、ダイアログを開くたびに新しいログ行だけを追加してしまいました。

public class LogsDialogFragment extends DialogFragment { 

    private StringBuilder log = new StringBuilder(); 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Sets the Layout for the UI 
     LayoutInflater i = getActivity().getLayoutInflater(); 
     View rootView = i.inflate(R.layout.fragment_logs_dialog, null); 

     TextView logTextView = (TextView) rootView.findViewById(R.id.logTextView); 
     logTextView.setMovementMethod(new ScrollingMovementMethod()); 

     try { 
      Process process = Runtime.getRuntime().exec("logcat -d"); 
      BufferedReader bufferedReader = new BufferedReader(
        new InputStreamReader(process.getInputStream())); 

      StringBuilder log = new StringBuilder(); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       if (line.contains(WifiDirectHandler.LOG_TAG)){ 
        // Removes log tag and PID from the log line 
        log.append(line.substring(line.indexOf(": ") + 2)).append("\n"); 
       } 
      } 

      this.log.append(log.toString().replace(this.log.toString(), "")); 
      logTextView.setText(this.log.toString()); 
     } catch (IOException e) { 
      Log.e("wifiDirectHandler", "Failure reading logcat"); 
     } 

     // Creates and returns the AlertDialog for the logs 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()) 
      .setTitle(getString(R.string.title_logs)) 
      .setNegativeButton(getString(R.string.action_close), 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         dialog.dismiss(); 
        } 
       } 
      ).setView(rootView); 
     return dialogBuilder.create(); 
    } 
} 
1
Please add permission in manifest for logcat <uses-permission android:name="android.permission.READ_LOGS" /> 
2

メッセージのためのlogcatバッファを使用すると、完全なログが必要な場合はそう古いメッセージはちょうど、logcatから消去されます、非常に大きなではありません - SDカード上のファイルにlogcatメッセージを保存して、それから情報を読んでください。このメソッドは現在のすべてのメッセージをファイルに保存します:

public static void saveLogcatToFile(Context context) { 
     String fileName = "yourlogname.log"; 
     File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + 
       "/YourAppName", fileName); 
     try { 
      @SuppressWarnings("unused") 
      Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

上記のコードは、ある時点でのログのスナップショットです。ログを継続的に書きたい場合は、FileLoggerを使用してJavaのLoggingを使用し、それに書き込みます。