logo
class

compiler::CompilerLog

sys::Obj
  compiler::CompilerLog
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   15 Sep 05  Brian Frank  Creation
   7  //    2 Jun 06  Brian Frank  Ported from Java to Fan
   8  //
   9  
  10  **
  11  ** CompilerLog manages logging compiler messages.  The default
  12  ** writes everything to standard output.
  13  **
  14  class CompilerLog
  15  {
  16  
  17  //////////////////////////////////////////////////////////////////////////
  18  // Construction
  19  //////////////////////////////////////////////////////////////////////////
  20  
  21    **
  22    ** Construct for specified output stream.
  23    **
  24    new make(OutStream out := Sys.out)
  25    {
  26      this.out = out
  27    }
  28  
  29  //////////////////////////////////////////////////////////////////////////
  30  // Methods
  31  //////////////////////////////////////////////////////////////////////////
  32  
  33    **
  34    ** Is level configured to log verbosely
  35    **
  36    Bool isVerbose()
  37    {
  38      return level <= LogLevel.verbose
  39    }
  40  
  41    **
  42    ** Indent the output.
  43    **
  44    Void indent()
  45    {
  46      indentation++
  47    }
  48  
  49    **
  50    ** Unindent the output.
  51    **
  52    Void unindent()
  53    {
  54      indentation--
  55      if (indentation < 0) indentation = 0
  56    }
  57  
  58    **
  59    ** Convenience for log(LogLevel.error, msg, aux, err)
  60    **
  61    Void error(Str msg, Str aux := null, Err err := null)
  62    {
  63      log(LogLevel.error, msg, aux, err)
  64    }
  65  
  66    **
  67    ** Convenience for log(LogLevel.warning, msg, aux, err)
  68    **
  69    Void warning(Str msg, Str aux := null, Err err := null)
  70    {
  71      log(LogLevel.warning, msg, aux, err)
  72    }
  73  
  74    **
  75    ** Convenience for log(LogLevel.message, msg, aux, err)
  76    **
  77    Void message(Str msg, Str aux := null, Err err := null)
  78    {
  79      log(LogLevel.message, msg, aux, err)
  80    }
  81  
  82    **
  83    ** Convenience for log(LogLevel.verbose, msg, aux, err)
  84    **
  85    Void verbose(Str msg, Str aux := null, Err err := null)
  86    {
  87      log(LogLevel.verbose, msg, aux, err)
  88    }
  89  
  90    **
  91    ** Generate a log entry.  The log entry is only generated if the
  92    ** specified level is greater than or equal to the configured level
  93    ** field.  The msg indicates the message to report.  Aux is used
  94    ** to separate extra information, by convention it is typically the
  95    ** target of msg such as the file name.  Err is used to report the
  96    ** associated exception.
  97    **
  98    virtual Void log(LogLevel level, Str msg, Str aux := null, Err err := null)
  99    {
 100      if (level < this.level) return
 101      if (level >= LogLevel.warning) print(level.toStr.capitalize).print(": ")
 102      n := indentation*2
 103      print(Str.spaces(n))
 104      print(msg); n += msg.size
 105      if (aux != null)
 106      {
 107        print("  [").print(aux).print("]")
 108      }
 109      printLine
 110  
 111      if (err != null)
 112      {
 113        if (isVerbose)
 114          err.trace(out)
 115        else
 116          print(Str.spaces(indentation*2+4)).printLine(err)
 117      }
 118    }
 119  
 120  //////////////////////////////////////////////////////////////////////////
 121  // CompilerErr
 122  //////////////////////////////////////////////////////////////////////////
 123  
 124    **
 125    ** Log a CompilerErr
 126    **
 127    virtual Void compilerErr(CompilerErr err)
 128    {
 129      if (LogLevel.error < this.level) return
 130      if (err.location != null)
 131      {
 132        printLine(err.location.toLocationStr + ": " + err.message)
 133      }
 134      else
 135      {
 136        printLine(err.message)
 137      }
 138      if (isVerbose) err.trace(out)
 139    }
 140  
 141  //////////////////////////////////////////////////////////////////////////
 142  // IO
 143  //////////////////////////////////////////////////////////////////////////
 144  
 145    **
 146    ** Print a string without trailing newline.
 147    **
 148    CompilerLog print(Obj s)
 149    {
 150      out.print(s)
 151      return this
 152    }
 153  
 154    **
 155    ** Print a line.
 156    **
 157    CompilerLog printLine(Obj s := "")
 158    {
 159      out.printLine(s).flush
 160      return this
 161    }
 162  
 163  //////////////////////////////////////////////////////////////////////////
 164  // Fields
 165  //////////////////////////////////////////////////////////////////////////
 166  
 167    ** Max severity of log entries to report
 168    LogLevel level := LogLevel.message
 169  
 170    ** Current level of indentation
 171    Int indentation := 0
 172  
 173    ** Sink for all output
 174    OutStream out := null
 175  
 176  }