Permalink
Browse files

pulling network logging into separate project.

  • Loading branch information...
1 parent 7dcb40c commit 29729d5818027e8bc11b950f21ef6734174d6f06 @rosejn committed Feb 23, 2011
Showing with 9 additions and 147 deletions.
  1. +9 −6 src/logjam/core.clj
  2. +0 −113 src/logjam/net.clj
  3. +0 −28 test/logjam/net_test.clj
View
@@ -72,7 +72,7 @@
(apply str "[" label "] "
(interpose " " args))))))
-(defn- console-writer
+(defn console-writer
"Returns a basic console writer function."
[]
(let [c-out *out*]
@@ -91,7 +91,7 @@
(.write writer "\n")
(.flush writer)))))
-(defn- file-writer
+(defn file-writer
"Returns a file based writer function configured to write to the
file located at path."
[path]
@@ -110,6 +110,10 @@
(add-writer chan-key channel (console-writer))
chan-key))
+(defn system-writer
+ []
+ (fn [chan args] (.println System/out (log-msg chan args))))
+
(defn system
"Log output to the System/out.
@@ -118,8 +122,7 @@
([channel]
(system channel (gensym)))
([channel chan-key]
- (add-writer chan-key channel
- (fn [chan args] (.println System/out (log-msg chan args))))))
+ (add-writer chan-key channel (system-writer))))
(defn remove-writer
"Remove the writer registered with a channel-key for a channel."
@@ -132,8 +135,8 @@
(alter writers* dissoc channel)))))
(defn file
- ([channel path] (file (gensym) channel path))
- ([chan-key channel path]
+ ([channel path] (file channel path (gensym)))
+ ([channel path chan-key]
(let [channel (chan-name channel)]
(add-writer chan-key channel (file-writer path))
chan-key)))
View
@@ -1,113 +0,0 @@
-(ns logjam.net
- (require [logjam.core :as log]
- [clojure.contrib.server-socket :as sock])
- (:import
- [java.net Socket]
- [java.net InetAddress InetSocketAddress SocketAddress
- DatagramPacket DatagramSocket]
- [java.nio ByteBuffer CharBuffer]
- [java.nio.channels Channel DatagramChannel SelectionKey Selector
- ServerSocketChannel SocketChannel]
- [java.nio.charset Charset CharsetEncoder]))
-
-(def MAX-LOG-EVENT-SIZE 1024)
-(def SELECT-TIMEOUT 100)
-
-(def charset (Charset/forName "UTF-8"))
-
-(def ERROR (atom nil))
-
-(defn- server-loop
- [{:keys [listening? tcp-chan udp-chan selector tcp-clients] :as s}]
- (try
- (let [recv-buf (ByteBuffer/allocate MAX-LOG-EVENT-SIZE)
- decoder (.newDecoder charset)]
- (while @listening?
- (try
- (.select selector SELECT-TIMEOUT)
- (doseq [k (.selectedKeys selector)]
- (.clear recv-buf)
- (let [chan (.channel k)]
- (cond
- (and (.isAcceptable k)
- (= tcp-chan chan))
- (if-let [client (.accept tcp-chan)]
- (dosync (alter tcp-clients conj client)))
- (and (.isReadable k)
- (= udp-chan chan))
- (let [client-addr (.receive udp-chan recv-buf)]
- (when (pos? (.position recv-buf))
- (.flip recv-buf)
- (let [recv-str (.toString (.decode decoder recv-buf))
- {:keys [channel args]} (read-string recv-str)]
- (logjam.core/log-event channel args)))))))
- (catch java.io.IOException e
- (log/to :server "got IO error: " e)))))
- (catch Exception e
- (reset! ERROR e)
- (log/to :server "Got exception in server-loop: "
- (let [err (java.io.StringWriter.)]
- (binding [*err* err]
- (.printStackTrace e))
- err)))))
-
-(defn server [port]
- (let [listening? (atom true)
- addr (InetSocketAddress. port)
- tcp-chan (ServerSocketChannel/open)
- tcp-clients (ref #{})
- udp-chan (DatagramChannel/open)
- selector (Selector/open)
- s {:listening? listening?
- :tcp-chan tcp-chan
- :tcp-clients tcp-clients
- :udp-chan udp-chan
- :selector selector}
- server-thread (Thread. #(server-loop s))]
-
- (-> tcp-chan (.socket) (.bind addr))
- (doto tcp-chan
- (.configureBlocking false)
- (.register selector SelectionKey/OP_ACCEPT))
-
- (-> udp-chan (.socket) (.bind addr))
- (doto udp-chan
- (.configureBlocking false)
- (.register selector SelectionKey/OP_READ))
-
- (.start server-thread)
- s))
-
-(defn stop-server [{:keys [listening? tcp-chan udp-chan]}]
- (reset! listening? false)
- (.close tcp-chan)
- (.close udp-chan))
-
-(defn udp-writer [host port]
- (let [addr (InetAddress/getByName host)
- sock (DatagramSocket.)
- encoder (.newEncoder charset)]
- (fn [chan args]
- (if (= :close (first args))
- (.close sock)
- (let [msg (with-out-str (prn {:channel chan
- :args args}))
- bytes (.getBytes msg)
- size (count bytes)]
- (.send sock (DatagramPacket. bytes size addr port)))))))
-
-(defn- socket-writer
- [host port]
- (let [writer (io/writer (Socket. host port))]
- (fn [chan args]
- (.write writer (prn-str {:type :logjam-msg
- :channel chan
- :args args})))))
-
-(defn socket
- ([channel host port] (socket channel host port (gensym)))
- ([channel host port chan-key]
- (let [channel (chan-name channel)]
- (add-writer chan-key channel (socket-writer host port))
- chan-key)))
-
View
@@ -1,28 +0,0 @@
-(ns logjam.net-test
- (:use clojure.test
- clojure.stacktrace
- clojure.contrib.repl-utils)
- (:require [logjam.core :as log]
- [logjam.net :as net-log]))
-
-(def TEST-PORT 5656)
-
-(log/channel :foo :debug)
-(log/channel :bar :foo)
-
-(deftest basic-net-test
- (let [s (net-log/server TEST-PORT)]
- (try
- (log/add-writer :foo (net-log/udp-writer "localhost" TEST-PORT))
- (log/console :foo)
- (let [a (with-out-str
- (log/to :foo "foo")
- (log/to :bar "foo")
- (Thread/sleep 200))]
- (is (= a "[foo] foo\n[bar] foo\n")))
- (finally
- (net-log/stop-server s)))))
-
-(defn net-tests []
- (binding [*test-out* *out*]
- (run-tests 'logjam.net-test)))

0 comments on commit 29729d5

Please sign in to comment.