Iain
Iain

Reputation: 300

How to suppress output to stderr in a clojure program

I want to produce a command line version of a clojure library using clojure/tools.cli and lein bin. This works fine except I am getting output to stderr when I run the script. This particular library has functions that override some basic functions, so naturally there are warnings when the clojure code is compiled. I know this is generally a bad idea but after careful consideration in this case I believe it is the best way to go. How do I stop these messages from appearing every time I run the script?

I added slf4j-nop to the dependencies as recommended in the monger documentation to suppress unwanted messages from monger, and this works, but has no effect on the warnings from the clojure compiler.

I have also tried using slf4j-log as detailed here: suppress output from `clojure.tools.logging` but without success.

Here is some of the code:

project.clj

(defproject image-search "0.1.0-SNAPSHOT"
  :description "Search for images containing specific metadata"
  :url "http://github.com/soulflyer/image-search"
  :license {:name "Eclipse Public License"
        :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.7.0"]
                 [org.clojure/tools.cli "0.3.3"]
                 [org.slf4j/slf4j-nop "1.7.21"]
                 [image-lib "0.1.1-SNAPSHOT"]]
  :main image-search.command-line
  :bin {:name "image-search"
        :bin-path "~/bin"})

and command_line.clj:

(ns image-search.command-line
  (:require [image-search.core :refer [open all-images ifeq ifin ]]
            [clojure.tools.cli  :refer :all])
  (:gen-class))

(def cli-options
  [["-c" "--count" "Counts the results"]
   ["-D" "--database DATABASE" "specifies database to use"
    :default "photos"]
   ["-I" "--image-collection IMAGE-COLLECTION" "specifies the image collection"
    :default "images"]
   ["-K" "--keyword-collection KEYWORD-COLLECTION" "specifies the keyword collection"
    :default "keywords"]
   ["-h" "--help"]
   ["-i" "--iso ISO" "Search on ISO value"]
   ["-s" "--shutter SHUTTER-SPEED" "search on SHUTTER-SPEED"]
   ["-f" "--aperture APERTURE" "search on APERTURE"]
   ["-y" "--year YEAR" "search on YEAR"]
   ["-m" "--month MONTH" "search on MONTH"]
   ["-M" "--model MODEL" "search by camera model"]
   ["-p" "--project PROJECT" "search photos in PROJECT"]
   ["-k" "--keyword KEYWORD" "search for KEYWORD"]])

(defn print-count [pics]
  (println (count pics)))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (let [{:keys [options arguments errors summary]} (parse-opts args cli-options)
        output-function (if (:count options) print-count open)]

    (-> all-images
        (ifeq :ISO-Speed-Ratings   (:iso options))
        (ifeq :Year               (:year options))
        (ifeq :Month             (:month options))
        (ifin :Project         (:project options))
        (ifeq :F-Number       (:aperture options))
        (ifin :Keywords        (:keyword options))
        (ifin :Model             (:model options))
        (output-function))))

I run lein bin, then run the executable it produces and get something like this:

(master) image-search: image-search -i 640 -c
WARNING: or already refers to: #'clojure.core/or in namespace: image-search.core, being replaced by: #'image-search.core/or
WARNING: and already refers to: #'clojure.core/and in namespace: image-search.core, being replaced by: #'image-search.core/and

Note that the 2 warning are referring to functions I don't use in the command line version. I am not even including them. They are not in the list given to refer when I :require the library. They are, however, important to the library when I'm using it from the repl or cider. So I really don't want to rename them.

Upvotes: 1

Views: 725

Answers (1)

Piotrek Bzdyl
Piotrek Bzdyl

Reputation: 13175

You can remove the warning by adding following to your (ns image-search.core) declaration:

(ns image-search.core
  (:refer-clojure :exclude [or and])

With that change you can still use the original Clojure's or and and by fully qualifying them:

`clojure.core/or`
`clojure.core/and`

Without that change the compiler warns you that you are overriding your namespace binding of or and and as they are by default bound to functions from clojure.core/or and clojure/and vars.

Upvotes: 3

Related Questions