Possible to suppress some stderr output?

715 views
Skip to first unread message

bruce li

unread,
Mar 5, 2013, 3:56:56 AM3/5/13
to clo...@googlegroups.com
Hi, guys,

I'm currently playing clojure with android monkey runner. So far it works fine but just one unpleasant thing -- the android monkey runner default prints a lot of junk information to stderr such as:

March 05, 2013 4:46:47 pm com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

These printing operations are evaluated inside its library when I call a method such as IChimpDevice.touch.

I'm writing some wrappers around the api:

(defn send-touch [^IChimpDevice device x y]
  (.touch device x y TouchPressType/DOWN_AND_UP))

Is it possible to suppress the its output to stderr and make it something like(in pseudo code):
(with-stderr-discard
  (.touch device x y TouchPressType/DOWN_AND_UP))

I cannot discard the whole program's stderr output with commands like
java -jar xxxxxx.jar 2>/dev/null

since I need vital stack trace information from other places for debug purpose.

Any help would be appreciated.


Jim - FooBar();

unread,
Mar 5, 2013, 9:11:20 AM3/5/13
to clo...@googlegroups.com
I don't know if you can suppress it, but you can certainly ignore it
with 'without-str' which will give you what was supposed to be printed
out, as a string...then it's up to you what to do with it... :)

HTH,
Jim
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

bruce li

unread,
Mar 5, 2013, 9:24:45 AM3/5/13
to clo...@googlegroups.com
Thanks, Jim. But with-out-str only works for output to *out*(stdout), but not *err*(stderr). A quick test is:
(with-out-str
          (try
           (throw (Exception. "Hello"))
           (catch Exception e
                  (.printStackTrace e))))
which it returns: "" but the stack trace still gets printed out:
java.lang.Exception: Hello
    at user$eval2774$fn__2775.invoke(NO_SOURCE_FILE:1)
    at user$eval2774.invoke(NO_SOURCE_FILE:1)
    at clojure.lang.Compiler.eval(Compiler.java:6511)
    at clojure.lang.Compiler.eval(Compiler.java:6477)
    at clojure.core$eval.invoke(core.clj:2797)
    at clojure.main$repl$read_eval_print__6405.invoke(main.clj:245)
    at clojure.main$repl$fn__6410.invoke(main.clj:266)
    at clojure.main$repl.doInvoke(main.clj:266)
    at clojure.lang.RestFn.invoke(RestFn.java:1096)
    at clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__1402.invoke(interruptible_eval.clj:57)
    at clojure.lang.AFn.applyToHelper(AFn.java:159)
    at clojure.lang.AFn.applyTo(AFn.java:151)
    at clojure.core$apply.invoke(core.clj:601)
    at clojure.core$with_bindings_STAR_.doInvoke(core.clj:1771)
    at clojure.lang.RestFn.invoke(RestFn.java:425)
    at clojure.tools.nrepl.middleware.interruptible_eval$evaluate.invoke(interruptible_eval.clj:42)
    at clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__1443$fn__1445.invoke(interruptible_eval.clj:170)
    at clojure.core$comp$fn__4034.invoke(core.clj:2278)
    at clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__1436.invoke(interruptible_eval.clj:137)
    at clojure.lang.AFn.run(AFn.java:24)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722

2013/3/5 Jim - FooBar(); <jimpi...@gmail.com>

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.

Jim - FooBar();

unread,
Mar 5, 2013, 9:33:29 AM3/5/13
to clo...@googlegroups.com
aaa sorry...I guess I should have tested first...

Jim

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Jim - FooBar();

unread,
Mar 5, 2013, 9:35:37 AM3/5/13
to clo...@googlegroups.com
but then it's pretty trivial to adapt it:

(defmacro with-out-err-str
  "Evaluates exprs in a context in which *out* and *err* ire bound to a fresh
  StringWriter.  Returns the string created by any nested printing
  calls."
  {:added "1.0"}
  [& body]
  `(let [s# (new java.io.StringWriter)]
     (binding [*out* s# *err* s#]
       ~@body
       (str s#))))

Jim

Jim - FooBar();

unread,
Mar 5, 2013, 9:39:39 AM3/5/13
to clo...@googlegroups.com
still doesn't work though! strange...
why would you want to do this anyway?

Jim

bruce li

unread,
Mar 5, 2013, 9:50:07 AM3/5/13
to clo...@googlegroups.com
Well, when I pack my project into a stand alone jar and run java -jar myproject-standalone.jar inside a terminal, the Android controller's debug information gets printed out which clusters the terminal output, making other vital information hard to identify. Thus I would want to find a way to suppress its output.

BTW, I came up with an UGLY solution:

(let [err System/err]
  (System/setErr (java.io.PrintStream. (java.io.FileOutputStream. "/dev/null")))
  (try
    (throw (Exception. "Hello"))
    (catch Exception e (.printStackTrace e)))
  (System/setErr err))

This time, the stack trace gets suppressed. But it looks really UGLY and by no means, in efficient as it opens /dev/null everytime.

What is the difference between *err* and System/err?!

Jim - FooBar();

unread,
Mar 5, 2013, 9:57:50 AM3/5/13
to clo...@googlegroups.com
On 05/03/13 14:50, bruce li wrote:
What is the difference between *err* and System/err?!

bruce li

unread,
Mar 5, 2013, 10:07:02 AM3/5/13
to clo...@googlegroups.com
Thanks. Then I think I've caught some idea.

When binding *err* with other writer (say a StringWriter s), anything that use the *err* writer will actually write to s. But the original stderr stream is not redirected. It is still preserved as System/err. Thus anything that does not use the *err* writer, but instead use the System/err stream will not get affected.

Hope this makes sense.

2013/3/5 Jim - FooBar(); <jimpi...@gmail.com>
On 05/03/13 14:50, bruce li wrote:

Jim - FooBar();

unread,
Mar 5, 2013, 10:18:01 AM3/5/13
to clo...@googlegroups.com
I can see why you would think that but it would be the same story for *out* wouldn't it. Noone redirects System.out when you bind *out* to some writer, however still it works...
I don't exactly have time to look into this now but I may do this afternoon :)

Jim

bruce li

unread,
Mar 5, 2013, 10:28:31 AM3/5/13
to clo...@googlegroups.com
I think the reason why we could redirect the *out* or *err* is that, all Clojure functions staffs USE these 2 writers. But in places where it doesn't (such as places where it uses System/out, System/err directly), it would cause problem.

Here is a proof:
(with-out-str
          (with-open [out-writer (clojure.java.io/writer System/out)]
            (.write out-writer "hello")))
This statement returns empty string: ""
Which means the output is never captured because it does not use the *out* writer.
Reply all
Reply to author
Forward
0 new messages