src/Pure/General/logger.scala
author wenzelm
Tue, 21 Jun 2022 14:51:17 +0200
changeset 75571 ac5e633ad9b3
parent 75393 87ebf5a50283
child 76592 ec8bf1268f45
permissions -rw-r--r--
tuned signature: more operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64606
a871fa7c24fc clarified modules;
wenzelm
parents: 64605
diff changeset
     1
/*  Title:      Pure/General/logger.scala
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     3
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     4
Minimal logging support.
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     5
*/
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     6
64606
a871fa7c24fc clarified modules;
wenzelm
parents: 64605
diff changeset
     7
package isabelle
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     8
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73777
diff changeset
    10
object Logger {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    11
  def make(log_file: Option[Path]): Logger =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    12
    log_file match { case Some(file) => new File_Logger(file) case None => No_Logger }
73777
52e43a93d51f clarified system_log: make this work independently of the particular "isabelle build" command-line (e.g. "isabelle mirabelle");
wenzelm
parents: 73340
diff changeset
    13
52e43a93d51f clarified system_log: make this work independently of the particular "isabelle build" command-line (e.g. "isabelle mirabelle");
wenzelm
parents: 73340
diff changeset
    14
  def make(progress: Progress): Logger =
52e43a93d51f clarified system_log: make this work independently of the particular "isabelle build" command-line (e.g. "isabelle mirabelle");
wenzelm
parents: 73340
diff changeset
    15
    new Logger { def apply(msg: => String): Unit = progress.echo(msg) }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    16
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    17
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73777
diff changeset
    18
trait Logger {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    19
  def apply(msg: => String): Unit
65921
5b42937d3b2d more operations;
wenzelm
parents: 64606
diff changeset
    20
5b42937d3b2d more operations;
wenzelm
parents: 64606
diff changeset
    21
  def timeit[A](message: String = "", enabled: Boolean = true)(e: => A): A =
5b42937d3b2d more operations;
wenzelm
parents: 64606
diff changeset
    22
    Timing.timeit(message, enabled, apply(_))(e)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    23
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    24
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73777
diff changeset
    25
object No_Logger extends Logger {
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 65921
diff changeset
    26
  def apply(msg: => String): Unit = {}
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    27
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    28
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73777
diff changeset
    29
class File_Logger(path: Path) extends Logger {
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 65921
diff changeset
    30
  def apply(msg: => String): Unit = synchronized { File.append(path, msg + "\n") }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    31
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    32
  override def toString: String = path.toString
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    33
}