We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › echo all print() to file
Page Index Toggle Pages: 1
echo all print() to file (Read 442 times)
echo all print() to file
Mar 4th, 2008, 1:52pm
 
I have a program with many methods with a lot of complicated print() to the console.
Could I also send all my print() to a file without duplicating all my print()?

I went from this: Code:
...
println(num1);
...
print(sin(num2));
...
println("square("+num3+") = "+(num3*num3));
...

to this: Code:
PrintWriter output=createWriter(dataPath(logfile.txt));
...
println(num1);
output.println(num1);
...
print(sin(num2));
output.println(sin(num2));
...
println("square("+num3+") = "+(num3*num3));
output.println("square("+num3+") = "+(num3*num3));
...

to this: Code:
PrintWriter output=createWriter(dataPath(logfile.txt));
boolean outputIsOn=true;
...
println(num1);
if (outputIsOn) output.println(num1);
...
print(sin(num2));
if (outputIsOn) output.println(sin(num2));
...
println("square("+num3+") = "+(num3*num3));
if (outputIsOn) output.println("square("+num3+") = "+(num3*num3));
...

but I feel there should be a better way...
Re: echo all print() to file
Reply #1 - Mar 4th, 2008, 3:03pm
 
Normally, you could simply override the print() method to add file output. But in PApplet it's declared as static, so you will have to make your own print() and println() commands and use those instead.

To keep it simple, just make two methods that take Strings as parameters. Just make sure everything you want to print is turned into a String by adding "" to it. The following example works as is:

Code:
PrintWriter output;
boolean outputIsOn=true;

public void setup() {
noLoop();
}

void myPrintln(String s) {
if (outputIsOn) output.println(s);
println(s);
}

void myPrint(String s) {
if (outputIsOn) output.print(s);
print(s);
}

public void draw() {
output=createWriter(dataPath("logfile.txt"));

float num2=10;
float num3=3;

myPrint(""+sin(num2)); // convert number to string
myPrintln(" square("+num3+") = "+(num3*num3));

output.close();
}
Re: echo all print() to file
Reply #2 - Mar 4th, 2008, 4:02pm
 
Yes, that would do. Thanks.

I had thought it would be too difficult to implement all the variations of the print() and println(). But you're right that everything can easily be converted to String, even by just adding "" if needed. And my logfile.txt is a textfile anyway.
Re: echo all print() to file
Reply #3 - Mar 5th, 2008, 3:36am
 
FWIW I usually use the Java Logging API for such stuff. It's very easy to use and you can make use of the different log levels to adjust the granularity of your outputs. The other good thing is that you can see which method did the logging call, a very helpful detail for debugging without an actual debugger...

Code:

import java.util.logging.*;

Logger logger;

/**
* Creates and initializes a new logger instance.
* @param file absolut path/pattern to log file
* @param level minimum log level to consider
* (use level "ALL" for all messages)
*
* See for more info/examles:
* http://www.exampledepot.com/egs/java.util.logging/pkg.html
*/
void initLogger(String file,String level) {
try {
// create a single log file and append to it if possible
FileHandler fh=new FileHandler(file,true);
fh.setFormatter(new SimpleFormatter());
logger = Logger.getLogger("myloggername");
logger.addHandler(fh);
logger.setLevel(Level.parse(level));
logger.info("logger started...");
}
catch(IOException e) {
e.printStackTrace();
}
}
Page Index Toggle Pages: 1