Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Get all text from System.out  (Read 715 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Posted 2014-01-07 22:04:47 »

Salutations,

I am creating a logging system for MERCury, and I want to be able to save all of the console output into a file. Is there a way to take all of what was put into System.out, and print it to a file?

Thanks,
-wes

Offline Danny02
« Reply #1 - Posted 2014-01-07 22:05:49 »

perhaps use a logging library?
try Log4J
Offline Magn919

Junior Member


Medals: 6
Exp: 4 years



« Reply #2 - Posted 2014-01-07 22:21:57 »

You can try this, have served me fine for over a year.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
public class SplitPrintStream extends PrintStream {
   private PrintStream second;
   
   public SplitPrintStream(PrintStream main, PrintStream second) {
      super(main);
      this.second = second;
   }
   
   @Override
   public void close() {
      super.close();
      if (second != null)
         second.close();
   }
   
   @Override
   public void flush() {
      super.flush();
      if (second != null)
         second.flush();
   }
   
   @Override
   public void write(byte[] buf, int off, int len) {
      super.write(buf, off, len);
      if (second != null)
         second.write(buf, off, len);
   }
   
   @Override
   public void write(int b) {
      super.write(b);
      if (second != null)
         second.write(b);
   }
   
   @Override
   public void write(byte[] b) throws IOException {
      super.write(b);
      if (second != null)
         second.write(b);
   }
}

Simply pass it System.out and the printstream for your log file or wherever else, you want to sent the console output(I use it to read console output, from my play testers via network.),
then replace System.out with the new SplitPrintStream using System.setOut().

For every new problem, a new source of solutions has come to exist.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #3 - Posted 2014-01-07 22:36:26 »

Thanks man! Will credit you in the code!

Online Longarmx
« Reply #4 - Posted 2014-01-08 02:08:06 »

You can also just call this at the start of your program.

1  
2  
3  
4  
5  
6  
7  
8  
9  
try
{
   PrintStream out = new PrintStream("log.txt");
   System.setOut(out);
   System.setErr(out);
} catch (FileNotFoundException e)
{
   e.printStackTrace();
}


This makes all
System.out
calls output to the file as well as
System.err
messages and stack traces. It has worked fine for me with all my projects.

Offline Drenius
« Reply #5 - Posted 2014-01-08 21:20:37 »

Just wrote a little class for splitting a PrintStream into two other ones so you could use Longarmx's way to write every console output to both, the file AND the normal console (or more).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
import java.io.PrintStream;

public class SplitPrintStream extends PrintStream
{
   private PrintStream[] printStreams;
   public SplitPrintStream(PrintStream[] printStreams)
   {
      super(printStreams[0]);
      this.printStreams = printStreams;
   }
   
   public void print(String s)      {   for(int i=0;i<this.printStreams.length;i++)   this.printStreams[i].print(s);}
   public void print(char c)      {   for(int i=0;i<this.printStreams.length;i++)   this.printStreams[i].print(c);}
   public void print(Object o)      {   for(int i=0;i<this.printStreams.length;i++)   this.printStreams[i].print(o);}
   //etc
 
   public void println(String s)   {   for(int i=0;i<this.printStreams.length;i++)   this.printStreams[i].println(s);}
   public void println(char c)      {   for(int i=0;i<this.printStreams.length;i++)   this.printStreams[i].println(c);}
   public void println(Object o)   {   for(int i=0;i<this.printStreams.length;i++)   this.printStreams[i].println(o);}
   //etc
}
Offline Danny02
« Reply #6 - Posted 2014-01-08 22:20:35 »

@all pls stop this hackery reinventing of the wheel

The things you try to do are already solved by logging libraries, there is also one already included in the standard JRE (java.util.logging).
Offline Drenius
« Reply #7 - Posted 2014-01-09 17:21:45 »

@Danny02
Sometimes it is just easier to make something yourself than understanding a whole new API of which you need only one little feature from.
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #8 - Posted 2014-01-09 20:46:29 »

How is this hackery? Merely using 2 printstreams is not a big thing. I still appreciate the posts above (log4j), but as kpars once put it (loosely)

Quote
That is like using a car engine to power a gameboy.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (11 views)
2014-09-21 23:33:17

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!