 
 (defn debug-print-message
   [_session msg]
-  #_(println msg))
+  (let [oops (select-keys msg [:error :throttled :throttled_reason])]
+    (if (every? (some-fn nil? false?) (keys oops))
+      (do ;; successful event/reply
+        #_(println msg))
+      (do ;; something's wrong
+        (println msg)))))
 
 (defn log-messages
   "Log message datas to file."


(defn auto-respond
  [session msg dispatch]
  (dosync
   (when-let [pre-handler (get dispatch :pre)]
     (pre-handler session msg))

    (fn [session _msg]
      (some-> session :state deref :logger :writer .close))}))

(defn find-last-logged
  "Find the most recently logged message."
  [logfile]
  (with-open [rdr (io/reader logfile :encoding "UTF-8")]
    (max-1 (fn compare-times [x y]
             (- (get-in x [:data :time])
                (get-in y [:data :time])))
           (json/parsed-seq rdr true))))

(defn -main
  "Demo connection to heim"
  [server room logfile]
  (let [w (io/writer logfile :encoding "UTF-8" :append true)
        ;; FIXME unused
        last-logged-id (:id (:data (find-last-logged logfile)))
        _ (println "Last logged message ID:" last-logged-id)
        session (run server room
                     {:logger {:lifecycle :waiting-for-snapshot
                               :path logfile
                               :writer w}}
                     logger-dispatch)]
    session))
