Created
July 3, 2012 17:15
-
-
Save martintrojer/3041119 to your computer and use it in GitHub Desktop.
logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(let [id (java.util.UUID/randomUUID)] | |
(emit-timed {:id id :op :add}) | |
(emit-timed {:id id :cat :backend :op :send}) | |
(emit-timed {:id id :cat :backend :op :ack :parent-op :send}) | |
(emit-timed {:id id :cat :backend :op :sub :parent-op :send}) | |
(emit-timed {:id id :cat :distribution :op :contribute}) | |
(emit-timed {:id id :cat :distribution :op :chain-contribute}) | |
(emit-timed {:id id :cat :distribution :op :chain-event :parent-op :contribute}) | |
(emit-timed {:id id :op :done :parent-op :add}))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def log (agent {})) | |
(defn- add-log [id data time & corr-ids] | |
(send log #(let [keys (into [id] corr-ids) | |
log-node (get-in % keys {:entries []}) | |
entries (:entries log-node)] | |
(assoc-in % keys | |
(assoc log-node :entries | |
(conj entries {:time time | |
:data data})))))) | |
(defn emit | |
"Emit data to the log, with a given id and potential corr-ids" | |
[id data & corr-ids] | |
(apply add-log id data (System/nanoTime) corr-ids)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def log (agent [])) | |
(defn emit | |
"Emit data to the log" | |
[data] | |
(send log #(conj % data))) | |
(defn emit-timed | |
"Emit datas to the log, timestamp appended" | |
[data] | |
(emit (assoc data :time (System/nanoTime)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn get-ids | |
"Return a set of ids seen in the log" | |
[log] | |
(->> log | |
(map :id) | |
set)) | |
(defn get-log | |
"Get chronological logs for a given id" | |
[log id] | |
(->> log | |
(filter #(= (:id %) id)) | |
(sort-by :time))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn get-ids | |
"Get all ids in log" | |
[log] | |
(keys log)) | |
(defn- walk-nodes [node f path] | |
(when node | |
(f node path) | |
(doseq [child-id (filter #(not= % :entries) (keys node))] | |
(walk-nodes (get node child-id) f (conj path child-id))))) | |
(defn get-log | |
"Get all log entries for a given id in chronological oder" | |
[log id] | |
(let [evts (atom [])] | |
(walk-nodes (get log id) | |
(fn [n _] (swap! evts #(into % (:entries n)))) | |
[id]) | |
(->> evts (sort-by :time) (map :data)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment