summaryrefslogtreecommitdiff
path: root/Source/AIFramework/Logger.cs
blob: aa7c5979ac02e625e947c926ad8709cafe5b5c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
namespace Microsoft.AbstractInterpretationFramework {
  using System;
  using System.Diagnostics;
  using System.Diagnostics.Contracts;

  public class Logger {
    private string/*!*/ dbgmsgContext;
    private static int contextWidth = 0;
    [ContractInvariantMethod]
    void ObjectInvariant() {
      Contract.Invariant(dbgmsgContext != null);
      Contract.Invariant(dbgmsgIndent != null);
    }


    public bool Enabled = false;

    public Logger(string/*!*/ contextMsg) {
      Contract.Requires(contextMsg != null);
      this.dbgmsgContext = "[" + contextMsg + "] ";
      contextWidth = Math.Max(contextWidth, contextMsg.Length + 3);
      // base();
    }

    private static System.Text.StringBuilder/*!*/ dbgmsgIndent = new System.Text.StringBuilder();

    public void DbgMsgIndent() {
      dbgmsgIndent.Append(' ', 2);
    }
    public void DbgMsgUnindent() {
      if (dbgmsgIndent.Length >= 2)
        dbgmsgIndent.Remove(0, 2);
    }

    [ConditionalAttribute("DEBUG")]
    public void DbgMsg(string msg) {
      if (Enabled)
        Debug.WriteLine(dbgmsgContext.PadRight(contextWidth) + dbgmsgIndent + msg);
    }
    [ConditionalAttribute("DEBUG")]
    public void DbgMsgNoLine(string msg) {
      if (Enabled)
        Debug.Write(dbgmsgContext.PadRight(contextWidth) + dbgmsgIndent + msg);
    }
    [ConditionalAttribute("DEBUG")]
    public void DbgMsgPlain(string msg) {
      if (Enabled)
        Debug.Write(msg);
    }
  }
}