   (lean-server-send-cmd-async (lean-cmd-valid) 'message))
 
 (defun lean-server-set-timer-for-event-handler ()
+  (lean-server-debug "set-timer-for-event-handler")
   (unless lean-global-retry-timer
     (setq lean-global-retry-timer
           (run-with-idle-timer

        (string-to-send (concat (lean-cmd-to-string cmd) "\n")))
    (lean-server-before-send-cmd cmd)
    ;; Logging
    (lean-server-log
     (string-join
      '("Send"
        "================"
        "%s"
        "================") "\n") string-to-send)
    ;; Trace
    (lean-server-trace
     (format "%s" string-to-send))

(defvar-local lean-server-trace-buffer-name "*lean-server-trace*")
(defvar-local lean-server-debug-buffer-name "*lean-server-debug*")
(defvar-local lean-server-option            "--server")
(defvar-local lean-server-trace-mode        nil)
(defvar-local lean-server-debug-mode        nil)

;; Log, Trace, Debug Function
;; ==========================
(defun lean-server-output-to-buffer (buffer-name format-string args)
  (with-current-buffer
        (get-buffer-create buffer-name)
      (save-selected-window
        (ignore-errors
          (select-window (get-buffer-window buffer-name t)))
        (goto-char (point-max))
        (insert (apply 'format format-string args)))))

(defun lean-server-log (format-string &rest args)
  "Display a message at the bottom of the *lean-server* buffer."
  (lean-server-output-to-buffer (lean-server-get-buffer)
                              (concat format-string)
                              args))

(defun lean-server-trace (format-string &rest args)
  "Display a message at the bottom of the *lean-server* buffer."
  (message "?\n")
  (when lean-server-trace-mode
    (message "!\n")
    (when lean-global-server-last-time-sent
      (let ((time-diff (- (float-time) lean-global-server-last-time-sent)))
        (lean-server-output-to-buffer lean-server-trace-buffer-name
                                    "SLEEP %i\n"
                                    `(,(truncate (* 1000 time-diff))))))
    (setq lean-global-server-last-time-sent (float-time))
    (lean-server-output-to-buffer lean-server-trace-buffer-name
                                format-string args)))

(defun lean-server-debug (format-string &rest args)
  "Display a message at the bottom of the *lean-server-debug* buffer."
  (when lean-server-debug-mode
    (lean-server-output-to-buffer lean-server-debug-buffer-name
                                (concat "%s -- " format-string "\n")
                                (cons (format-time-string "%H:%M:%S:%3N" (current-time))
                                      args))))

(defun lean-server-turn-on-debug-mode ()
  (interactive)
  (setq-local lean-server-debug-mode t))
(defun lean-server-turn-off-debug-mode ()
  (interactive)
  (setq-local lean-server-debug-mode nil))
(defun lean-server-toggle-debug-mode ()
  (interactive)
  (if lean-server-debug-mode
      (lean-server-turn-off-debug-mode)
    (lean-server-turn-on-debug-mode)))

(defun lean-server-turn-on-trace-mode ()
  (interactive)
  (setq-local lean-server-trace-mode t))
(defun lean-server-turn-off-trace-mode ()
  (interactive)
  (setq-local lean-server-trace-mode nil))
(defun lean-server-toggle-trace-mode ()
  (interactive)
  (if lean-server-trace-mode
      (lean-server-turn-off-trace-mode)
    (lean-server-turn-on-trace-mode)))


;; How to read data from an async process
;; ======================================
