ein-log.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. (eval-when-compile (require 'cl))
  19. (require 'ein-core)
  20. (defvar ein:log-all-buffer-name " *ein:log-all*")
  21. (defvar ein:log-level-def
  22. '(;; debugging
  23. (blather . 60) (trace . 50) (debug . 40)
  24. ;; information
  25. (verbose . 30) (info . 20)
  26. ;; errors
  27. (warn . 10) (error . 0))
  28. "Named logging levels.")
  29. ;; Some names are stolen from supervisord (http://supervisord.org/logging.html)
  30. (defvar ein:log-level 30)
  31. (defvar ein:log-message-level 20)
  32. (defvar ein:log-print-level 1 "`print-level' for `ein:log'")
  33. (defvar ein:log-print-length 10 "`print-length' for `ein:log'")
  34. (defvar ein:log-max-string 1000)
  35. (defun ein:log-set-level (level)
  36. (setq ein:log-level (ein:log-level-name-to-int level)))
  37. (defun ein:log-set-message-level (level)
  38. (setq ein:log-message-level (ein:log-level-name-to-int level)))
  39. (defun ein:log-level-int-to-name (int)
  40. (loop for (n . i) in ein:log-level-def
  41. when (>= int i)
  42. return n
  43. finally 'error))
  44. (defun ein:log-level-name-to-int (name)
  45. (cdr (assq name ein:log-level-def)))
  46. (defun ein:log-wrapper (level func)
  47. (setq level (ein:log-level-name-to-int level))
  48. (when (<= level ein:log-level)
  49. (let* ((levname (ein:log-level-int-to-name level))
  50. (print-level ein:log-print-level)
  51. (print-length ein:log-print-length)
  52. (msg (format "[%s] %s" levname (funcall func)))
  53. (orig-buffer (current-buffer)))
  54. (if (and ein:log-max-string
  55. (> (length msg) ein:log-max-string))
  56. (setq msg (substring msg 0 ein:log-max-string)))
  57. (ein:with-read-only-buffer (get-buffer-create ein:log-all-buffer-name)
  58. (goto-char (point-max))
  59. (insert msg (format " @%S" orig-buffer) "\n"))
  60. (when (<= level ein:log-message-level)
  61. (message "ein: %s" msg)))))
  62. (defmacro ein:log (level string &rest args)
  63. (declare (indent 1))
  64. `(ein:log-wrapper ,level (lambda () (format ,string ,@args))))
  65. ;; FIXME: this variable must go to somewhere more central
  66. (defvar ein:debug nil
  67. "Set to non-`nil' to raise errors instead of suppressing it.
  68. Change the behavior of `ein:log-ignore-errors'.")
  69. (defmacro ein:log-ignore-errors (&rest body)
  70. "Execute BODY; if an error occurs, log the error and return nil.
  71. Otherwise, return result of last form in BODY."
  72. (declare (debug t) (indent 0))
  73. `(if ein:debug
  74. (progn ,@body)
  75. (condition-case err
  76. (progn ,@body)
  77. (error
  78. (ein:log 'debug "Error: %S" err)
  79. (ein:log 'error (error-message-string err))
  80. nil))))
  81. (defun ein:log-pop-to-all-buffer ()
  82. (interactive)
  83. (pop-to-buffer (get-buffer-create ein:log-all-buffer-name)))
  84. (provide 'ein-log)
  85. ;;; ein-log.el ends here