Newsgroup: it.comp.java
Titolo: redirezione System.out per debug

se volessi redirigere su file quanto esce da System.out per tenere un log di
debug, come posso fare (in modo semplice quick and dirty senza usare un
sistema di log)?
grazie


Ciao, hai già provato ad usare le redirection di sistema tipo >   >> ??
Fammi sapere
MArco

 wrote in message
news:3AA8C129...@libero.it...


arrivano da classi che richiamo).
Dal momento in cui inserisci nel codice l'istruzione
     System.setOut(new PrintStream(new
FileOutputStream("path/del/file/di/log", true)));
tutto ciň che dai in pasto al System.out.println() viene ridiretto sul file
di log che hai indicato.
Se č ciň che cercavi buon lavoro,
Oliviero.

raccolgo anche messaggi che arrivano da classi che richiamo).
Puoi usare una classe come questa. Usando
LogPrintStream.useAsSystemOut("blahblah",new File("blahblah.log"))
ottieni quando ti serve con in piu' anche il tagging dell'output.
---------------------------------------------------------------------------
//Source file: c:/classes/org/sadun/util
package org.sadun.util;
import java.io.*;
import java.text.*;
import java.util.*;
/**
 * A PrintStream which tags output lines with a tag and a date
 * and optionally ties the output to a file.
 * It can be directly substituted to <tt>System.out</tt>, either directly
 * or by using the <tt>useAsSystemOut</tt> method.
 * <p>
 * (Note that for an EJB/ri implementation this option is not viable,
 * since the System.out which would be replaced is the one on the server;
 * it is therefore necessary to explicitly instantiate and use a
 * separate LogprintStream which embeds System.out)
 *
 * @version 1.0
 * @deprecated new code should use the logging framework provided by
 *             <a href="LoggingObject.html">LoggingObject</a> and
 *             <a href="log/package-summary.html">org.sadun.util.log</a> packages.
 */
public class LogPrintStream extends PrintStream 
{
   private String tag;
   private PrintStream ps;
   private StringBuffer buffer = new StringBuffer ();
 
   /**
    * @param ps
    * @param tag
    * @exception
    * @roseuid 3A19304802C0
    */
   public LogPrintStream(PrintStream ps, String tag) 
   {
   	super(ps, true);
 
this.ps
=ps;
   	this.tag=tag;
   }
 
   /**
    * @param ps
    * @param tag
    * @param File
    * @exception
    * @roseuid 3A19343E03E0
    */
   public LogPrintStream(PrintStream ps, String tag, File outputFile) 
   {
   	super(new PrintStream(new TieOutputStream(ps, getFileOutputStream(outputFile))), true);
 
this.ps
=new PrintStream(super.out);
   	this.tag=tag;
   }
 
   public String getTag() { return tag; }
   public void setTag(String tag) { this.tag=tag; }
 
   /**
    * @return String
    * @exception
    * @roseuid 3A1932160210
    */
   private String createEntry() 
   {
   	StringBuffer sb = new StringBuffer();
   	sb.append("[");
   	sb.append(tag);
   	sb.append(": ");
   	sb.append(DateFormat.getDateTimeInstance().format(new Date()));
   	sb.append("] ");
 
   	return sb.toString();
   }
 
   /**
    * @param s
    * @return void
    * @exception 
    * @roseuid 3A193EED00EA
    */
   public void print(String s) {
   	write(s);
   }
 
   public void print(int b) {
   	write(b);
   }
 
   public void print(long b) {
   	write(b);
   }
 
   public void print(float b) {
   	write(b);
   }
 
   public void print(double b) {
   	write(b);
   }
 
   public void print(char b) {
   	write(b);
   }
 
   public void print(byte b) {
   	write(b);
   } 
 
   public void print(Object obj) {
   	print(obj.toString());
   }
 
   private void write(String s) 
   {
	if (s == null) {
	    s = "null";
	}
		for(int i=0;i<s.length();i++) {
   		write(s.charAt(i));
   	}
   }
 
   public void write(char b) {
   	write((int)b);
   }
 
   public void write(byte b) {
   	write((int)b);
   }
 
   public void write(long b) {
   	write(String.valueOf(b));
   }
 
   public void write(double b) {
   	write(String.valueOf(b));
   }
 
   public void write(float b) {
   	write(String.valueOf(b));
   }
 
   public void write(char buf[]) {
   	for(int i=0;i<buf.length;i++)
   		write(buf[i]);
 
   }
 
