Skip to content

Commit

Permalink
Merge pull request #39 from brentonashworth/logging
Browse files Browse the repository at this point in the history
Added an ILogViewer protocol. Used to start and stop log views.
  • Loading branch information
candera committed Jan 3, 2012
2 parents 813722c + c16e652 commit e677d7d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/app/cljs/one/sample/logging.cljs
Expand Up @@ -18,9 +18,9 @@ For more information see library.logging."}

(comment
;; log to the console
(log/console-output)
(start-display (log/console-output))
;; log to to the "fancy" window
(log/fancy-output "main")
(start-display (log/fancy-output "main"))
;; change the logging level
(log/set-level logger :fine)
)
38 changes: 31 additions & 7 deletions src/lib/cljs/one/logging.cljs
Expand Up @@ -9,6 +9,10 @@
[goog.debug.FancyWindow :as fancy]
[goog.debug.Logger :as logger]))

(defprotocol ILogViewer
(start-display [this] "Start displaying log messages in this viewer.")
(stop-display [this] "Stop displaying log messages in this viewer."))

(def ^{:doc "Maps log level keywords to `goog.debug.Logger.Levels`."}
levels {:severe goog.debug.Logger.Level.SEVERE
:warning goog.debug.Logger.Level.WARNING
Expand Down Expand Up @@ -66,15 +70,35 @@
[logger level]
(.setLevel logger (get levels level goog.debug.Logger.Level.INFO)))

(extend-protocol ILogViewer

goog.debug.Console
(start-display [this]
(.setCapturing this true))
(stop-display [this]
(.setCapturing this false))

goog.debug.FancyWindow
(start-display [this]
(doto this
(.setEnabled true)
(.init ())))
(stop-display [this]
(.setCapturing this false)))

(defn console-output
"Direct log messages to the browser's `console` window."
"Returns a log viewer which will direct log messages to the
browser's `console` window. Use the `start-display` and
`stop-display` functions to start and stop printing log messages to
the console."
[]
(doto (goog.debug.Console.)
(.setCapturing true)))
(goog.debug.Console.))

(defn fancy-output
"Open a fancy logging window and direct log messages to it."
"Returns a log viewer which will open a fancy logging window and
direct log messages to it. Use the `start-display` and
`stop-display` functions to start and stop printing log messages in
this window."
[name]
(doto (goog.debug.FancyWindow. name)
(.setEnabled true)
(.init ())))
(goog.debug.FancyWindow. name))

0 comments on commit e677d7d

Please sign in to comment.