0

My OS is JellyBean.I have an android service that makes collecting Logcat logs and saving to txt file every 5 minutes. But there is a problem. First when i was coding without any threads.like that

@Override
public void onCreate()
{
        // TODO Auto-generated method stub
     Toast.makeText(getApplicationContext(), "Service Üretildi", 1).show();
     super.onCreate();
     process = Runtime.getRuntime()
                    .exec("logcat -d -b main -v time");
.
.   
.
}

There was no problem.Application collects system logs properly.But when i used thread in TimerTask It can logging only TestService's debug logs.What should i do?

public class TestService extends Service
 {

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Service Üretildi", 1).show();
        super.onCreate();
        runService();

    }

    private void runService()
    {
        timer.scheduleAtFixedRate(new runningTask(), 0, 300 * 1000);
    }

    private class runningTask extends TimerTask
    {

        @Override
        public void run()
        {
            Log.i("LOGTEST", "LOGTEST");

            try
            {
                process = Runtime.getRuntime()
                        .exec("logcat -d -b main -v time");
            }
            catch (IOException e)
            {
                Log.e("LOGTEST", e.getMessage());
            }

            Log.i("LOGTEST", "COLLECTED");
            bufferedReader = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line;
            try
            {
                while ((line = bufferedReader.readLine()) != null)
                {
                    log.append(line);
                    log.append("\n");
                }
                // log = stabilizing(log);
                bufferedReader.close();
            }
            catch (IOException e)
            {
                Log.e("LOGTEST", e.getMessage());
            }

            String logFilePath = Environment.getExternalStorageDirectory()
                    + File.separator + "AAAA" + i + ".txt";
            i++;
            File logFile = new File(logFilePath);
            if (!logFile.exists())
                try
                {
                    logFile.createNewFile();
                    FileOutputStream outStream = new FileOutputStream(logFile,
                            true);
                    byte[] buffer = log.toString().getBytes();

                    outStream.write(buffer);
                    outStream.close();
                }
                catch (IOException e)
                {
                    Log.e("LOGTEST", e.getMessage());
                }

                // Runtime.getRuntime().exec("logcat -c");
                finally
                {
                    process.destroy();;
                }

        }

    }



}
yfpayalan
  • 33
  • 1
  • 9

1 Answers1

0

My understanding is that it hasn't been possible to do this on a non-rooted device since the JellyBean release (4.2). If you root your device, you should be able to access the system logs. If you don't root your device, I imagine you can only gain access to logs generated from inside your application.

whitfin
  • 4,539
  • 6
  • 39
  • 67