fan-1.0 /src/sys/dotnet/fan/sys/Log.cs

Language C# Lines 250
MD5 Hash ae87f28e2b71e0103ed711d396670aae Estimated Cost $4,229 (why?)
Repository https://bitbucket.org/bedlaczech/fan-1.0 View Raw File View Project SPDX
  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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// Copyright (c) 2007, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
//  24 Dec 07  Andy Frank  Creation
//

using System.Collections;

namespace Fan.Sys
{
  /// <summary>
  /// Log provides a simple, but standardized mechanism for logging.
  /// </summary>
  public class Log : FanObj
  {

  //////////////////////////////////////////////////////////////////////////
  // Construction
  //////////////////////////////////////////////////////////////////////////

    public static List list()
    {
      lock (lockObj)
      {
        Log[] arr = new Log[byName.Count];
        byName.Values.CopyTo(arr, 0);
        return new List(Sys.LogType, arr).ro();
      }
    }

    public static Log find(string name) { return find(name, true); }
    public static Log find(string name, bool check)
    {
      lock (lockObj)
      {
        Log log = (Log)byName[name];
        if (log != null) return log;
        if (check) throw Err.make("Unknown log: " + name).val;
        return null;
      }
    }

    public static Log get(string name)
    {
      lock (lockObj)
      {
        Log log = (Log)byName[name];
        if (log != null) return log;
        return make(name, true);
      }
    }

    public static Log make(string name, bool register)
    {
      Log self = new Log();
      make_(self, name, register);
      return self;
    }

    public static void make_(Log self, string name, bool register)
    {
      // verify valid name
      Uri.checkName(name);
      self.m_name = name;

      if (register)
      {
        lock (lockObj)
        {
          // verify unique
          if (byName[name] != null)
            throw ArgErr.make("Duplicate log name: " + name).val;

          // init and put into map
          byName[name] = self;

          // check for initial level
          string val = (string)Sys.m_sysPod.props(Uri.fromStr("log.props"), Duration.m_oneMin).get(name);
          if (val != null) self.m_level = LogLevel.fromStr(val);
        }
      }
    }

  //////////////////////////////////////////////////////////////////////////
  // Identity
  //////////////////////////////////////////////////////////////////////////

    public override Type @typeof()
    {
      return Sys.LogType;
    }

    public sealed override string toStr()
    {
      return m_name;
    }

    public string name()
    {
      return m_name;
    }

  //////////////////////////////////////////////////////////////////////////
  // Severity Level
  //////////////////////////////////////////////////////////////////////////

    public LogLevel level()
    {
      return m_level;
    }

    public void level(LogLevel level)
    {
      if (level == null) throw ArgErr.make("level cannot be null").val;
      this.m_level = level;
    }

    public bool enabled(LogLevel level)
    {
      return this.m_level.m_ord <= level.m_ord;
    }

    public bool isEnabled(LogLevel level)
    {
      return enabled(level);
    }

    public bool isErr() { return isEnabled(LogLevel.m_err); }

    public bool isWarn()  { return isEnabled(LogLevel.m_warn); }

    public bool isInfo()  { return isEnabled(LogLevel.m_info); }

    public bool isDebug() { return isEnabled(LogLevel.m_debug); }

  //////////////////////////////////////////////////////////////////////////
  // Logging
  //////////////////////////////////////////////////////////////////////////

    public void err(string message) { err(message, (Err)null); }
    public void err(string message, System.Exception e) { err(message, Err.make(e)); }
    public void err(string message, Err err)
    {
      log(LogRec.make(DateTime.now(), LogLevel.m_err, m_name, message, err));
    }

    public void warn(string message) { warn(message, (Err)null); }
    public void warn(string message, System.Exception e) { warn(message, Err.make(e)); }
    public void warn(string message, Err err)
    {
      log(LogRec.make(DateTime.now(), LogLevel.m_warn, m_name, message, err));
    }

    public void info(string message) { info(message, (Err)null); }
    public void info(string message, System.Exception e) { info(message, Err.make(e)); }
    public void info(string message, Err err)
    {
      log(LogRec.make(DateTime.now(), LogLevel.m_info, m_name, message, err));
    }

    public void debug(string message) { debug(message, (Err)null); }
    public void debug(string message, System.Exception e) { debug(message, Err.make(e)); }
    public void debug(string message, Err err)
    {
      log(LogRec.make(DateTime.now(), LogLevel.m_debug, m_name, message, err));
    }

    public virtual void log(LogRec rec)
    {
      if (!enabled(rec.m_level)) return;

      Func[] handlers = Log.m_handlers;
      for (int i=0; i<handlers.Length; ++i)
      {
        try
        {
          handlers[i].call(rec);
        }
        catch (System.Exception e)
        {
          Err.dumpStack(e);
        }
      }
    }

  //////////////////////////////////////////////////////////////////////////
  // Handlers
  //////////////////////////////////////////////////////////////////////////

    public static List handlers()
    {
      return new List(Sys.FuncType, m_handlers).ro();
    }

    public static void addHandler(Func func)
    {
      if (!func.isImmutable())
        throw NotImmutableErr.make("handler must be immutable").val;

      lock (lockObj)
      {
        List temp = new List(Sys.FuncType, m_handlers).add(func);
        m_handlers = (Func[])temp.toArray(new Func[temp.sz()]);
      }
    }

    public static void removeHandler(Func func)
    {
      lock (lockObj)
      {
        List temp = new List(Sys.FuncType, m_handlers);
        temp.remove(func);
        m_handlers = (Func[])temp.toArray(new Func[temp.sz()]);
      }
    }

    // handlers
    static volatile Func[] m_handlers = new Func[1];

  //////////////////////////////////////////////////////////////////////////
  // Static Init
  //////////////////////////////////////////////////////////////////////////

    static Log()
    {
      try
      {
        m_handlers[0] = Sys.LogRecType.method("print", true).func();
      }
      catch (System.Exception e)
      {
        m_handlers = new Func[0];
        Err.dumpStack(e);
      }
    }

  //////////////////////////////////////////////////////////////////////////
  // Fields
  //////////////////////////////////////////////////////////////////////////

    private static object lockObj = new System.Object();  // synchronization
    private static Hashtable byName = new Hashtable();    // string -> Log

    private string m_name;
    private volatile LogLevel m_level = LogLevel.m_info;

  }
}
Back to Top