Add debug logging and messages

This commit is contained in:
contrapunctus 2022-01-09 15:58:52 +05:30
parent 43b924ca03
commit 57ba75f376
3 changed files with 37 additions and 4 deletions

View File

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3. Unified migration interface with command `chronometrist-migrate`.
4. New custom variable `chronometrist-task-list`, to add/hide tasks without modifying the database.
5. New command `chronometrist-discard-active`, to discard the active interval.
6. Debug logging messages - to view them, set `warning-minimum-level` and `warning-minimum-log-level` to `:debug`.
### Fixed
1. File change detection code has been rewritten, hopefully fixing some uncommon `read` and `args out of range` errors.

View File

@ -5,7 +5,7 @@
;; Keywords: calendar
;; Homepage: https://tildegit.org/contrapunctus/chronometrist
;; Package-Requires: ((emacs "27.1") (dash "2.16.0") (seq "2.20") (ts "0.2"))
;; ;; Version: 0.9.0
;; Version: 0.9.0
;; This is free and unencumbered software released into the public domain.
;;
@ -238,6 +238,18 @@ Return new position of point."
(find-file (concat chronometrist-install-directory (plist-get chronometrist-doc-paths :lp))))
;; open-literate-source:1 ends here
;; [[file:chronometrist.org::*debug logging][debug logging:1]]
(defcustom chronometrist-debug-buffer "*chronometrist-debug*"
"Name of buffer to log debug messages to."
:type 'string
:group 'chronometrist)
(defmacro chronometrist-debug (format-string &rest args)
`(display-warning '(chronometrist)
(apply #'format (concat (format-time-string "[%T] ") ,format-string) (list ,@args))
:debug ,chronometrist-debug-buffer))
;; debug logging:1 ends here
;; [[file:chronometrist.org::*reset][reset:1]]
(defun chronometrist-reset ()
"Reset Chronometrist's internal state."
@ -1286,7 +1298,7 @@ FS-EVENT is the event passed by the `filenotify' library (see `file-notify-add-w
(chronometrist-file-change-type backend)))
(reset-watch-p (or (eq action 'deleted)
(eq action 'renamed))))
;; (message "chronometrist - file change type is %s" change)
(chronometrist-debug "File change type %s" change)
;; If only the last plist was changed, update hash table and
;; task list, otherwise clear and repopulate hash table.
(cond ((or reset-watch-p
@ -1371,6 +1383,7 @@ STREAM (which is the value of `current-buffer')."
;; [[file:chronometrist.org::*insert][insert:1]]
(cl-defmethod chronometrist-insert ((backend chronometrist-plist-backend) plist)
(chronometrist-backend-run-assertions backend)
(chronometrist-debug "Insert plist %s" plist)
(chronometrist-sexp-in-file (chronometrist-backend-file backend)
(goto-char (point-max))
;; If we're adding the first s-exp in the file, don't add a
@ -1384,6 +1397,7 @@ STREAM (which is the value of `current-buffer')."
;; [[file:chronometrist.org::*remove-last][remove-last:1]]
(cl-defmethod chronometrist-remove-last ((backend chronometrist-plist-backend))
(chronometrist-backend-run-assertions backend)
(chronometrist-debug "Remove last plist")
(chronometrist-sexp-in-file (chronometrist-backend-file backend)
(goto-char (point-max))
;; this condition should never really occur, since we insert a
@ -1489,6 +1503,7 @@ This is meant to be run in `chronometrist-file' when using an s-expression backe
;; [[file:chronometrist.org::*replace-last][replace-last:1]]
(cl-defmethod chronometrist-replace-last ((backend chronometrist-plist-backend) plist)
(chronometrist-debug "Replace last plist with %s" plist)
(chronometrist-sexp-in-file (chronometrist-backend-file backend)
(goto-char (chronometrist-remove-last backend))
(funcall chronometrist-sexp-pretty-print-function plist (current-buffer))

View File

@ -391,7 +391,7 @@ But I discovered that if I do that, =package-lint= says - =error: Couldn't parse
;; Keywords: calendar
;; Homepage: https://tildegit.org/contrapunctus/chronometrist
;; Package-Requires: ((emacs "27.1") (dash "2.16.0") (seq "2.20") (ts "0.2"))
;; ;; Version: 0.9.0
;; Version: 0.9.0
;; This is free and unencumbered software released into the public domain.
;;
@ -678,6 +678,19 @@ Return new position of point."
(find-file (concat chronometrist-install-directory (plist-get chronometrist-doc-paths :lp))))
#+END_SRC
*** debug logging
#+BEGIN_SRC emacs-lisp
(defcustom chronometrist-debug-buffer "*chronometrist-debug*"
"Name of buffer to log debug messages to."
:type 'string
:group 'chronometrist)
(defmacro chronometrist-debug (format-string &rest args)
`(display-warning '(chronometrist)
(apply #'format (concat (format-time-string "[%T] ") ,format-string) (list ,@args))
:debug ,chronometrist-debug-buffer))
#+END_SRC
** Data structures
:PROPERTIES:
:CUSTOM_ID: program-data-structures
@ -778,6 +791,7 @@ If REPLACE is non-nil, replace the last interval with PLIST."
(puthash date it hash-table))
hash-table))
#+END_SRC
*** last-date :reader:
#+BEGIN_SRC emacs-lisp
(defun chronometrist-events-last-date (hash-table)
@ -2043,7 +2057,7 @@ FS-EVENT is the event passed by the `filenotify' library (see `file-notify-add-w
(chronometrist-file-change-type backend)))
(reset-watch-p (or (eq action 'deleted)
(eq action 'renamed))))
;; (message "chronometrist - file change type is %s" change)
(chronometrist-debug "File change type %s" change)
;; If only the last plist was changed, update hash table and
;; task list, otherwise clear and repopulate hash table.
(cond ((or reset-watch-p
@ -2234,6 +2248,7 @@ In this backend, it's easier to implement this in terms of [[#program-backend-pl
#+BEGIN_SRC emacs-lisp
(cl-defmethod chronometrist-insert ((backend chronometrist-plist-backend) plist)
(chronometrist-backend-run-assertions backend)
(chronometrist-debug "Insert plist %s" plist)
(chronometrist-sexp-in-file (chronometrist-backend-file backend)
(goto-char (point-max))
;; If we're adding the first s-exp in the file, don't add a
@ -2248,6 +2263,7 @@ In this backend, it's easier to implement this in terms of [[#program-backend-pl
#+BEGIN_SRC emacs-lisp
(cl-defmethod chronometrist-remove-last ((backend chronometrist-plist-backend))
(chronometrist-backend-run-assertions backend)
(chronometrist-debug "Remove last plist")
(chronometrist-sexp-in-file (chronometrist-backend-file backend)
(goto-char (point-max))
;; this condition should never really occur, since we insert a
@ -2366,6 +2382,7 @@ This is meant to be run in `chronometrist-file' when using an s-expression backe
***** replace-last :writer:method:
#+BEGIN_SRC emacs-lisp
(cl-defmethod chronometrist-replace-last ((backend chronometrist-plist-backend) plist)
(chronometrist-debug "Replace last plist with %s" plist)
(chronometrist-sexp-in-file (chronometrist-backend-file backend)
(goto-char (chronometrist-remove-last backend))
(funcall chronometrist-sexp-pretty-print-function plist (current-buffer))