1

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.

Drew
  • 77,472
  • 10
  • 114
  • 243
beetlej
  • 1,098
  • 1
  • 7
  • 20

1 Answers1

1

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))
                                               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))))))

  (get-log)

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

lucky1928
  • 1,666
  • 10
  • 28