PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/bedlaczech/fan-1.0
C# | 250 lines | 171 code | 43 blank | 36 comment | 15 complexity | ae87f28e2b71e0103ed711d396670aae MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2007, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 24 Dec 07 Andy Frank Creation
  7. //
  8. using System.Collections;
  9. namespace Fan.Sys
  10. {
  11. /// <summary>
  12. /// Log provides a simple, but standardized mechanism for logging.
  13. /// </summary>
  14. public class Log : FanObj
  15. {
  16. //////////////////////////////////////////////////////////////////////////
  17. // Construction
  18. //////////////////////////////////////////////////////////////////////////
  19. public static List list()
  20. {
  21. lock (lockObj)
  22. {
  23. Log[] arr = new Log[byName.Count];
  24. byName.Values.CopyTo(arr, 0);
  25. return new List(Sys.LogType, arr).ro();
  26. }
  27. }
  28. public static Log find(string name) { return find(name, true); }
  29. public static Log find(string name, bool check)
  30. {
  31. lock (lockObj)
  32. {
  33. Log log = (Log)byName[name];
  34. if (log != null) return log;
  35. if (check) throw Err.make("Unknown log: " + name).val;
  36. return null;
  37. }
  38. }
  39. public static Log get(string name)
  40. {
  41. lock (lockObj)
  42. {
  43. Log log = (Log)byName[name];
  44. if (log != null) return log;
  45. return make(name, true);
  46. }
  47. }
  48. public static Log make(string name, bool register)
  49. {
  50. Log self = new Log();
  51. make_(self, name, register);
  52. return self;
  53. }
  54. public static void make_(Log self, string name, bool register)
  55. {
  56. // verify valid name
  57. Uri.checkName(name);
  58. self.m_name = name;
  59. if (register)
  60. {
  61. lock (lockObj)
  62. {
  63. // verify unique
  64. if (byName[name] != null)
  65. throw ArgErr.make("Duplicate log name: " + name).val;
  66. // init and put into map
  67. byName[name] = self;
  68. // check for initial level
  69. string val = (string)Sys.m_sysPod.props(Uri.fromStr("log.props"), Duration.m_oneMin).get(name);
  70. if (val != null) self.m_level = LogLevel.fromStr(val);
  71. }
  72. }
  73. }
  74. //////////////////////////////////////////////////////////////////////////
  75. // Identity
  76. //////////////////////////////////////////////////////////////////////////
  77. public override Type @typeof()
  78. {
  79. return Sys.LogType;
  80. }
  81. public sealed override string toStr()
  82. {
  83. return m_name;
  84. }
  85. public string name()
  86. {
  87. return m_name;
  88. }
  89. //////////////////////////////////////////////////////////////////////////
  90. // Severity Level
  91. //////////////////////////////////////////////////////////////////////////
  92. public LogLevel level()
  93. {
  94. return m_level;
  95. }
  96. public void level(LogLevel level)
  97. {
  98. if (level == null) throw ArgErr.make("level cannot be null").val;
  99. this.m_level = level;
  100. }
  101. public bool enabled(LogLevel level)
  102. {
  103. return this.m_level.m_ord <= level.m_ord;
  104. }
  105. public bool isEnabled(LogLevel level)
  106. {
  107. return enabled(level);
  108. }
  109. public bool isErr() { return isEnabled(LogLevel.m_err); }
  110. public bool isWarn() { return isEnabled(LogLevel.m_warn); }
  111. public bool isInfo() { return isEnabled(LogLevel.m_info); }
  112. public bool isDebug() { return isEnabled(LogLevel.m_debug); }
  113. //////////////////////////////////////////////////////////////////////////
  114. // Logging
  115. //////////////////////////////////////////////////////////////////////////
  116. public void err(string message) { err(message, (Err)null); }
  117. public void err(string message, System.Exception e) { err(message, Err.make(e)); }
  118. public void err(string message, Err err)
  119. {
  120. log(LogRec.make(DateTime.now(), LogLevel.m_err, m_name, message, err));
  121. }
  122. public void warn(string message) { warn(message, (Err)null); }
  123. public void warn(string message, System.Exception e) { warn(message, Err.make(e)); }
  124. public void warn(string message, Err err)
  125. {
  126. log(LogRec.make(DateTime.now(), LogLevel.m_warn, m_name, message, err));
  127. }
  128. public void info(string message) { info(message, (Err)null); }
  129. public void info(string message, System.Exception e) { info(message, Err.make(e)); }
  130. public void info(string message, Err err)
  131. {
  132. log(LogRec.make(DateTime.now(), LogLevel.m_info, m_name, message, err));
  133. }
  134. public void debug(string message) { debug(message, (Err)null); }
  135. public void debug(string message, System.Exception e) { debug(message, Err.make(e)); }
  136. public void debug(string message, Err err)
  137. {
  138. log(LogRec.make(DateTime.now(), LogLevel.m_debug, m_name, message, err));
  139. }
  140. public virtual void log(LogRec rec)
  141. {
  142. if (!enabled(rec.m_level)) return;
  143. Func[] handlers = Log.m_handlers;
  144. for (int i=0; i<handlers.Length; ++i)
  145. {
  146. try
  147. {
  148. handlers[i].call(rec);
  149. }
  150. catch (System.Exception e)
  151. {
  152. Err.dumpStack(e);
  153. }
  154. }
  155. }
  156. //////////////////////////////////////////////////////////////////////////
  157. // Handlers
  158. //////////////////////////////////////////////////////////////////////////
  159. public static List handlers()
  160. {
  161. return new List(Sys.FuncType, m_handlers).ro();
  162. }
  163. public static void addHandler(Func func)
  164. {
  165. if (!func.isImmutable())
  166. throw NotImmutableErr.make("handler must be immutable").val;
  167. lock (lockObj)
  168. {
  169. List temp = new List(Sys.FuncType, m_handlers).add(func);
  170. m_handlers = (Func[])temp.toArray(new Func[temp.sz()]);
  171. }
  172. }
  173. public static void removeHandler(Func func)
  174. {
  175. lock (lockObj)
  176. {
  177. List temp = new List(Sys.FuncType, m_handlers);
  178. temp.remove(func);
  179. m_handlers = (Func[])temp.toArray(new Func[temp.sz()]);
  180. }
  181. }
  182. // handlers
  183. static volatile Func[] m_handlers = new Func[1];
  184. //////////////////////////////////////////////////////////////////////////
  185. // Static Init
  186. //////////////////////////////////////////////////////////////////////////
  187. static Log()
  188. {
  189. try
  190. {
  191. m_handlers[0] = Sys.LogRecType.method("print", true).func();
  192. }
  193. catch (System.Exception e)
  194. {
  195. m_handlers = new Func[0];
  196. Err.dumpStack(e);
  197. }
  198. }
  199. //////////////////////////////////////////////////////////////////////////
  200. // Fields
  201. //////////////////////////////////////////////////////////////////////////
  202. private static object lockObj = new System.Object(); // synchronization
  203. private static Hashtable byName = new Hashtable(); // string -> Log
  204. private string m_name;
  205. private volatile LogLevel m_level = LogLevel.m_info;
  206. }
  207. }