Repository: google/mobly

Issue:
logpersist missing.
Seems like most devices don't have the logpersist service. Atleast not my Nexus 6P with latest Android 7.0.1. Any thoughts on how I can make this work?

```
$python hello.py -c config.json
[SampleTestBed] 12-11 13:27:46.351 INFO ==========> HelloWorldTest <==========
[SampleTestBed] 12-11 13:27:46.625 ERROR [AndroidDevice|DEVICE_ID_REDACTED] Failed to start adb logcat!
Traceback (most recent call last):
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 384, in start_services
    self.start_adb_logcat()
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 580, in start_adb_logcat
    self.adb.shell("logpersist.stop --clear")
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 137, in adb_call
    return self._exec_adb_cmd(clean_name, arg_str)
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 108, in _exec_adb_cmd
    return self._exec_cmd(' '.join((self.adb_str, name, arg_str)))
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 105, in _exec_cmd
    raise AdbError(cmd=cmd, stdout=out, stderr=err, ret_code=ret)
AdbError: Error executing adb cmd 'adb -s DEVICE_ID_REDACTED shell logpersist.stop --clear'. ret: 127, stdout: , stderr: /system/bin/sh: logpersist.stop: not found

[SampleTestBed] 12-11 13:27:46.626 ERROR [AndroidDevice|DEVICE_ID_REDACTED] Failed to start some services, abort!
Traceback (most recent call last):
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 129, in _start_services_on_ads
    ad.start_services(skip_sl4a=getattr(ad, KEY_SKIP_SL4A, False))
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 384, in start_services
    self.start_adb_logcat()
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 580, in start_adb_logcat
    self.adb.shell("logpersist.stop --clear")
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 137, in adb_call
    return self._exec_adb_cmd(clean_name, arg_str)
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 108, in _exec_adb_cmd
    return self._exec_cmd(' '.join((self.adb_str, name, arg_str)))
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 105, in _exec_cmd
    raise AdbError(cmd=cmd, stdout=out, stderr=err, ret_code=ret)
AdbError: Error executing adb cmd 'adb -s DEVICE_ID_REDACTED shell logpersist.stop --clear'. ret: 127, stdout: , stderr: /system/bin/sh: logpersist.stop: not found

[SampleTestBed] 12-11 13:27:46.735 ERROR Failed to initialize objects for controller AndroidDevice, abort!
Traceback (most recent call last):
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/test_runner.py", line 376, in register_controller
    objects = create(controller_config)
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 82, in create
    _start_services_on_ads(ads)
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 129, in _start_services_on_ads
    ad.start_services(skip_sl4a=getattr(ad, KEY_SKIP_SL4A, False))
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 384, in start_services
    self.start_adb_logcat()
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 580, in start_adb_logcat
    self.adb.shell("logpersist.stop --clear")
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 137, in adb_call
    return self._exec_adb_cmd(clean_name, arg_str)
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 108, in _exec_adb_cmd
    return self._exec_cmd(' '.join((self.adb_str, name, arg_str)))
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 105, in _exec_cmd
    raise AdbError(cmd=cmd, stdout=out, stderr=err, ret_code=ret)
AdbError: Error executing adb cmd 'adb -s DEVICE_ID_REDACTED shell logpersist.stop --clear'. ret: 127, stdout: , stderr: /system/bin/sh: logpersist.stop: not found

[SampleTestBed] 12-11 13:27:46.736 ERROR Failed to setup HelloWorldTest.
Traceback (most recent call last):
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/base_test.py", line 504, in run
    self._setup_class()
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/base_test.py", line 128, in _setup_class
    self.setup_class()
  File "hello.py", line 11, in setup_class
    self.ads = self.register_controller(android_device)
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/test_runner.py", line 376, in register_controller
    objects = create(controller_config)
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 82, in create
    _start_services_on_ads(ads)
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 129, in _start_services_on_ads
    ad.start_services(skip_sl4a=getattr(ad, KEY_SKIP_SL4A, False))
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 384, in start_services
    self.start_adb_logcat()
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device.py", line 580, in start_adb_logcat
    self.adb.shell("logpersist.stop --clear")
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 137, in adb_call
    return self._exec_adb_cmd(clean_name, arg_str)
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 108, in _exec_adb_cmd
    return self._exec_cmd(' '.join((self.adb_str, name, arg_str)))
  File "build/bdist.macosx-10.11-x86_64/egg/mobly/controllers/android_device_lib/adb.py", line 105, in _exec_cmd
    raise AdbError(cmd=cmd, stdout=out, stderr=err, ret_code=ret)
AdbError: Error executing adb cmd 'adb -s DEVICE_ID_REDACTED shell logpersist.stop --clear'. ret: 127, stdout: , stderr: /system/bin/sh: logpersist.stop: not found

[SampleTestBed] 12-11 13:27:46.736 ERROR Error executing adb cmd 'adb -s DEVICE_ID_REDACTED shell logpersist.stop --clear'. ret: 127, stdout: , stderr: /system/bin/sh: logpersist.stop: not found

[SampleTestBed] 12-11 13:27:46.736 INFO [Test Case] setup_class FAIL
[SampleTestBed] 12-11 13:27:46.737 INFO Summary for test run SampleTestBed@12-11-2016_13-27-46-350: Error 0, Executed 1, Failed 1, Passed 0, Requested 1, Skipped 0
```


Discussion:
Thanks for reporting the issue!

What kind of build are you using? You can check with adb command:
`$ adb shell getprop ro.build.type`

I'll upload a patch to handle this better. Sorry about the trouble.
The patch will skip calls to logpersist for 'user' builds.

logpersist only works on 'userdebug' build, so if you want to store all log caches on your device, you'll need to use a 'userdebug' build on your phone.

Resolving patch:
diff --git a/mobly/controllers/android_device.py b/mobly/controllers/android_device.py
--- a/mobly/controllers/android_device.py
+++ b/mobly/controllers/android_device.py
@@ -575,10 +575,12 @@ def start_adb_logcat(self):
             raise Error(
               'Android device %s already has an adb logcat thread going on. '
               'Cannot start another one.' % self.serial)
-        # Disable adb log spam filter. Have to stop and clear settings first
-        # because 'start' doesn't support --clear option before Android N.
-        self.adb.shell("logpersist.stop --clear")
-        self.adb.shell("logpersist.start")
+        # Disable adb log spam filter for rootable. Have to stop and clear
+        # settings first because 'start' doesn't support --clear option before
+        # Android N.
+        if self.is_rootable:
+            self.adb.shell("logpersist.stop --clear")
+            self.adb.shell("logpersist.start")
         f_name = "adblog,{},{}.txt".format(self.model, self.serial)
         utils.create_dir(self.log_path)
         logcat_file_path = os.path.join(self.log_path, f_name)


Test patch: