Can you find out the cause of the debugging in the program (exe) state?

Can you find out the cause of the debugging in the program (exe) state?

  1. For example, in the case of processing (run), a debugging log file can be obtained.

  2. In the case of processing (exe), is there a way to get the debugging log file?

If it is a processing (exe) file, the cause is unknown even if the program stops. I want to know why the program stopped, what should I do in this case?

You can try using printStream.

In your setup use

try{
PrintStream ps=new PrintStream("FileName");
System.setOut(ps);
System.setErr(ps);
}
catch(Exception e){}

This way the sketch will rerout the console to a file.

2 Likes

Hi @GWAK,

Just looked, but it seems that the stderr/stdout not writing to a console or log by default, but if you have access to the source and are able to build the exe again, you can apply a log by yourself …

The Program below redirects the stderr and stdout to the files console.err and console.out, which would be created in the same directory as the executable at start (at least if you dblclick in Explorer).

Cheers
— mnse

ie:

import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.File;
import javax.swing.JOptionPane;

int lastSec = -1;
void redirectConsole() {
  try {
    System.setErr(new PrintStream(new FileOutputStream(new File(sketchPath() + "/console.err"))));
    System.setOut(new PrintStream(new FileOutputStream(new File(sketchPath() + "/console.out"))));
  } 
  catch (Exception e) {
    JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
  }
}

void settings() {
  size(500, 500);
  redirectConsole();
}

void setup() {
  textAlign(CENTER, CENTER);
  textSize(50);
}

void draw() {
  background(0);
  fill(255);
  int m = int(millis()/1000); 
  text(m+"", width/2, height/2);
  if (lastSec != m) {

    // writes to console.err
    System.err.println("ERR: " + m);
    // writes to console.out
    System.out.println("OUT: " + m);
    println("println: " + m);

    lastSec = m;
  }
}
2 Likes

@NumericPrime @mnse

Thank you both very much for your kind replies.

However, I want to save the log where the error occurred.

For example, the source code is configured as follows.

import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.File;
import javax.swing.JOptionPane;

int lastSec = -1;
int test_array[] = {0,0,0,0,0};

void redirectConsole() {
  String str_name = str(month())+"_"+str(day())+"_"+str(hour())+"_"+str(minute())+"_";
  // println(sketchPath() + "/data/"+str_name+"console.err");
  try {
    System.setErr(new PrintStream(new FileOutputStream(new File(sketchPath() + "/data/"+str_name+"console.err"))));
    // System.setOut(new PrintStream(new FileOutputStream(new File(sketchPath() + "/data/"+str_name+"console.out"))));
  } 
  catch (Exception e) {    
    JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
  }
}

void settings() {
  size(500, 500);
  redirectConsole();
}

void setup() {
  textAlign(CENTER, CENTER);
  textSize(50);
}

void draw() {
  background(0);
  fill(255);
  int m = int(millis()/1000); 
  text(m+"", width/2, height/2);
  test_array[m]=m+1;
  
  if (lastSec != m) {
    System.err.println("ERR: " + m);
    lastSec = m;
  }  
  
}

As in the above source code and picture
Program hung with ‘ArrayIndexOutOfBoundsException: 5’.
I hope that such a message will be saved as above.

Do you have any good way?

You can try to use try/catch take a look at this code:

void setup() {
}
void draw() {
  try {
    drawCode();
  }
  catch(Exception e) {
    e.printStackTrace();
    System.err.println("An error occurred... shutting down");
    exit();
  }
}
void drawCode() {
  if (random(0, 1)<0.2) throw new RuntimeException("Something went wrong");
}

1 Like

To fix your issue specificly I would recommend using ArrayLists instead like this:

import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.File;
import javax.swing.JOptionPane;
import java.util.*;

int lastSec = -1;
List<Integer> test_array = new ArrayList<Integer>();

void redirectConsole() {
  String str_name = str(month())+"_"+str(day())+"_"+str(hour())+"_"+str(minute())+"_";
  // println(sketchPath() + "/data/"+str_name+"console.err");
  try {
    System.setErr(new PrintStream(new FileOutputStream(new File(sketchPath() + "/data/"+str_name+"console.err"))));
    // System.setOut(new PrintStream(new FileOutputStream(new File(sketchPath() + "/data/"+str_name+"console.out"))));
  } 
  catch (Exception e) {    
    JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
  }
}

void settings() {
  size(500, 500);
  redirectConsole();
}

void setup() {
  textAlign(CENTER, CENTER);
  textSize(50);
}

void draw() {
  background(0);
  fill(255);
  int m = int(millis()/1000); 
  text(m+"", width/2, height/2);
  while(m>=test_array.size()) test_array.add(m+1);
  test_array.set(m,m+1);
  if (lastSec != m) {
    System.err.println("ERR: " + m);
    lastSec = m;
  }  
  
}
1 Like

@NumericPrime

You are a genius. Thanks for the kind explanation and reply.

void draw() {
  try {
    drawCode();
  }
  catch(Exception e) {
    e.printStackTrace();
    System.err.println("An error occurred... shutting down");
    exit();
  }
}

When using the code in this form, there may be a problem.
Are there any disadvantages such as slowness when using it compared to not using it?

I just looked it up. Using try has only a minor cost. Catching an Exception however is more slow. However since you don’t need to catch them all the time you should not notice a dip in performance.

1 Like

@NumericPrime

I understand your meaning. thank you.

1 Like

I’d like to add one last thing:
Another advantage of having a central try/catch block is that if you have an exception you are 100% sure won’t occurr you can just hand it to the central try catch block:
For example if you used:

void drawCode() {
  boolean error=false;
  if(error) throw new IOException("testing");
}

Java will complain because you don’t handle IOExceptions. If you use

void drawCode() throws Exception{
  boolean error=false;
  if(error) throw new IOException("testing");
}

It won’t and since you catch it in draw.

1 Like