I want to write a elisp script to filter a log command to output specific logs only.

  1. The log command will never exist till user press 'C-q'.
  2. The log output will be filtered by regex such as "test.*".
  3. The filtered output print to emacs buffer.

How can I implement that? I know shell-command-to-string but it's not proper since the log command not exit at all.


Use start-process and add a filter to process log output line by line:

  (defun line-split (real-filter)
    (let ((cum-string-sym (make-symbol "proc-filter-buff"))
          (newline (string-to-char "\n"))
          (string-indexof (lambda (string char start)
                            (loop for i from start below (length string)
                                  thereis (and (eq char (aref string i))
      (set cum-string-sym "")
      `(lambda (proc string)
         (setf string (concat ,cum-string-sym string))
         (let ((start 0) new-start)
           (while (setf new-start (funcall ,string-indexof string ,newline start))
             (funcall ,real-filter proc (substring string start new-start))
             (setf start (1+ new-start)))
           (setf ,cum-string-sym (substring string start))))))

  (defun my-line(proc line)
    (when (string-match "Headset" line)
      (print (format "----line: %s" line))))

  (defun get-log()
    (let ((proc-log nil)(procname "mylogcat")(logbuffer "logbuffer"))
      (setq proc-log (start-process procname logbuffer "adb" "logcat"))
      (set-process-filter proc-log
         (line-split (lambda (proc line) (my-line proc line))))))


This example will capture output of command "adb logcat", then print the lines which contains "Headset" keyword.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.