9 |
9 |
10 object Logger { |
10 object Logger { |
11 def make_file(log_file: Option[Path], guard_time: Time = Time.min): Logger = |
11 def make_file(log_file: Option[Path], guard_time: Time = Time.min): Logger = |
12 log_file match { |
12 log_file match { |
13 case Some(file) => new File_Logger(file, guard_time) |
13 case Some(file) => new File_Logger(file, guard_time) |
14 case None => No_Logger |
14 case None => new Logger |
15 } |
15 } |
16 |
16 |
17 def make_progress(progress: => Progress, guard_time: Time = Time.min): Logger = |
17 def make_progress(progress: => Progress, guard_time: Time = Time.min): Logger = |
18 new Logger { |
18 new Logger { |
19 override def output(msg: => String): Unit = |
19 override def output(msg: => String): Unit = |
20 if (progress != null) progress.echo(msg) |
20 if (progress != null) progress.echo(msg) |
21 } |
21 } |
22 |
22 |
23 def make_system_log(progress: Progress, options: Options, guard_time: Time = Time.min): Logger = |
23 def make_system_log(progress: Progress, options: Options, guard_time: Time = Time.min): Logger = |
24 options.string("system_log") match { |
24 options.string("system_log") match { |
25 case "" => No_Logger |
25 case "" => new Logger |
26 case "-" => make_progress(progress, guard_time = guard_time) |
26 case "-" => make_progress(progress, guard_time = guard_time) |
27 case log_file => make_file(Some(Path.explode(log_file)), guard_time = guard_time) |
27 case log_file => make_file(Some(Path.explode(log_file)), guard_time = guard_time) |
28 } |
28 } |
29 } |
29 } |
30 |
30 |
31 trait Logger { |
31 class Logger { |
32 val guard_time: Time = Time.min |
32 val guard_time: Time = Time.min |
33 def guard(t: Time): Boolean = t >= guard_time |
33 def guard(t: Time): Boolean = t >= guard_time |
34 def output(msg: => String): Unit = {} |
34 def output(msg: => String): Unit = {} |
35 |
35 |
36 final def apply(msg: => String, time: Option[Time] = None): Unit = |
36 final def apply(msg: => String, time: Option[Time] = None): Unit = |
39 final def timeit[A](body: => A, |
39 final def timeit[A](body: => A, |
40 message: Exn.Result[A] => String = null, |
40 message: Exn.Result[A] => String = null, |
41 enabled: Boolean = true |
41 enabled: Boolean = true |
42 ): A = Timing.timeit(body, message = message, enabled = enabled, output = apply(_)) |
42 ): A = Timing.timeit(body, message = message, enabled = enabled, output = apply(_)) |
43 } |
43 } |
44 |
|
45 object No_Logger extends Logger |
|
46 |
44 |
47 class File_Logger(path: Path, override val guard_time: Time = Time.min) |
45 class File_Logger(path: Path, override val guard_time: Time = Time.min) |
48 extends Logger { |
46 extends Logger { |
49 override def output(msg: => String): Unit = synchronized { File.append(path, msg + "\n") } |
47 override def output(msg: => String): Unit = synchronized { File.append(path, msg + "\n") } |
50 override def toString: String = path.toString |
48 override def toString: String = path.toString |