3

I have some log statements in my app that are shown in Android Studio's LogCat if the app runs on an emulator device. E.g.:

public VocabularyTrainerModelImpl() {
        ...
  Log.d(TAG, "First line index is -1");
        ...
}

If understood it right the one suggested to run:

$ adb logcat -d > log.txt

to get the log file from a real device. How do I get the logfile from an Android device?

I connected my phone via USB to my PC and ran this command. But I couldn't see my log messages. How do I get them? If possible without any additional installations.

There is a bug in the app which occurs very seldom. So in this case I want to connect the phone to PC, extract the log file and analyze it.

ka3ak
  • 2,435
  • 2
  • 30
  • 57
  • Why minus? What's wrong with the question? – ka3ak Jul 21 '17 at 06:12
  • Do you want to write Log to the real Android device? – KeLiuyue Jul 21 '17 at 06:24
  • @KeLiuyue Yes. Isn't it the default? I thought it is and I just have to know the way how to extract them. – ka3ak Jul 21 '17 at 06:29
  • I don't know.But l use a class and method to write to the real Android device.If you need ,I will tell you. – KeLiuyue Jul 21 '17 at 06:37
  • adding `-e` will get the logcat from the emulator. Doing `-d` will do it from a physical device. -s SERIAL allows you to specify the device by serial number (in case there is more than one of either kind). You can get a list of devices with `adb devices` – fattire Jul 21 '17 at 06:53
  • @fattire As you might have seen I already use "logcat -d" and my phone is the only connected phone. The log.txt contains some messages, but not the messages that are logged inside the app. – ka3ak Jul 21 '17 at 07:10
  • What messages are there? – fattire Jul 21 '17 at 07:55
  • @fattire There are lots of messages, but neither of them contains the text "First line index is -1". If I use Log.d(...) statements in my app, are they written anywhere on the device or not? – ka3ak Jul 21 '17 at 07:59
  • They should be... Try piping the output of logcat to grep.. ie. adb logcat | grep "First line" – fattire Jul 21 '17 at 08:11
  • @fattire In this way I could see my debug messages. But if I try to save them to a file the command never returns: adb logcat > log.txt. – ka3ak Jul 21 '17 at 08:33

2 Answers2

3

I've found a solution here: https://stackoverflow.com/a/22802081/971355

The log file isn't created automatically. I have to write it myself by running logcat from the app.

public static void printLog(Context context){
    String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
    String command = "logcat -f "+ filename + " -v time *:V";

    Log.d(TAG, "command: " + command);

    try{
        Runtime.getRuntime().exec(command);
    }
    catch(IOException e){
        e.printStackTrace();
    }
}
ka3ak
  • 2,435
  • 2
  • 30
  • 57
0

In my project,I use this to solve the problem.

1.add this class

public class LogcatHelper {

private static LogcatHelper INSTANCE = null;
private static String PATH_LOGCAT;
private LogDumper mLogDumper = null;
private int mPId;

/**
 * init data
 */
public void init(Context context) {
    if (Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED)) {// sd first
        PATH_LOGCAT = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + File.separator + "logcat";
    } else {
        PATH_LOGCAT = context.getFilesDir().getAbsolutePath()
                + File.separator + "logcat";
    }
    File file = new File(PATH_LOGCAT);
    if (!file.exists()) {
        file.mkdirs();
    }
}

public static LogcatHelper getInstance(Context context) {
    if (INSTANCE == null) {
        INSTANCE = new LogcatHelper(context);
    }
    return INSTANCE;
}

private LogcatHelper(Context context) {
    init(context);
    mPId = android.os.Process.myPid();
}

public void start() {
    if (mLogDumper == null)
        mLogDumper = new LogDumper(String.valueOf(mPId), PATH_LOGCAT);
    mLogDumper.start();
}

public void stop() {
    if (mLogDumper != null) {
        mLogDumper.stopLogs();
        mLogDumper = null;
    }
}

private class LogDumper extends Thread {

    private Process logcatProc;
    private BufferedReader mReader = null;
    private boolean mRunning = true;
    String cmds = null;
    private String mPID;
    private FileOutputStream out = null;

    public LogDumper(String pid, String dir) {
        mPID = pid;
        try {
            out = new FileOutputStream(new File(dir, "logcat"
                    + getFileName() + ".log"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        /**
         *
         * Level:*:v , *:d , *:w , *:e , *:f , *:s
         *
         *
         * */

        // cmds = "logcat *:e *:w | grep \"(" + mPID + ")\""; // print e level and ilevel info
        // cmds = "logcat  | grep \"(" + mPID + ")\"";// print all
        // cmds = "logcat -s way";// print filter info
        cmds = "logcat *:e *:i | grep \"(" + mPID + ")\"";

    }

    public void stopLogs() {
        mRunning = false;
    }

    @Override
    public void run() {
        try {
            logcatProc = Runtime.getRuntime().exec(cmds);
            mReader = new BufferedReader(new InputStreamReader(
                    logcatProc.getInputStream()), 1024);
            String line = null;
            while (mRunning && (line = mReader.readLine()) != null) {
                if (!mRunning) {
                    break;
                }
                if (line.length() == 0) {
                    continue;
                }
                if (out != null && line.contains(mPID)) {
                    out.write((getDateEN() + "  " + line + "\n")
                            .getBytes());
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (logcatProc != null) {
                logcatProc.destroy();
                logcatProc = null;
            }
            if (mReader != null) {
                try {
                    mReader.close();
                    mReader = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                out = null;
            }

        }

    }

}

public static String getFileName() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    String date = format.format(new Date(System.currentTimeMillis()));
    return date;
}

public static String getDateEN() {
    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String date1 = format1.format(new Date(System.currentTimeMillis()));
    return date1;
}

}

  1. add code in the Application class

    LogcatHelper.getInstance((getApplicationContext())).start();

3.add permissions in the Application class

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                Process process = Runtime.getRuntime().exec("logcat -f "+logFile);
            }
        }

I hope I can help you.

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
  • Thank you. But I've found a solution here: https://stackoverflow.com/a/22802081/971355 Does it something differently than yours? After running it I was able to find my debug messages in the log file. – ka3ak Jul 21 '17 at 14:06