0

I am able to save the logcat to external file with the link - Saving Logcat to a text file in Android Device

Is there any way to limit the file size? i.e. For e.g. I want the output file size shouldn't exceed 2MB. If it exceeds the limit, it should clear the existing file content and start writing the new content.

Thanks in advance.

Kameswari
  • 738
  • 7
  • 27

2 Answers2

0

You can try this:

File file = new File("text.txt");
long filesize = file.length();
long filesizeInKB = filesize / 1024;
long filesizeinMB=filesizeInKB/1024;
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
rachna
  • 124
  • 8
0

Instead of executing

 process = Runtime.getRuntime().exec("logcat -f " + logFile);

Add

 process = Runtime.getRuntime().exec("logcat -f " + logFile + " -r <kbytes> -n <count>");

Example for a maximum of 10 files, where each max size is 500kb :

 process = Runtime.getRuntime().exec("logcat -f " + logFile + " -r 500 -n 10");

You can also defined theses variables (500 and 10) in your environment variables to keep your code maintainable.

Source : https://developer.android.com/studio/command-line/logcat

Alexy
  • 21
  • 2