3

I'm trying to get all my logcat, which I see in Android Studio (like here https://i.stack.imgur.com/sseEr.png), to save as a txt file directly on my device.

What I've tryed so far is this code in the onCreate:

File logFile = new File(LogcatDir.getAbsolutePath() + "/log.txt" );
try
{
    Process process = Runtime.getRuntime().exec( "logcat -c");
    process = Runtime.getRuntime().exec( "logcat -f " + logFile + " *:S MyActivity_BUT_WHAT_TO_PUT_HERE");
}
catch ( IOException e ) {e.printStackTrace();}

and in the manifest file:

<uses-permission android:name="android.permission.READ_LOGS" />

I intentionally did a parse Error like

long test = Long.parseLong("z");

but there was actually only this in the log.txt:

--------- beginning of /dev/log/main
--------- beginning of /dev/log/system

Does anyone know, what I have to code, that I get the whole logcat?

Pixel_95
  • 954
  • 2
  • 9
  • 21
  • Note that `READ_LOGS` is unavailable to ordinary Android SDK apps on Android 4.1 and higher: https://commonsware.com/blog/2012/07/12/read-logs-regression.html – CommonsWare Jan 13 '16 at 23:25
  • Try to not call the "logcat -c" since that flushes it – ivan Jan 13 '16 at 23:27
  • This post might help: http://stackoverflow.com/questions/1756296/android-writing-logs-to-text-file – bhiku Jan 13 '16 at 23:42
  • @ivan i don't call `logcat -c ` again so i guess i'm not deleting the file – Pixel_95 Jan 13 '16 at 23:49
  • @bhiku what these methods do, is writing manually into a file, but i wanna have the normal logcat in to a file ... so not by `appendToLog(String)` but by default the whole logcat – Pixel_95 Jan 13 '16 at 23:51
  • 1
    my best guess is that since you execute logcat -c first it flushes the log and then it saves it, do a test with Log.w("Before the flush","-c") Log.w("After the flush","-f") and see what happens that may be it – ivan Jan 13 '16 at 23:52
  • @ivan well now i did this: `Log.w("before","-c"); Process process = Runtime.getRuntime().exec( "logcat -c"); Log.w("After the flush","-f");`` But actually it's still the same in the `log.txt` It's the same if i go `System.err.print("err");` – Pixel_95 Jan 13 '16 at 23:57
  • try executing: logcat -d -f – ivan Jan 14 '16 at 00:02
  • @ivan doesn't effect anything :/ neither if I do `Process process = Runtime.getRuntime().exec( "logcat -d -f");` nor if I do `process = Runtime.getRuntime().exec( "logcat -d -f " + logFile + " *:S .main");` – Pixel_95 Jan 14 '16 at 08:42
  • @Pixel_95 ill try to do some tests around to see if i get it to work. – ivan Jan 14 '16 at 21:23

1 Answers1

8

This worked for me it saves it in the main storage with this code:

Log.w("before","Logcat save");
try {
  Process process = Runtime.getRuntime().exec("logcat -d");
  process = Runtime.getRuntime().exec( "logcat -f " + "/storage/emulated/0/"+"Logging.txt");
}catch(Exception e)
{
  e.printStackTrace();
}

enter image description here

enter image description here

ivan
  • 1,177
  • 8
  • 23
  • 1
    No prob :D @Pixel_95 – ivan Jan 14 '16 at 23:50
  • @ivan does this not require ? – Lv99Zubat Jun 01 '16 at 16:12
  • Oh according to this [video](https://www.youtube.com/watch?feature=player_embedded&v=WDDgoxvQsrQ#t=1369s) by google. JellyBean and up allows without a permission to read system log for your own app. I wish this statement was thrown around a lot more. – Lv99Zubat Jun 01 '16 at 17:38
  • Something to note: the exec command doesn't automatically create a directory for anyone that wanted to do ".../logs/log.txt", you'll have to create it beforehand. – Prof Dec 31 '16 at 16:00