?? log.java
字號:
/** * @(#)Log.java 1.26 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.tools.javac.v8.util;import java.io.*;import java.util.ResourceBundle;import java.util.MissingResourceException;import java.text.MessageFormat;/** * A class for error logs. Reports errors and warnings, and * keeps track of error numbers and positions. */public class Log implements LayoutCharacters { /** * The context key for the log. */ private static final Context.Key logKey = new Context.Key(); /** * The context key for the output PrintWriter. */ public static final Context.Key outKey = new Context.Key(); public final PrintWriter errWriter; public final PrintWriter warnWriter; public final PrintWriter noticeWriter; /** * The maximum number of errors/warnings that are reported, * can be reassigned from outside. */ private final int MaxErrors; private final int MaxWarnings; /** * Switch: prompt user on each error. */ public boolean promptOnError; /** * Switch: emit warning messages. */ public boolean emitWarnings; /** * Construct a log with given I/O redirections. */ protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { super(); context.put(logKey, this); this.errWriter = errWriter; this.warnWriter = warnWriter; this.noticeWriter = noticeWriter; Options options = Options.instance(context); this.promptOnError = options.get("-prompt") != null; this.emitWarnings = options.get("-nowarn") == null; this.sourcename = Name.Table.instance(context).__input; this.MaxErrors = getIntOption(options, "-Xmaxerrs", 100); this.MaxWarnings = getIntOption(options, "-Xmaxwarns", 100); } private int getIntOption(Options options, String optionName, int defaultValue) { String s = (String) options.get(optionName); try { if (s != null) return Integer.parseInt(s); } catch (NumberFormatException e) { } return defaultValue; } /** * The default writer for diagnostics */ static final PrintWriter defaultWriter(Context context) { PrintWriter result = (PrintWriter) context.get(outKey); if (result == null) context.put(outKey, result = new PrintWriter(System.err)); return result; } /** * Construct a with default settings. */ protected Log(Context context) { this(context, defaultWriter(context)); } /** * Construct a with all output redirected. */ protected Log(Context context, PrintWriter defaultWriter) { this(context, defaultWriter, defaultWriter, defaultWriter); } /** * Get the Log instance for this context. */ public static Log instance(Context context) { Log instance = (Log) context.get(logKey); if (instance == null) instance = new Log(context); return instance; } /** * The name of the file that's currently translated. */ private Name sourcename; /** * The number of errors encountered so far. */ public int nerrors = 0; /** * The number of warnings encountered so far. */ public int nwarnings = 0; /** * A set of all errors generated so far. This is used to avoid printing an * error message more than once. For each error, a pair consisting of the * source file name and source code position of the errir is added to the set. */ private Set recorded = Set.make(); /** * The buffer containing the file that's currently translated. */ private byte[] buf = null; /** * The position in the buffer at which last error was reported */ private int bp; /** * the source line at which last error was reported. */ private int lastLine; /** * Re-assign source name, returning previous setting. */ public Name useSource(Name name) { Name prev = sourcename; sourcename = name; if (prev != sourcename) buf = null; return prev; } /** * Return current source name. */ public Name currentSource() { return sourcename; } /** * Flush the logs */ public void flush() { errWriter.flush(); warnWriter.flush(); noticeWriter.flush(); } /** * Prompt user after an error. */ public void prompt() { if (promptOnError) { System.err.println(getLocalizedString("resume.abort")); char ch; try { while (true) { switch (System.in.read()) { case 'a': case 'A': System.exit(-1); return; case 'r': case 'R': return; case 'x': case 'X': throw new AssertionError("user abort"); default: } } } catch (IOException e) { } } } /** * Print a faulty source code line and point to the error. * @param line The line number of the printed line in the current buffer. * @param col The number of the column to be highlighted as error position. */ private void printErrLine(int line, int col, PrintWriter writer) { try { if (buf == null) { FileInputStream in = new FileInputStream(sourcename.toString()); buf = new byte[in.available()]; in.read(buf); in.close(); bp = 0; lastLine = 1; } else if (lastLine > line) { bp = 0; lastLine = 1; } while (bp < buf.length && lastLine < line) { switch (buf[bp]) { case CR: bp++; if (bp < buf.length && buf[bp] == LF) bp++; lastLine++; break; case LF: bp++; lastLine++; break; default: bp++; } } int lineEnd = bp; while (lineEnd < buf.length && buf[lineEnd] != CR && buf[lineEnd] != LF) lineEnd++; printLines(writer, new String(buf, bp, lineEnd - bp)); if (col == 0) col = 1; byte[] ptr = new byte[col]; for (int i = 0; i < col - 1; i++) ptr[i] = (byte)' '; ptr[col - 1] = (byte)'^'; printLines(writer, new String(ptr, 0, col)); } catch (IOException e) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -