langtools/src/share/classes/com/sun/tools/javac/util/Log.java
changeset 11052 65b9fa7eaf55
parent 9071 88cd61b4e5aa
child 11053 48713f779b1d
equal deleted inserted replaced
10950:e87b50888909 11052:65b9fa7eaf55
    58 
    58 
    59     /** The context key for the output PrintWriter. */
    59     /** The context key for the output PrintWriter. */
    60     public static final Context.Key<PrintWriter> outKey =
    60     public static final Context.Key<PrintWriter> outKey =
    61         new Context.Key<PrintWriter>();
    61         new Context.Key<PrintWriter>();
    62 
    62 
    63     //@Deprecated
    63     public enum WriterKind { NOTICE, WARNING, ERROR };
    64     public final PrintWriter errWriter;
    64 
    65 
    65     protected PrintWriter errWriter;
    66     //@Deprecated
    66 
    67     public final PrintWriter warnWriter;
    67     protected PrintWriter warnWriter;
    68 
    68 
    69     //@Deprecated
    69     protected PrintWriter noticeWriter;
    70     public final PrintWriter noticeWriter;
       
    71 
    70 
    72     /** The maximum number of errors/warnings that are reported.
    71     /** The maximum number of errors/warnings that are reported.
    73      */
    72      */
    74     public final int MaxErrors;
    73     protected int MaxErrors;
    75     public final int MaxWarnings;
    74     protected int MaxWarnings;
    76 
    75 
    77     /** Switch: prompt user on each error.
    76     /** Switch: prompt user on each error.
    78      */
    77      */
    79     public boolean promptOnError;
    78     public boolean promptOnError;
    80 
    79 
   129         context.put(logKey, this);
   128         context.put(logKey, this);
   130         this.errWriter = errWriter;
   129         this.errWriter = errWriter;
   131         this.warnWriter = warnWriter;
   130         this.warnWriter = warnWriter;
   132         this.noticeWriter = noticeWriter;
   131         this.noticeWriter = noticeWriter;
   133 
   132 
   134         Options options = Options.instance(context);
       
   135         this.dumpOnError = options.isSet(DOE);
       
   136         this.promptOnError = options.isSet(PROMPT);
       
   137         this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
       
   138         this.suppressNotes = options.isSet("suppressNotes");
       
   139         this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
       
   140         this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
       
   141 
       
   142         boolean rawDiagnostics = options.isSet("rawDiagnostics");
       
   143         messages = JavacMessages.instance(context);
       
   144         this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
       
   145                                               new BasicDiagnosticFormatter(options, messages);
       
   146         @SuppressWarnings("unchecked") // FIXME
   133         @SuppressWarnings("unchecked") // FIXME
   147         DiagnosticListener<? super JavaFileObject> dl =
   134         DiagnosticListener<? super JavaFileObject> dl =
   148             context.get(DiagnosticListener.class);
   135             context.get(DiagnosticListener.class);
   149         this.diagListener = dl;
   136         this.diagListener = dl;
   150 
   137 
   151         String ek = options.get("expectKeys");
   138         messages = JavacMessages.instance(context);
   152         if (ek != null)
   139 
   153             expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *")));
   140         final Options options = Options.instance(context);
       
   141         initOptions(options);
       
   142         options.addListener(new Runnable() {
       
   143             public void run() {
       
   144                 initOptions(options);
       
   145             }
       
   146         });
   154     }
   147     }
   155     // where
   148     // where
       
   149         private void initOptions(Options options) {
       
   150             this.dumpOnError = options.isSet(DOE);
       
   151             this.promptOnError = options.isSet(PROMPT);
       
   152             this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
       
   153             this.suppressNotes = options.isSet("suppressNotes");
       
   154             this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
       
   155             this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
       
   156 
       
   157             boolean rawDiagnostics = options.isSet("rawDiagnostics");
       
   158             this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
       
   159                                                   new BasicDiagnosticFormatter(options, messages);
       
   160 
       
   161             String ek = options.get("expectKeys");
       
   162             if (ek != null)
       
   163                 expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *")));
       
   164         }
       
   165 
   156         private int getIntOption(Options options, OptionName optionName, int defaultValue) {
   166         private int getIntOption(Options options, OptionName optionName, int defaultValue) {
   157             String s = options.get(optionName);
   167             String s = options.get(optionName);
   158             try {
   168             try {
   159                 if (s != null) {
   169                 if (s != null) {
   160                     int n = Integer.parseInt(s);
   170                     int n = Integer.parseInt(s);
   178             return 100;
   188             return 100;
   179         }
   189         }
   180 
   190 
   181     /** The default writer for diagnostics
   191     /** The default writer for diagnostics
   182      */
   192      */
   183     static final PrintWriter defaultWriter(Context context) {
   193     static PrintWriter defaultWriter(Context context) {
   184         PrintWriter result = context.get(outKey);
   194         PrintWriter result = context.get(outKey);
   185         if (result == null)
   195         if (result == null)
   186             context.put(outKey, result = new PrintWriter(System.err));
   196             context.put(outKey, result = new PrintWriter(System.err));
   187         return result;
   197         return result;
   188     }
   198     }
   246      */
   256      */
   247     public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) {
   257     public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) {
   248         this.diagFormatter = diagFormatter;
   258         this.diagFormatter = diagFormatter;
   249     }
   259     }
   250 
   260 
       
   261     public PrintWriter getWriter(WriterKind kind) {
       
   262         switch (kind) {
       
   263             case NOTICE:    return noticeWriter;
       
   264             case WARNING:   return warnWriter;
       
   265             case ERROR:     return errWriter;
       
   266             default:        throw new IllegalArgumentException();
       
   267         }
       
   268     }
       
   269 
       
   270     public void setWriter(WriterKind kind, PrintWriter pw) {
       
   271         pw.getClass();
       
   272         switch (kind) {
       
   273             case NOTICE:    noticeWriter = pw;  break;
       
   274             case WARNING:   warnWriter = pw;    break;
       
   275             case ERROR:     errWriter = pw;     break;
       
   276             default:        throw new IllegalArgumentException();
       
   277         }
       
   278     }
       
   279 
       
   280     public void setWriters(PrintWriter pw) {
       
   281         pw.getClass();
       
   282         noticeWriter = warnWriter = errWriter = pw;
       
   283     }
       
   284 
   251     /** Flush the logs
   285     /** Flush the logs
   252      */
   286      */
   253     public void flush() {
   287     public void flush() {
   254         errWriter.flush();
   288         errWriter.flush();
   255         warnWriter.flush();
   289         warnWriter.flush();
   256         noticeWriter.flush();
   290         noticeWriter.flush();
       
   291     }
       
   292 
       
   293     public void flush(WriterKind kind) {
       
   294         getWriter(kind).flush();
   257     }
   295     }
   258 
   296 
   259     /** Returns true if an error needs to be reported for a given
   297     /** Returns true if an error needs to be reported for a given
   260      * source name and pos.
   298      * source name and pos.
   261      */
   299      */
   311     }
   349     }
   312 
   350 
   313     /** Print the text of a message, translating newlines appropriately
   351     /** Print the text of a message, translating newlines appropriately
   314      *  for the platform.
   352      *  for the platform.
   315      */
   353      */
       
   354     public void printLines(WriterKind kind, String msg) {
       
   355         printLines(getWriter(kind), msg);
       
   356     }
       
   357 
       
   358     /** Print the text of a message, translating newlines appropriately
       
   359      *  for the platform.
       
   360      */
   316     public static void printLines(PrintWriter writer, String msg) {
   361     public static void printLines(PrintWriter writer, String msg) {
   317         int nl;
   362         int nl;
   318         while ((nl = msg.indexOf('\n')) != -1) {
   363         while ((nl = msg.indexOf('\n')) != -1) {
   319             writer.println(msg.substring(0, nl));
   364             writer.println(msg.substring(0, nl));
   320             msg = msg.substring(nl+1);
   365             msg = msg.substring(nl+1);