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 debug level enabled
  35    **
  36    Bool isDebug()
  37    {
  38      return level <= LogLevel.debug
  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    ** Log an error level message.
  60    **
  61    Void error(Str msg, Err err := null)
  62    {
  63      log(LogRecord.make(DateTime.now, LogLevel.error, null, msg, err))
  64    }
  65  
  66    **
  67    ** Log a warn level message.
  68    **
  69    Void warn(Str msg, Err err := null)
  70    {
  71      log(LogRecord.make(DateTime.now, LogLevel.warn, null, msg, err))
  72    }
  73  
  74    **
  75    ** Log an info level message.
  76    **
  77    Void info(Str msg, Err err := null)
  78    {
  79      log(LogRecord.make(DateTime.now, LogLevel.info, null, msg, err))
  80    }
  81  
  82    **
  83    ** Log an debug level message.
  84    **
  85    Void debug(Str msg, Err err := null)
  86    {
  87      log(LogRecord.make(DateTime.now, LogLevel.debug, null, msg, err))
  88    }
  89  
  90    **
  91    ** Generate a log entry.  The log entry is only generated
  92    ** if the specified level is greater than or equal to
  93    ** the configured level field.
  94    **
  95    virtual Void log(LogRecord rec)
  96    {
  97      if (rec.level < this.level) return
  98      if (rec.level >= LogLevel.warn) print(rec.level.toStr.capitalize).print(": ")
  99      print(Str.spaces(indentation*2))
 100      printLine(rec.message);
 101  
 102      if (rec.err != null)
 103      {
 104        if (isDebug)
 105          rec.err.trace(out)
 106        else
 107          print(Str.spaces(indentation*2+4)).printLine(rec.err)
 108      }
 109    }
 110  
 111  //////////////////////////////////////////////////////////////////////////
 112  // CompilerErr
 113  //////////////////////////////////////////////////////////////////////////
 114  
 115    **
 116    ** Log a CompilerErr
 117    **
 118    virtual Void compilerErr(CompilerErr err)
 119    {
 120      if (LogLevel.error < this.level) return
 121      if (err.location != null)
 122      {
 123        printLine(err.location.toLocationStr + ": " + err.message)
 124      }
 125      else
 126      {
 127        printLine(err.message)
 128      }
 129      if (isDebug) err.trace(out)
 130    }
 131  
 132  //////////////////////////////////////////////////////////////////////////
 133  // IO
 134  //////////////////////////////////////////////////////////////////////////
 135  
 136    **
 137    ** Print a string without trailing newline.
 138    **
 139    CompilerLog print(Obj s)
 140    {
 141      out.print(s)
 142      return this
 143    }
 144  
 145    **
 146    ** Print a line.
 147    **
 148    CompilerLog printLine(Obj s := "")
 149    {
 150      out.printLine(s).flush
 151      return this
 152    }
 153  
 154  //////////////////////////////////////////////////////////////////////////
 155  // Fields
 156  //////////////////////////////////////////////////////////////////////////
 157  
 158    ** Max severity of log entries to report
 159    LogLevel level := LogLevel.info
 160  
 161    ** Current level of indentation
 162    Int indentation := 0
 163  
 164    ** Sink for all output
 165    OutStream out := null
 166  
 167  }