/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * JFlex 1.8.2                                                             *
 * Copyright (C) 1998-2018  Gerwin Klein <lsf@jflex.de>                    *
 * All rights reserved.                                                    *
 *                                                                         *
 * License: BSD                                                            *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package jflex.logging;

import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import jflex.base.Build;
import jflex.exceptions.GeneratorException;
import jflex.l10n.ErrorMessages;
import jflex.option.Options;
import jflex.performance.Timer;

In this class all output to the java console is filtered.

Use the switches verbose, time and DUMP at compile time to determine the verbosity of JFlex output. There is no switch for suppressing error messages. verbose and time can be overridden by command line parameters.

Redirects output to a TextArea in GUI mode.

Counts error and warning messages.

Author:Gerwin Klein
Version:JFlex 1.8.2
/** * In this class all output to the java console is filtered. * * <p>Use the switches verbose, time and DUMP at compile time to determine the verbosity of JFlex * output. There is no switch for suppressing error messages. verbose and time can be overridden by * command line parameters. * * <p>Redirects output to a TextArea in GUI mode. * * <p>Counts error and warning messages. * * @author Gerwin Klein * @version JFlex 1.8.2 */
public final class Out {
Platform specific newline.
/** Platform specific newline. */
public static final String NL = System.getProperty("line.separator"); private Out() {}
count total warnings
/** count total warnings */
private static int warnings;
count total errors
/** count total errors */
private static int errors;
output device
/** output device */
private static StdOutWriter out = new StdOutWriter();
Switches to GUI mode if text</code> is not <code>null
Params:
  • text – the message TextArea of the JFlex GUI
/** * Switches to GUI mode if {@code text</code> is not <code>null} * * @param text the message TextArea of the JFlex GUI */
public static void setGUIMode(TextArea text) { out.setGUIMode(text); }
Sets a new output stream and switches to non-gui mode.
Params:
  • stream – the new output stream
/** * Sets a new output stream and switches to non-gui mode. * * @param stream the new output stream */
public static void setOutputStream(OutputStream stream) { out = new StdOutWriter(stream); out.setGUIMode(null); }
Report time statistic data.
Params:
  • message – the message to be printed
  • time – elapsed time
/** * Report time statistic data. * * @param message the message to be printed * @param time elapsed time */
public static void time(ErrorMessages.ErrorMessage message, Timer time) { if (Options.time) { String msg = ErrorMessages.get(message, time.toString()); out.println(msg); } }
Report time statistic data.
Params:
  • message – the message to be printed
/** * Report time statistic data. * * @param message the message to be printed */
public static void time(String message) { if (Options.time) { out.println(message); } }
Report generation progress.
Params:
  • message – the message to be printed
/** * Report generation progress. * * @param message the message to be printed */
public static void println(String message) { if (Options.verbose) { out.println(message); } }
Report generation progress.
Params:
  • message – the message to be printed
  • data – data to be inserted into the message
/** * Report generation progress. * * @param message the message to be printed * @param data data to be inserted into the message */
public static void println(ErrorMessages.ErrorMessage message, String data) { if (Options.verbose) { out.println(ErrorMessages.get(message, data)); } }
Report generation progress.
Params:
  • message – the message to be printed
  • data – data to be inserted into the message
/** * Report generation progress. * * @param message the message to be printed * @param data data to be inserted into the message */
public static void println(ErrorMessages.ErrorMessage message, int data) { if (Options.verbose) { out.println(ErrorMessages.get(message, data)); } }
Report generation progress.
Params:
  • message – the message to be printed
/** * Report generation progress. * * @param message the message to be printed */
public static void print(String message) { if (Options.verbose) { out.print(message); } }
Dump debug information to System.out

Use like this if (Out.DEBUG) Out.debug(message) to save performance during normal operation (when DEBUG is turned off).

Params:
/** * Dump debug information to System.out * * <p>Use like this {@code if (Out.DEBUG) Out.debug(message)} to save performance during normal * operation (when DEBUG is turned off). * * @param message a {@link java.lang.String} object. */
public static void debug(String message) { if (Build.DEBUG) { System.out.println(message); } }
All parts of JFlex, that want to provide dump information should use this method for their output.
Params:
  • message – the message to be printed
/** * All parts of JFlex, that want to provide dump information should use this method for their * output. * * @param message the message to be printed */
public static void dump(String message) { if (Options.dump) { out.println(message); } }
All parts of JFlex, that want to report error messages should use this method for their output.
Params:
  • message – the message to be printed
/** * All parts of JFlex, that want to report error messages should use this method for their output. * * @param message the message to be printed */
public static void err(String message) { out.println(message); }
throws a GeneratorException if there are any errors recorded
/** throws a GeneratorException if there are any errors recorded */
public static void checkErrors() { if (errors > 0) { throw new GeneratorException(); } }
print error and warning statistics
/** print error and warning statistics */
public static void statistics() { StringBuilder line = new StringBuilder(errors + " error"); if (errors != 1) line.append("s"); line.append(", ").append(warnings).append(" warning"); if (warnings != 1) line.append("s"); line.append("."); err(line.toString()); }
reset error and warning counters
/** reset error and warning counters */
public static void resetCounters() { errors = 0; warnings = 0; }
print a warning without position information
Params:
  • message – the warning message
/** * print a warning without position information * * @param message the warning message */
public static void warning(String message) { warnings++; err(NL + "Warning : " + message); }
print a warning message without line information
Params:
  • message – code of the warning message
See Also:
/** * print a warning message without line information * * @param message code of the warning message * @see ErrorMessages */
public static void warning(ErrorMessages.ErrorMessage message) { warning(message, 0); }
print a warning with line information
Params:
  • message – code of the warning message
  • line – the line information
See Also:
/** * print a warning with line information * * @param message code of the warning message * @param line the line information * @see ErrorMessages */
public static void warning(ErrorMessages.ErrorMessage message, int line) { warnings++; String msg = NL + "Warning"; if (line > 0) msg = msg + " in line " + (line + 1); err(msg + ": " + ErrorMessages.get(message)); }
print warning message with location information
Params:
  • file – the file the warning is issued for
  • message – the code of the message to print
  • line – the line number of the position
  • column – the column of the position
/** * print warning message with location information * * @param file the file the warning is issued for * @param message the code of the message to print * @param line the line number of the position * @param column the column of the position */
public static void warning(File file, ErrorMessages.ErrorMessage message, int line, int column) { String msg = NL + "Warning"; if (file != null) msg += " in file \"" + file + "\""; if (line >= 0) msg = msg + " (line " + (line + 1) + ")"; try { err(msg + ": " + NL + ErrorMessages.get(message)); } catch (ArrayIndexOutOfBoundsException e) { err(msg); } warnings++; if (line >= 0) { if (column >= 0) showPosition(file, line, column); else showPosition(file, line); } }
print error message (string)
Params:
  • message – the message to print
/** * print error message (string) * * @param message the message to print */
public static void error(String message) { errors++; err(NL + message); }
print error message (code)
Params:
  • message – the code of the error message
See Also:
/** * print error message (code) * * @param message the code of the error message * @see ErrorMessages */
public static void error(ErrorMessages.ErrorMessage message) { errors++; err(NL + "Error: " + ErrorMessages.get(message)); }
print error message with data
Params:
  • data – data to insert into the message
  • message – the code of the error message
See Also:
/** * print error message with data * * @param data data to insert into the message * @param message the code of the error message * @see ErrorMessages */
public static void error(ErrorMessages.ErrorMessage message, String data) { errors++; err(NL + "Error: " + ErrorMessages.get(message, data)); }
IO error message for a file (displays file name in parentheses).
Params:
  • message – the code of the error message
  • file – the file it occurred for
/** * IO error message for a file (displays file name in parentheses). * * @param message the code of the error message * @param file the file it occurred for */
public static void error(ErrorMessages.ErrorMessage message, File file) { errors++; err(NL + "Error: " + ErrorMessages.get(message) + " (" + file + ")"); }
print error message with location information
Params:
  • file – the file the error occurred for
  • message – the code of the error message to print
  • line – the line number of error position
  • column – the column of error position
/** * print error message with location information * * @param file the file the error occurred for * @param message the code of the error message to print * @param line the line number of error position * @param column the column of error position */
public static void error(File file, ErrorMessages.ErrorMessage message, int line, int column) { String msg = NL + "Error"; if (file != null) msg += " in file \"" + file + "\""; if (line >= 0) msg = msg + " (line " + (line + 1) + ")"; try { err(msg + ": " + NL + ErrorMessages.get(message)); } catch (ArrayIndexOutOfBoundsException e) { err(msg); } errors++; if (line >= 0) { if (column >= 0) showPosition(file, line, column); else showPosition(file, line); } }
prints a line of a file with marked position.
Params:
  • file – the file of which to show the line
  • line – the line to show
  • column – the column in which to show the marker
/** * prints a line of a file with marked position. * * @param file the file of which to show the line * @param line the line to show * @param column the column in which to show the marker */
public static void showPosition(File file, int line, int column) { try { String ln = getLine(file, line); if (ln != null) { err(ln); if (column < 0) return; String t = "^"; for (int i = 0; i < column; i++) t = " " + t; err(t); } } catch (IOException e) { /* silently ignore IO errors, don't show anything */ } }
print a line of a file
Params:
  • file – the file to show
  • line – the line number
/** * print a line of a file * * @param file the file to show * @param line the line number */
public static void showPosition(File file, int line) { try { String ln = getLine(file, line); if (ln != null) err(ln); } catch (IOException e) { /* silently ignore IO errors, don't show anything */ } }
get one line from a file
Params:
  • file – the file to read
  • line – the line number to get
Throws:
/** * get one line from a file * * @param file the file to read * @param line the line number to get * @throws IOException if any error occurs */
private static String getLine(File file, int line) throws IOException { BufferedReader reader = Files.newBufferedReader(file.toPath(), Options.encoding); String msg = ""; for (int i = 0; i <= line; i++) msg = reader.readLine(); reader.close(); return msg; } }