When I programing I want to see what happened on run program but write on console is not quite right stuff beacuse every run it clean console. So I decide to make DEBUG for programing with help AI. I succeed to create this code with time stamp and output to console{optional) and the main log is to DEBUG file. This code paste to main code and just write the right name DEBUG and see output on console and just in file. Don’t be nervous when program broke or get stuck. Files is udpate every time when call DEBUG word or similiar. I forget say everytime you run program new file is created in DEBUG folder.
//********************DEBUG FOR PROGRAMS SETUP********************************************
PrintWriter debugWriter;
import java.util.Arrays;
String timestamp = nf(day(), 2) + "-" + nf(month(), 2) + "-" + year() + "_" + nf(hour(), 2) + "-" + nf(minute(), 2) + "-" + nf(second(), 2);
//********************DEBUG FOR PROGRAMS******************************************************
void startDEBUG() {
File DEBUGFolder = new File(sketchPath("DEBUG"));
if (!DEBUGFolder.exists()) { //if don't exist make a new one
DEBUGFolder.mkdirs();
}
String logFileName = sketchPath("DEBUG/" + timestamp + "_debug_log.txt");
debugWriter = createWriter(logFileName);
DEBUG("INFO", "=== DEBUG START ===");
}
//*********************CLASSIC DEBUG OUTPUT**************************************************
void DEBUG(String level, String text) {
String timeNow = nf(day(), 2) + "-" + nf(month(), 2) + "-" + year() + "_" + nf(hour(), 2) + "-" + nf(minute(), 2) + "-" + nf(second(), 2);
String logLine = "[" + timeNow + "] [" + level + "] " + text;
debugWriter.println(logLine);
println(logLine); // At the same time to the console (optional)
debugWriter.flush();
}
void DEBUG(String level, int value) {
DEBUG(level, str(value));
}
void DEBUG(String level, float value) {
DEBUG(level, str(value));
}
void DEBUG(String level, byte value) {
DEBUG(level, str(value));
}
void DEBUG(String level, char value) {
DEBUG(level, str(value));
}
void DEBUG(String level, boolean value) {
DEBUG(level, str(value));
}
//******************DEBUG PRO ARRAY****************************************************
void DEBUG_ARRAY(String level, String[] arr) {
String output = "[";
for (int i = 0; i < arr.length; i++) {
output += arr[i];
if (i < arr.length - 1) output += ", ";
}
output += "]";
DEBUG(level, "Array: " + output);
}
void DEBUG_ARRAY(String level, int[] arr) {
String output = Arrays.toString(arr);
DEBUG(level, "Array (int[]): " + output);
}
void DEBUG_ARRAY(String level, float[] arr) {
String output = Arrays.toString(arr);
DEBUG(level, "Array (float[]): " + output);
}
void DEBUG_ARRAY(String level, boolean[] arr) {
String output = Arrays.toString(arr);
DEBUG(level, "Array (boolean[]): " + output);
}
void DEBUG_ARRAY(String level, char[] arr) {
String output = Arrays.toString(arr);
DEBUG(level, "Array (char[]): " + output);
}
void DEBUG_ARRAY(String level, byte[] arr) {
String output = Arrays.toString(arr);
DEBUG(level, "Array (char[]): " + output);
}
//*************************DEBUG MATRIX(2D ARRAY)**********************************************
void DEBUG_MATRIX(String level, int[][] matrix) {
StringBuilder output = new StringBuilder("2D pole (int[][]):\n");
for (int x = 0; x < matrix.length; x++) {
output.append("Řádek ").append(x).append(": ");
for (int y = 0; y < matrix[x].length; y++) {
output.append("[").append(x).append(",").append(y).append("]:").append(matrix[x][y]).append(" ");
}
output.append("\n");
}
DEBUG(level, output.toString());
}
void DEBUG_MATRIX(String level, byte[][] matrix) {
StringBuilder output = new StringBuilder("2D pole (byte[][]):\n");
for (int x = 0; x < matrix.length; x++) {
output.append("Řádek ").append(x).append(": ");
for (int y = 0; y < matrix[x].length; y++) {
output.append("[").append(x).append(",").append(y).append("]:").append(matrix[x][y]).append(" ");
}
output.append("\n");
}
DEBUG(level, output.toString());
}
void DEBUG_MATRIX(String level, float[][] matrix) {
StringBuilder output = new StringBuilder("2D pole (float[][]):\n");
for (int x = 0; x < matrix.length; x++) {
output.append("Řádek ").append(x).append(": ");
for (int y = 0; y < matrix[x].length; y++) {
output.append("[").append(x).append(",").append(y).append("]:").append(matrix[x][y]).append(" ");
}
output.append("\n");
}
DEBUG(level, output.toString());
}
void DEBUG_MATRIX(String level, boolean[][] matrix) {
StringBuilder output = new StringBuilder("2D pole (boolean[][]):\n");
for (int x = 0; x < matrix.length; x++) {
output.append("Řádek ").append(x).append(": ");
for (int y = 0; y < matrix[x].length; y++) {
output.append("[").append(x).append(",").append(y).append("]:").append(matrix[x][y]).append(" ");
}
output.append("\n");
}
DEBUG(level, output.toString());
}
void DEBUG_MATRIX(String level, char[][] matrix) {
StringBuilder output = new StringBuilder("2D pole (char[][]):\n");
for (int x = 0; x < matrix.length; x++) {
output.append("Řádek ").append(x).append(": ");
for (int y = 0; y < matrix[x].length; y++) {
output.append("[").append(x).append(",").append(y).append("]:").append(matrix[x][y]).append(" ");
}
output.append("\n");
}
DEBUG(level, output.toString());
}
void DEBUG_MATRIX(String level, String[][] matrix) {
StringBuilder output = new StringBuilder("2D pole (String[][]):\n");
for (int x = 0; x < matrix.length; x++) {
output.append("Řádek ").append(x).append(": ");
for (int y = 0; y < matrix[x].length; y++) {
output.append("[").append(x).append(",").append(y).append("]:").append(matrix[x][y]).append(" ");
}
output.append("\n");
}
DEBUG(level, output.toString());
}
//********************if not used keyPressed********************************************************************
void keyPressed() {
if (key == 'q' || key == 'Q') {
DEBUG("QUIT_KEY", "PROGRAM");
stopDebug(true); //true starts the editor at the end of the program run and a false don't
exit();
}
}
//********************if not used mousePressed***********************
void mousePressed() {
if (mouseButton == RIGHT) {
DEBUG("QUIT_MOUSE", "PROGRAM");
stopDebug(true); //true starts the editor at the end of the program run and a false don't
exit();
}
}
//*********************STOPS DEBUG AND WRITE IN FILE***************************************
void stopDEBUG(boolean launch) { //true starts the editor at the end of the program run and a false don't
DEBUG("INFO", "=== DEBUG END ===");
debugWriter.flush();
debugWriter.close();
println("Log saved here: " + sketchPath("DEBUG"));
// Starts the log in the default editor (works on Windows/Linux)
if (launch) launch(sketchPath("DEBUG/" + timestamp + "_debug_log.txt"));
}
/********************************************************************************************
to output to DEBUG use:
DEBUG(popis,hodnota);
description:
"INFO"
"WARN"
"ERROR"
"DATA"
value type:
char
boolean
int
float
byte
String
*************************************
to output to DEBUG_ARRAY(description,value);
description:
"INFO"
"WARN"
"ERROR"
"DATA"
value type:
char[]
boolean[]
int[]
float[]
byte[]
String[]
************************************
to output to DEBUG use:DEBUG_MATRIX(description,value);
description:
"INFO"
"WARN"
"ERROR"
"DATA"
value type:
char[][]
boolean[][]
int[][]
float[][]
byte[][]
String[][]
*************************************/
This part:
********************DEBUG FOR PROGRAMS SETUP********************************************
PrintWriter debugWriter;
import java.util.Arrays;
String timestamp = nf(day(), 2) + "-" + nf(month(), 2) + "-" + year() + "_" + nf(hour(), 2) + "-" + nf(minute(), 2) + "-" + nf(second(), 2);
must be placed before void setup() and in void setup() on first line after this word please place word startDEBUG(); its a necessary to work the DEBUG loging. If quiting or ending program pls use mousePressed() or keyPressed() partto end the logging. And don’t worry, the lost logging data is always saved to the file, but no regular exits or breaks are necessary.