0

I've redirected the logcat to file in my code when my app starts. But when my application is restarted, the redirecting code runs again and the result is that each line in the log is written twice.

How can I execute the command and make sure the child process dies when the parent dies?

String.format("logcat -f %s -r %d", filename.getAbsolutePath(), LOG_FILE_SIZE_KB);    
Runtime.getRuntime().exec(cmd);

How can i make sure that my app's logcat is redirected only once? (Whats happends if other app calls the logcat and redirects it to it's own file, will the check still work then?)

thanks!

Adibe7
  • 3,469
  • 7
  • 30
  • 36
  • Why do you need to redirect it to a file? And show some code, please. – Lukas Knuth Jun 19 '11 at 11:44
  • I need to send the file to the server. and i added the code that does the redirection. what do you think? – Adibe7 Jun 19 '11 at 11:48
  • The LogCat is meant to debug on your developing environment. If the App crashes on the users device and it's on the Market, the user can send a Stack-Trace to you. Anyways, can you show the whole method you're using this code in? (i guess it's the `onCreate`). – Lukas Knuth Jun 19 '11 at 11:56
  • This is the whole method, and i use it only for debugging purposes. the problem is that the second line (Runtime.getRuntime().exec(cmd);) is called twice, and i want to know how to check if it was already called on this device – Adibe7 Jun 19 '11 at 12:02
  • Look over this, this might help : http://stackoverflow.com/a/43364736/2046324 – Vishal Sharma Apr 12 '17 at 09:10

3 Answers3

1

If you only use LogCat for debugging purposes, you might want to read this.

After you Activated LogCat, you can open the LogCat-View in Eclipse which will then show you all the LogCat-Output, so you don't need to write it to a file in the first place.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • I need the files because i'm sending them to the server. I want to use real devices for the testing, is there no normal way to check if the logcat is already redirected to a file or not? – Adibe7 Jun 19 '11 at 12:32
  • If you want to use a Hardware-Device, you might want to read [this](http://developer.android.com/guide/developing/device.html). – Lukas Knuth Jun 19 '11 at 12:44
1

Two options:
a) Make a static global variable that will contain information weather Logcat was already redirected or not.
b) Make a file on the sdcard or in the app directory (xml or properties file) containing info weather LogCat was already redirected.

bbaja42
  • 2,099
  • 18
  • 34
-1
/** Redirects the log output to the SDCard.
 *  
 * make sure your app has the WRITE_EXTERNAL_STORAGE and READ_LOGS permissions - 
 *  or it won't allow it to read logs and write them to the sdcard.
 *  If the application doesn't have the permissions, there will be no exception
 *  and the program will continue regularly.
 */
public static void redirectOutputToFile()
{
    s_enableLogs = true;

    if (s_logcatProcess != null)
    {
        Logger log = new Logger("Logger");  

        log.info("redirectOutputToFile() called more then once, perhaps from service onCreate and onStart.");

        return;
    }

    try 
    {
        String path = Environment.getExternalStorageDirectory() + LOG_FILE_NAME;
        File filename = new File(path);

        filename.createNewFile();

        //http://www.linuxtopia.org/online_books/android/devguide/guide/developing/tools/android_adb_logcatoptions.html
        String cmd = String.format("logcat -v time -f %s -r %d -n %d", filename.getAbsolutePath(), LOG_FILE_SIZE_KB, LOG_FILE_ROTATIONS);    

        s_logcatProcess = Runtime.getRuntime().exec(cmd);
    } 
    catch (IOException e) 
    {       
        Logger log = new Logger("Logger");
        log.exception(e);
    }
}

/** Kills the logcat process that was created in the redirectOutputToFile() method. */
public static void killLogcatProcess()
{
    // first update the log mode state
    s_enableLogs = false;

    if (s_logcatProcess != null)
    {
        s_logcatProcess.destroy();
        s_logcatProcess = null;
    }
}
Adibe7
  • 3,469
  • 7
  • 30
  • 36
  • -1 This is horrid code & a terrible answer: 1) `Environment.getExternalStorageDirectory() + LOG_FILE_NAME;` Use the `File` constructor that accepts a `File` and a `String`. 2) In 1.5+ use a `ProcessBuilder`, which in turn makes it one step easier to.. 3) Implement *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html). – Andrew Thompson Jul 20 '11 at 13:29