🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #19864

How to give option to send o/p to stdout or file?

#19864
From: Aldous Wilson
Date: Sun, 30 Nov 1997 00:00
56 lines
2254 bytes
I'm using JBuilder Pro to build a server based on the example
multi-threaded server in the excellent book Java Examples in a
Nutshell.

What I would like to do is have a command line switch, "-debug", on
starting the server whereby all output from the server goes to
stdout.  If the debug switch isn't supplied I would like all output
to go to a logfile by default.  I guess the relevant code is,

public class MyServer {
	public static void main(String[] args) {
		// ...
		Server s = new Server(System.out, 10) //Logstream,
						  // max connections
		// ...
	}
	// ...
}

public class Server {
	// ...
	public Server(OutputStream logStream, int maxConnections) {
		setLogStream(logStream);
		log("Starting server");
		// ...
	}
	
	public void setLogStream(OutputStream out) {
		if (out != null) 
			logStream = new PrintWriter(new
OutputStreamWriter(out));
		else logStream = null;
	}
	
	public synchronized void log(String s) {
		if (logStream != null) {
			logStream.println("...");
			logStream.flush();
		}
	}
}


If used as above with System.out called then it works as expected,
but I can't get it to work with a file.  I've tried using a
FileOuputStream, using System.setOut() with a PrintWriter object
amongst other less obvious things but nothing I've tried works.  

If anybody can help me to allow the option to specify where to send
output to I would be very grateful.  I'm sure it shouldn't be this
difficult, but the Java IO libraries appear, to me,  to be a maze.

Apologies for the length of this post and thanks for reading.

Aldous
Technical Details
Message-ID: <3484f887.20688828@nntp.netcomuk.co.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10a146,a122f52f98b70141,start
X-Google-Attributes: gid10a146,public
X-Google-Thread: f74b3,a122f52f98b70141,start
X-Google-Attributes: gidf74b3,public
From: aldous@netcomuk.co.uk (Aldous Wilson)
Subject: How to give option to send o/p to stdout or file?
Date: 1997/11/30
Message-ID: <3484f887.20688828@nntp.netcomuk.co.uk>
X-Deja-AN: 294015798
X-Complaints-To: abuse@corp.netcom.net.uk
X-Trace: taliesin.netcom.net.uk 880933111 25626 194.42.228.139 (30 Nov 1997 23:38:31 GMT)
Organization: None
Newsgroups: borland.public.jbuilder.java.language,comp.lang.java.programmer