Skip to content

Commit

Permalink
Add logging functions for debugging. Resolves quil#315.
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonygalea committed Jul 30, 2019
1 parent 84cd0f7 commit a1c6b3e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/cljc/quil/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,13 @@
"Retrieve sketch-specific state-atom. All changes to the
atom will be reflected in the state.
Example:
```
(set-state! :foo 1)
(state :foo) ;=> 1
(swap! (state-atom) update-in [:foo] inc)
(state :foo) ;=> 2"
(state :foo) ;=> 2
```"
[]
#?(:clj (-> (ap/current-applet) meta :state)
:cljs (. (ap/current-applet) -quil)))
Expand All @@ -202,13 +205,16 @@
:subcategory nil
:added "1.0"}
state
"Retrieve sketch-specific state by key. Must initially call
set-state! to store state. If no parameter passed whole
"Retrieve sketch-specific state by `key`. Must initially call
[[set-state!]] to store state. If no parameter is passed the whole
state map is returned.
Example:
```
(set-state! :foo 1)
(state :foo) ;=> 1
(state) ;=> {:foo 1}"
(state) ;=> {:foo 1}
```"
([] @(state-atom))

([key] (let [state (state)]
Expand Down Expand Up @@ -3362,6 +3368,33 @@
(let [mode (u/resolve-constant-key mode rect-modes)]
(.rectMode (current-graphics) mode)))

(defn
^{:requires-bindings true
:processing-name nil
:p5js-name nil
:category "Debugging"
:added "3.1.0"}
print-first-n
"Prints the provided arguments for the first `n` iterations."
[n & more]
(when (<= n (frame-count))
(apply print more)))

(defn
^{:requires-bindings true
:processing-name nil
:p5js-name nil
:category "Debugging"
:added "3.1.0"}
print-every-n-millisec
"Prints the provided arguments every `n` milliseconds."
[n & more]
(let [elapsed (millis)
state (internal-state)]
(when (< n (- elapsed (get @state :last-time)))
(swap! state assoc :last-time elapsed)
(apply print more))))

(defn
^{:requires-bindings true
:processing-name "red()"
Expand Down
1 change: 1 addition & 0 deletions src/cljc/quil/snippets/all_snippets.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

;;; Require all snippets.
quil.snippets.image
quil.snippets.debugging
quil.snippets.environment
quil.snippets.input
quil.snippets.output
Expand Down
20 changes: 20 additions & 0 deletions src/cljc/quil/snippets/debugging.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns quil.snippets.debugging
(:require #?(:clj [quil.snippets.macro :refer [defsnippet]])
[quil.core :as q :include-macros true]
quil.snippets.all-snippets-internal)
#?(:cljs
(:use-macros [quil.snippets.macro :only [defsnippet]])))

(defsnippet print-first-n
["print-first-n"]
{}

(q/background 255)
(q/print-first-n 5 "foo"))

(defsnippet print-every-n-millisec
["print-every-n-millisec"]
{}

(q/background 255)
(q/print-every-n-millisec 1000 "foo"))

0 comments on commit a1c6b3e

Please sign in to comment.