   public void write(byte buf[], int off, int len) {
   	for(int i=0;i<len;i++)
   		write(buf[off+i]);
   }
 
   public void flush() {
   	newLine();
   	ps.flush();
   }
 
   /**
    * @param b
    * @return void
    * @exception 
    * @roseuid 3A193EED0144
    */
   public void write(int b) 
   {
   	String ls = System.getProperty("line.separator");
   	int lsl = ls.length();
   	buffer.append((char)b);
   	int bl=buffer.length();
   	if (bl >= lsl) {
   		String tk = buffer.substring(bl-lsl);
	   	if(ls.equals(tk)) {
	   		buffer.delete(bl-lsl, bl);
	   		newLine();
	   	}
	   }
   }
 
   /**
    * @return void
    * @exception 
    * @roseuid 3A193EED019E
    */
   private void newLine() 
   {
   	ps.print(createEntry());
   	ps.println(buffer.toString());
   	buffer=new StringBuffer();
   }
 
   /**
    * @param s
    * @return void
    * @exception 
    * @roseuid 3A193EED01DA
    */
   public void println(String s) 
   {
   	for(int i=0;i<s.length();i++) {
   		write(s.charAt(i));
   	}
   	newLine();
   }
 
   /**
    * @param f
    * @return FileOutputStream
    * @exception 
    * @roseuid 3A193EED0234
    */
   private static FileOutputStream getFileOutputStream(File f) 
   {
		try {
 
			PrintWriter fw = new PrintWriter(new BufferedWriter(new FileWriter(f.getAbsolutePath(), true)));
			String nl=System.getProperty("line.separator");
			fw.println(nl+"Logging starts at "+DateFormat.getDateTimeInstance().format(new Date()));
			fw.close();
 
			FileOutputStream fos = new FileOutputStream(f.getAbsolutePath(), true);
			return fos;
 
		} catch (IOException e) {
			throw new RuntimeException(e.getClass()+": "+e.getMessage());
		}
   }
 
   /**
    * A LogPrintStream is created, tied to the given log file, 
    * and substituted to System.out.
    *
    * @param tag the tag to use for each output line
    * @param logFile the log file to which the output is sent besides the original standard output
    * @roseuid 3A1A61560161
    */
   public static void useAsSystemOut(String tag, File logFile) 
   {
   	if (System.out instanceof LogPrintStream) throw new RuntimeException("Can't call useAsSystemOut twice");
   	java.lang.System.setOut(
   		new no.gdpm.util.LogPrintStream(
   			java.lang.System.out,
   			tag,
   			logFile
   		)
   	);
   }
 
}
-- 
God is Real, unless declared Integer.
ObjectZone - 
http://space.tin.it/computer/csadun

[...]
Dimenticavo un pezzo:
//Source file: c:\light2\Source\no\gdpm\ttil\TieOutputStream.java
package no.gdpm.util;
import java.io.OutputStream;
import java.io.IOException;
/**
 * An outputstream which ties output to two underlying streams. Any character written to the stream
 * is replicated on both.
 *
 * @version 1.0
 */
public class TieOutputStream extends OutputStream 
{
   private OutputStream os1;
   private OutputStream os2;
 
   /**
    * Builds an outputstream that ties togheter the two given streams
    * @param os1 the first stream
    * @param os2 the second stream
    * @roseuid 3A19339B020F
    */
   public TieOutputStream(OutputStream os1, OutputStream os2) 
   { this.os1=os1;
     this.os2=os2;
   }
 
   /**
    * Writes a byte to the stream, and thus to the two underlying streams.
    *
    * @param b the byte to write
    * @exception IOException if something fails during writing
    * @roseuid 3A1933C90035
    */
   public void write(int b) throws IOException 
   {
   	os1.write(b);
   	os2.write(b);


[...]
Dimenticavo un pezzo :)
-----------------------------------------------------------------------
package org.sadun.util;
import java.io.OutputStream;
import java.io.IOException;

    */
   public void write(int b) throws IOException 
   {
   	os1.write(b);
   	os2.write(b);


: 
..e avevo dimenticato di cambiare un nome. :(





---------------------------------------------------------------------------



    */
   public void write(int b) 

   		new org.sadun.LogPrintStream(
   			java.lang.System.out,
   			tag,
   			logFile
   		)
   	);
   }
 
}


č necesasrio salvare l'oggetto precedente?
Non me ne sono mai preoccupato, ma cosě a occhio mi sa tanto di sě.
Oliviero.