Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.8 KiB

  1. ;;; ein-log.el --- Logging module for ein.el
  2. ;; Copyright (C) 2012- Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-log.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-log.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-log.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'ein-core)
  19. (defvar ein:log-all-buffer-name "*ein:log-all*")
  20. (defvar ein:log-level-def
  21. '(;; debugging
  22. (blather . 60) (trace . 50) (debug . 40)
  23. ;; information
  24. (verbose . 30) (info . 20)
  25. ;; errors
  26. (warn . 10) (error . 0))
  27. "Named logging levels.")
  28. ;; Some names are stolen from supervisord (http://supervisord.org/logging.html)
  29. (defvar ein:log-level 30)
  30. (defvar ein:log-message-level 20)
  31. (defvar ein:log-print-length 10 "`print-length' for `ein:log'")
  32. (defvar ein:log-print-level 1 "`print-level' for `ein:log'")
  33. (defvar ein:log-max-string 1000)
  34. (defun ein:log-set-level (level)
  35. (setq ein:log-level (ein:log-level-name-to-int level)))
  36. (defun ein:log-set-message-level (level)
  37. (setq ein:log-message-level (ein:log-level-name-to-int level)))
  38. (defun ein:log-level-int-to-name (int)
  39. (cl-loop for (n . i) in ein:log-level-def
  40. when (>= int i)
  41. return n
  42. finally 'error))
  43. (defun ein:log-level-name-to-int (name)
  44. (cdr (assq name ein:log-level-def)))
  45. (defsubst ein:log-strip-timestamp (msg)
  46. (replace-regexp-in-string "^[0-9: ]+" "" msg))
  47. (defun ein:log-wrapper (level func)
  48. (setq level (ein:log-level-name-to-int level))
  49. (when (<= level ein:log-level)
  50. (let* ((levname (ein:log-level-int-to-name level))
  51. (print-level ein:log-print-level)
  52. (print-length ein:log-print-length)
  53. (msg (format "%s: [%s] %s" (format-time-string "%H:%M:%S:%3N") levname (funcall func)))
  54. (orig-buffer (current-buffer)))
  55. (if (and ein:log-max-string
  56. (> (length msg) ein:log-max-string))
  57. (setq msg (substring msg 0 ein:log-max-string)))
  58. (ein:with-read-only-buffer (get-buffer-create ein:log-all-buffer-name)
  59. (goto-char (point-max))
  60. (insert msg (format " @%S" orig-buffer) "\n"))
  61. (when (<= level ein:log-message-level)
  62. (message "ein: %s" (ein:log-strip-timestamp msg))))))
  63. (defmacro ein:log (level string &rest args)
  64. (declare (indent 1))
  65. `(ein:log-wrapper ,level (lambda () (format ,string ,@args))))
  66. ;; FIXME: this variable must go to somewhere more central
  67. (defvar ein:debug nil
  68. "Set to non-`nil' to raise errors instead of suppressing it.
  69. Change the behavior of `ein:log-ignore-errors'.")
  70. (defmacro ein:log-ignore-errors (&rest body)
  71. "Execute BODY; if an error occurs, log the error and return nil.
  72. Otherwise, return result of last form in BODY."
  73. (declare (debug t) (indent 0))
  74. `(if ein:debug
  75. (progn ,@body)
  76. (condition-case err
  77. (progn ,@body)
  78. (error
  79. (ein:log 'debug "Error: %S" err)
  80. (ein:log 'error (error-message-string err))
  81. nil))))
  82. (defun ein:log-pop-to-request-buffer ()
  83. (interactive)
  84. (ein:aif (get-buffer request-log-buffer-name)
  85. (pop-to-buffer it)
  86. (message "No buffer named \"%s\"" request-log-buffer-name)))
  87. (defun ein:log-pop-to-all-buffer ()
  88. (interactive)
  89. (pop-to-buffer (get-buffer-create ein:log-all-buffer-name)))
  90. (provide 'ein-log)
  91. ;;; ein-log.el ends here