-;;; core-debug.el --- Spacemacs Core File
+;;; core-debug.el --- Spacemacs Core File  -*- lexical-binding: t; -*-
 ;;
 ;; Copyright (c) 2012-2014 Sylvain Benner
 ;; Copyright (c) 2014-2015 Sylvain Benner & Contributors

;; Keep debug-on-error on for stuff that is lazily loaded
(add-hook 'after-init-hook (lambda () (setq debug-on-error t)))

(when (member "--profile" command-line-args)
  (setq command-line-args (delete "--profile" command-line-args))
  (profiler-start 'cpu+mem)
  (add-hook 'after-init-hook
            (lambda ()

    (let ((start (current-time)) res delta)
      (setq res ad-do-it
            delta (float-time (time-since start)))
      (when (> delta spacemacs-debug-timer-threshold)
        (with-current-buffer "*load-times*"
          (goto-char (point-max))
          (insert (format "File %s: Loaded %s: %.3f sec\n"
                          load-file-name (ad-get-arg 0) delta))))
      res)))

(when spacemacs-debug-with-adv-timers
  (with-current-buffer (get-buffer-create "*load-times*")
    (insert (format "Measured times greater than %.3f sec:\n\n"
                    spacemacs-debug-timer-threshold)))

  (add-hook 'after-init-hook
            (lambda ()
              (with-current-buffer "*load-times*"
                (goto-char (point-max))
                (insert (format "[%.3f] Spacemacs finished initializing\n\n"
                                (float-time (time-since emacs-start-time)) )))))

  (advice-add 'load :around #'spacemacs//load-timer)
  (advice-add 'require :around #'spacemacs//load-timer)
  (advice-add 'package-initialize
	      :around
        (spacemacs||make-function-timer package-intialize))
  (advice-add 'configuration-layer/sync
              :around
              (spacemacs||make-function-timer configuration-layer/sync))
  ;; (advice-add 'configuration-layer/sync
  ;;             :around
  ;;             (spacemacs||make-function-profiler configuration-layer/sync))
  (advice-add 'configuration-layer//configure-package
	      :around
	      (spacemacs||make-function-timer configuration-layer//configure-package)))

(provide 'core-debug)
