Skip to content

Improve *fsharp-debug* logging #132

@juergenhoetzel

Description

@juergenhoetzel

Description

As already addressed here, I would like to improve fsharp-mode internal logging to make error analysis easier.

I adapted a similar solution from tramp-mode to get the following improvements:

  • log caller-function (when (>= fsharp-ac-debug 3)
  • supply format string instead of already formatted string
  • readable timestamps
  • ensure newline before log message

Code:

(defun fsharp--log (fmt-string &rest arguments)
  (when fsharp-ac-debug
    (with-current-buffer (get-buffer-create fsharp-ac--log-buf)
      (let* ((pt (point))
	     (atend (eq (point-max) (point)))
	     (now (current-time))
	     (tstr (concat (format-time-string "%T." now)
			   (format "%06d" (nth 2 now))))
	     (mstr (apply #'format-message fmt-string arguments))
	     fn)
        (goto-char (point-max))
        (unless atend
          (goto-char pt))
	;; always ensure newline (could be missing in format string)
	(unless (bolp)
	  (insert "\n"))
	;; insert function name
	(when (>= fsharp-ac-debug 3)
	  ;; Get function name from backtrace
	  ;; no high-order fun here because of effect on backtrace
	  (let ((btn 1) btf)
	    (while (not fn)
	      (setq btf (nth 1 (backtrace-frame btn)))
	      (if (not btf)
		  (setq fn "")
		(when (and (symbolp btf) (string-match "^fsharp-" (symbol-name btf))
			   (not (string-match "^fsharp--log$" (symbol-name btf))))
		  (setq fn (symbol-name btf)))
		(setq btn (1+ btn))))))
	(insert (format "%s%s: %s" tstr (if fn (concat " (" fn ")") "") mstr))))))

For the latest issue we had, this would improve logging code/output from:

(fsharp-ac--log (format "Malformed JSON: %s" (buffer-substring-no-properties (point-min) (point-max))))
1496488271.045965: Malformed JSON: non-IL or abstract method with non-zero RVA
1496488271.0676157: non-IL or abstract method with non-zero RVA

to

(fsharp--log "Malformed JSON: %s" (buffer-substring-no-properties (point-min) (point-max)))
13:16:18.365914 (fsharp-ac--get-msg): Malformed JSON: non-IL or abstract method with non-zero RVA

or (< fsharp-ac-debug 3):

13:32:24.149494: Malformed JSON: non-IL or abstract method with non-zero RVA

If your are OK with that I can create a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions