If that subject did not scare you off already you might be the right
person to comment.

To set the scene, I'm writing an application with an embedded web
server (using ring and jetty), and my srv.core module has a log
function defined as:

  (let [repl-out *out*]
    (defn log [msg & vals]
      (binding [*out* repl-out]
        (let [line (apply format msg vals)]
          (println line)))))

The reason I'm storing *out* in repl-out is that I also want to
capture output from all threads (including jetty's) and have it
displayed in my repl (and probably elsewhere in the future). Without
this kind of construction, output seems to get lost inside the emacs
repl, possibly related to Java's behaviour of redirecting output in
threads to /dev/null or similar.

This seemed to work fine, until I decided to get fancy. Leiningen
allows you to load up a repl specific namespace through it's "repl-
init" setting, and I figured this may be a nice place to create a
function to manually start up the jetty web server by calling one of
my functions inside the srv.core module.

My method in srv.core is nothing fancy, just:

  (defn srvstart []
    (log "Server started")
    (alter-var-root (var server)
                    (fn [v]
                      (run-jetty (app) {:port 8080 :join? false}))))

In my repl.helper module, I first tried:

  (ns repl.helper
    (:use clojure.repl
            srv.core))

  (defn replsrv []
    (srv.core/srvstart))

Worked fine, except my log output got lost. If I ran it in a terminal
based console "lein repl" style, the log output appeared, but if I ran
it inside emacs "clojure-jack-in" style, it did not.

I haven't figured out exactly why (yet anyway). It seems the *out*
value that got stored in repl-out when the module was pulled in by the
"use srv.core" statement in repl.helper stores a different value than
the final value *out* has when the repl is up and running, at least
inside the emacs repl.

I figured I would try to delay pulling in srv.core until it was
actually needed, and that seems to solve it, ending up with the
following code in repl.helper:

  (ns repl.helper
    (:use clojure.repl))

  (defn replsrv []
    (require 'srv.core)
    (@(resolve 'srv.core/srvstart)))

I have only vague theories about *out* getting set to something
unusable if pulled in during the first reading phase. Whether that is
correct behaviour or not, or if this is just a "feature" of how the
repl works inside emacs, I have no clue about.

If somebody with a clue would like to comment, please do.

Thanks,

Marius K.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to