/* * Copyright (C) 2007 Jonathan Craven * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package fr.craven.util; import java.io.FileNotFoundException; import java.io.PrintStream; /** *

Redirects stderr and stdout to a file to catch hard-to-find error messages, especially * on Windows systems. Not a replacement for proper logging!

* *

Example usage

* *

In your main class, insert the following:

* *
*
  static {
    StdRedirect.redirect(System.getProperty("user.home") + File.separator + "myLogFile.log");
  }

  
* *
Copyright © 2007 Jonathan Craven http://jonathan.craven.fr
* * @author Jonathan Craven * */ public final class StdRedirect extends PrintStream { private static PrintStream instance; private StdRedirect(String filename) throws FileNotFoundException { super(filename); } /** * @see fr.craven.util.StdRedirect * @throws IllegalStateException if invoked more than once. */ public static void redirect(String filename) { if (instance != null) throw new IllegalStateException("You shouldn't be calling redirect more than once, it's wacky"); try { instance = new StdRedirect(filename); System.setErr(instance); System.setOut(instance); } catch (FileNotFoundException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } return; } }