3

I have two threads - one awaits for input and the other is printing the debugging info.

However only one console window, so I can't type 'exit' (or whatever to stop the process), because System.out.println constantly prints the stuff. Can I have two separate console windows for each?

P.S. I wouldn't want to use Swing just for this purpose - there must be a way.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Aubergine
  • 5,862
  • 19
  • 66
  • 110
  • 3
    Is writing the debug info to a file not an option? You could open another console window and `tail` the file (you would also have the debug info after the process has exited). – hmjd Feb 20 '12 at 22:41
  • well it is an option, but again I don't remember how to do that. Judging from answers there is no trivial solution though, so I will try some of them. But now it is not an issue, as my port seems to close properly even if I terminate it manually. – Aubergine Feb 21 '12 at 14:48

3 Answers3

4

Log to a file then go into another window and tail the file (in unix/mac use "tail -f filename", in another os--install unix/cygwin!)

This keeps your log separate from your console and makes it persistent as well.

There are a lot of logging utilities out there that will help with this and will even help a bit more by telling you what file a given line is coming from.

Bill K
  • 62,186
  • 18
  • 105
  • 157
3

The only way I could think of would be to have two difference processes and a link betweeen the two processes. But I don't have a clue as to how to do that. Perhaps your best bet is to use the JOptionPane class.

While you stated that you don't want to use Swing, I believe that JOptionPane would be the best option for you. Simply using JOptionPane.showInputDialog is a fast way to solve your issue. Here's a link to JOptionPane's JavaDoc.

If you really can't use Swing, there's always the option to press Ctrl + C to stop the process.

A final option would be to buffer the output and only write it after the input. After you receive input, you would flush the buffer and then deal with the input. In this manner, you would prevent the application from closing before the buffer is flushed. There are two ways to do this:

  1. You can use a BufferedWriter with a very large size (maybe 100,000?) and store this as a static variable. Instead of calling System.out.println(), you could call MyClass.out.println()
  2. You could override System using System.setOut(). You would create your own PrintWriter that would take any input and send it to a LinkedList (or your own LinkedList designed for chars, if you choose). I suggest you use a linked list because appending is O(1) for a linked list while appending is O(n) for an array list.

Edit:

As for hmjd's suggestion (file writing), you would do that like this:

System.setOut(new FileWriter(new File(myFileName)));
Ryan Amos
  • 5,422
  • 4
  • 36
  • 56
  • 2
    I actually used a version of replacing stdout like that to implement my own logging system in a system to big to replace all the system.outs. The really cool thing is that as part of the intercept I could add the class file/line number that did the system.out and use that info to implement filters (such as filtering messages unless they were from a specific class or unless they contained my initials [bk]... Very Expensive in CPU time but that often doesn't matter during dev and it could be easily turned off to ship. – Bill K Feb 20 '12 at 22:54
  • @BillK Couldn't you just use a find/replace tool? Find "System.out.print", replace with "MyLogger.log". But the intercept is quite useful. I assume you were reading up the stack trace to find the class. Is that correct? – Ryan Amos Feb 20 '12 at 23:03
  • Yes. A flag would let me turn off the stack trace thing. Part of it was trying to solve the problem without disrupting other parts of the project--some of which were packaged in Jars and we never recompiled (the initial goal was to be able to mute selected prpackaged libraries). Also since it was java version 1.3 all I had was Exception.printStackTrace() so my intercept had to actually swap in a different "system.out" to catch the stack traces... – Bill K Feb 21 '12 at 17:07
2

Your question is similar to this one, so I think the answer is the same. However, maybe this question might be right for you.

Community
  • 1
  • 1
enzom83
  • 8,080
  • 10
  • 68
  • 114