I have a testing program (that I didn't write) that is giving a very weird error, which only happens some of the time. At the end of the program a string that contains the test report is output using
logger.warn(reportString);
Anywhere from 1/10 to 1/3 tries (it varies) this string isn't being displayed, but everything before it is.
Before this I added this line to ensure that the string is always created properly, which it is since this gives the same length every time.
logger.info("Length: " + reportString.length());
To experiment I added another logging line after, so the end of the program now looks like this:
logger.info("Length: " + reportString.length());
logger.warn(reportString);
logger.info("REACHED END OF PROGRAM");
When doing this the reportString always seems to be printed/logged, but the "REACHED END OF PROGRAM" string is only printed/logged about 50% of the time. Maybe it's something with my windows command prompt? I know this is a pretty vague problem but if anyone has any ideas they would be greatly appreciated.
Maybe you need to flush the all buffered logs to send all to the output. Add the next:
logger.warn(reportString);
LogManager.shutdown();
Just in case you have it in your code, you can try removing the sentence:
System.exit(0);
This probably has to do with how log4j buffers the data it outputs, try explicitly adding \n in the last string the logger prints, and see if it prints consistently now.
Related
I am writing simple socket chat using console to output messages. The problem is that when one user types a message, and at the same time getting one, his input interrupted:
I: writing my input here, but
Other_user: hi! here is a message for you.
I: it splits to different lines, which is 1) very inconvenient 2) cannot see which simbols i deleted if press backspace
So, what i am asking is, how can I avoid this
(something like: if message is received, check input for symbols; if there are, remember them, delete last stroke in console, print message, and then recreate that stroke).
EDIT: attached picture
hard to tell without specific code, but an option is to use two threads, one to handle the socket input, one for output. attach these to System.in and System.out respectively. it seems like you might be using only one thread to do both.
This is not the basic Scanner(System.in).
I have some recursive methods that I saw online that does exactly what I want but now I need to count the number of lines that have been "outputed" to the console.
So, I run the program. It does some computations and then it writes on the console some lines (as you can see above) and I need to , in the end on the computations (after the execution of the main), count the number of that lines and write it to somewhere.
Example of the output:
Does anyone know how it can be done?
Thanks.
You could use the class TeeOutputStream from Apache Common IO that split the outputs into two streams. This way you can still publish your stream in the default output and log them in a file. With the outputs in the file you can do whatever you wanted (count lines...).
Why don´t you declare a variable counter and initialize it with 0. After every System.out.println() statement, you increment the counter by adding counter++.
As final Statement in your main-Method, you print the variable to the console:
System.out.println("Counter: "+counter");
If that doesn´t work for you, you could still write the output of the program to a file instead of the console, read that file afterwards and look for line breaks.
A few days back I wrote a Textinterpreter plugin in Eclipse which basically takes a text file and simply printout it's content in the console. It does this by first taking a text file and converts it to a string.
then it makes an Arraylist out of it from which each line is printed out in the console.
List<String> mLines = new LinkedList<String>(Arrays.asList(string)
while(!mLines.isEmpty())) {
String line = mLines.remove(0);
if(line.equals("Stop...")){
debug(DebugAction.Suspend);
}
System.out.println(">>> " + line + " <<<");
}
You can see an if statement in code above which checks whether "Stop..." is written on any line in the text file and if it is then the debug() funtion is called(which suspends running unless the user press resume() button in debugmode.)
Now I want to do the same for .java files. i.e write a Java interpreter plugin which execute a java file normally until it finds "Stop..." written in code.
Any Suggestions?
I don't think you really want to implement a Java interpreter, that'd be a huge project keeping you busy for some years maybe.
The most natural solution for you task might be to scan the Java source file and automatically create breakpoints at each Stop statement. Then run the application in debug mode and you get the desired behaviour. Since you only need the line number for creating the breakpoint you can actually keep reading/scanning files line-by-line.
To get additional statements executed (like calling debug(..)) add your snippet as a breakpoint condition (followed by return true; to tell the debug to stop indeed).
Is there any character limit for the output of Java's System.out.println(String x) statement?
When I try to print some XML from a web service call using System.out.println(), only a portion of it is actually printed in the console.
The XML string that I am trying to print is huge.
Why is this happening?
Are you experiencing this within Eclipse? If yes:
EDIT:
Go to Window > Preferences > Run/Debug > Console
Uncheck "Limit Console Output" (Alternatively you can increase the Console buffer size.)
Source
My guess is that you only see the last part of the String because the console has a limited number of lines it can display.
Consider logging to a file from Java, or redirecting the standard output from the program to a file:
java com.foo.bar.Main > output.log
You're limited by the maximum size of a Java String. That's all. This should be the equivalent of length Integer.MAX_VALUE (2147483647), which is the max size of an array, since a String is a char array.
Otherwise, it's the Eclipse console capacity limit, as others have said.
If you are using Eclipse, it is because there is a limit on the capacity of the Eclipse output console. See this SO question: How do I increase the capacity of the Eclipse output console?
I know that printing very long strings into the Eclipse console results in part or all of the string becoming invisible. You may want to break your xml into chunks. If you are only seeing the tail part of the xml then I'd guess its your console buffer trimming off part of it. #Quaylar posted a link about this.
There isn't really an explicit maximum, but the offset in the string is determined by int, so Integer.MaxValue would be one limitation IMO. It also would depend on your available memory.
Your best bet would be to stream the output and write portions at a time to ensure you get it all.
What are some scenarios in which java's System.out.println would fail to produce any output. I have a call to it inside of a method and sometimes when the method is called I get the println and othertimes I don't.
Update: I am also using System.out.flush() after the println.
Update: Thank you for the debugging help. It turned out a blocking call to open a dialog made output appear vastly out of the proper order. I thought the method I was trying to print messages for was being called when the dialog closed but the method itself was what was calling the dialog and so after the closing it was already past the printouts which was where i started looking for the test. If someone has the ability to delete this question as the issue was not what was originally asked it'd be appreciated.
System.out.println on some platforms uses buffered output. Depending on what your code is doing, it is possible that the buffers are not flushed before your program exits. Try putting System.out.flush() after your printlns and see if that helps.
Edit:
sometimes when the method is called I get the println and othertimes I don't
How are you verifying that the method is called but the println produces no output? Is it possible your method is throwing an Exception (which is then swallowed) before it gets to the println?
It would, of course, be very helpful to see some actual code.
I have never seen this scenario before. In theory, it would only "fail" when the output isn't there where you expect it is. The output target can namely be changed using System#setOut().
Where are you checking for your output? It's possible that System.out has been redirected elsewhere, so maybe you're looking in the wrong place.
answer as per #BalusC's suggestion--
Thank you for the debugging help. It turned out a blocking call to open a dialog made output appear vastly out of the proper order. I thought the method I was trying to print messages for was being called when the dialog closed but the method itself was what was calling the dialog and so after the closing it was already passed the printouts which were where I started looking for the test. If someone has the ability to delete this question as the issue was not what was originally asked it'd be appreciated.
System.out.println is buffered output, if you do not flush the buffer, it may seem to 'wait' until the end of program. Sometimes, the program can die before flushing its buffers. System.out.flush() will force output to be flushed.
It's possible that the file handle has been changed. I.e., stdout's file descriptor is no longer 1. I've seen this done in logging utilities where people don't want to go and catch any text that might be printed to a file descriptor, so instead they just redirect the stream to a file handle of an open file.
Here's an example in python:
import sys
h = open(r"C:\foo.txt","a+")
sys.stdout = h
sys.stdout.write("hey fellas")
h.close()
Run this at the cmdline, and you'll not get "hey fellas" printed out as you expect. Instead, it will be redirected to the file C:\foo.txt