Merge pull request #178 from setrofim/master

Various fixes.
diff --git a/devlib/utils/android.py b/devlib/utils/android.py
index 170fda0..ce1ab0b 100644
--- a/devlib/utils/android.py
+++ b/devlib/utils/android.py
@@ -286,6 +286,10 @@
     """
     # TODO this is a hacky way to issue a adb command to all listed devices
 
+    # Ensure server is started so the 'daemon started successfully' message
+    # doesn't confuse the parsing below
+    adb_command(None, 'start-server', adb_server=adb_server)
+
     # The output of calling adb devices consists of a heading line then
     # a list of the devices sperated by new line
     # The last line is a blank new line. in otherwords, if there is a device found
@@ -533,6 +537,16 @@
         aapt = _env.aapt
 
 class LogcatMonitor(threading.Thread):
+    """
+    Helper class for monitoring Anroid's logcat
+
+    :param target: Android target to monitor
+    :type target: :class:`AndroidTarget`
+
+                    device. Logcat entries that don't match any will not be
+                    seen. If omitted, all entries will be sent to host.
+    :type regexps: list(str)
+    """
 
     FLUSH_SIZE = 1000
 
@@ -545,6 +559,7 @@
 
         self.target = target
 
+        self._started = threading.Event()
         self._stopped = threading.Event()
         self._match_found = threading.Event()
 
@@ -556,6 +571,12 @@
         self._regexps = regexps
 
     def start(self, outfile=None):
+        """
+        Start logcat and begin monitoring
+
+        :param outfile: Optional path to file to store all logcat entries
+        :type outfile: str
+        """
         if outfile:
             self._logfile = outfile
         else:
@@ -580,12 +601,21 @@
         logger.debug('logcat command ="{}"'.format(logcat_cmd))
         self._logcat = self.target.background(logcat_cmd)
 
+        self._started.set()
+
         while not self._stopped.is_set():
             line = self._logcat.stdout.readline(1024)
             if line:
                 self._add_line(line)
 
     def stop(self):
+        if not self.is_alive():
+            logger.warning('LogcatMonitor.stop called before start')
+            return
+
+        # Make sure we've started before we try to kill anything
+        self._started.wait()
+
         # Kill the underlying logcat process
         # This will unblock self._logcat.stdout.readline()
         host.kill_children(self._logcat.pid)
@@ -616,7 +646,7 @@
         with self._datalock:
             while not self._lines.empty():
                 self._lines.get()
-                
+
             with open(self._logfile, 'w') as fh:
                 pass
 
@@ -653,6 +683,15 @@
         """
         Search a line that matches a regexp in the logcat log
         Wait for it to appear if it's not found
+
+        :param regexp: regexp to search
+        :type regexp: str
+
+        :param timeout: Timeout in seconds, before rasing RuntimeError.
+                        ``None`` means wait indefinitely
+        :type timeout: number
+
+        :returns: List of matched strings
         """
         res = self.search(regexp)