Loggin.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace CommonLang.Log
  6. {
  7. public class LoggerFactory
  8. {
  9. public static LoggerFactory CurrentFactory { get; private set; }
  10. static LoggerFactory()
  11. {
  12. CurrentFactory = new LoggerFactory();
  13. }
  14. public static void SetFactory(LoggerFactory factory)
  15. {
  16. CurrentFactory = factory;
  17. }
  18. public static Logger GetLogger(string name)
  19. {
  20. return CurrentFactory.CreateLogger(name);
  21. }
  22. private static Logger mDefaultLog = null;
  23. public static Logger GetDefLogger()
  24. {
  25. if(mDefaultLog == null)
  26. {
  27. mDefaultLog = GetLogger("logger");
  28. }
  29. return mDefaultLog;
  30. }
  31. virtual protected Logger CreateLogger(string name)
  32. {
  33. Logger logger = new ConsoleLogger();
  34. logger.SetName(name);
  35. return logger;
  36. }
  37. public static Logger Dummy { get { return new DummyLogger(); } }
  38. private class DummyLogger : Logger
  39. {
  40. protected override void Print(string text)
  41. {
  42. }
  43. }
  44. }
  45. public class LoggerLevel
  46. {
  47. //public const uint FINE = 0x00000001;
  48. public const uint DEBUG = 0x00000002;
  49. //public const uint TRACE = 0x00000004;
  50. //public const uint LOG = 0x00000008;
  51. public const uint INFO = 0x00000010;
  52. public const uint WARNNING = 0x00000020;
  53. public const uint ERROR = 0x00000040;
  54. //public const uint FATAL = 0x00000080;
  55. public const uint LIXU = 0x00000100;
  56. }
  57. public abstract class Logger
  58. {
  59. private uint mLevel = 0xFFFFFFFF;
  60. protected string mName = "Log - ";
  61. public uint Level { get { return mLevel; } }
  62. //---------------------------------------------------------------------------------------
  63. protected internal void SetName(string name)
  64. {
  65. mName = name;
  66. }
  67. //---------------------------------------------------------------------------------------
  68. public bool IsDebugEnabled { get { return (mLevel & LoggerLevel.DEBUG) != 0; } }
  69. public bool IsErrorEnabled { get { return (mLevel & LoggerLevel.ERROR) != 0; } }
  70. public bool IsInfoEnabled { get { return (mLevel & LoggerLevel.INFO) != 0; } }
  71. public bool IsWarnEnabled { get { return (mLevel & LoggerLevel.WARNNING) != 0; } }
  72. //---------------------------------------------------------------------------------------
  73. public void SetLevel(uint level)
  74. {
  75. mLevel = level;
  76. }
  77. public bool IsLevelEnable(uint level)
  78. {
  79. return ((level & mLevel) != 0);
  80. // return level >= mLevel;
  81. }
  82. //---------------------------------------------------------------------------------------
  83. public virtual void LogLevel(uint level, string msg, Exception err)
  84. {
  85. if (err != null)
  86. {
  87. Print(msg, err);
  88. }
  89. else
  90. {
  91. Print(msg);
  92. }
  93. }
  94. virtual protected void LogLevelInternal(uint level, object msg, object[] args, Exception exception)
  95. {
  96. if (IsLevelEnable(level))
  97. {
  98. string message = msg != null ? msg.ToString() : "null";
  99. if (args != null && args.Length > 0)
  100. {
  101. message = string.Format(message, args);
  102. }
  103. LogLevel(level, message, exception);
  104. }
  105. }
  106. virtual protected void Print(string text)
  107. {
  108. }
  109. protected void Print(string text, Exception err)
  110. {
  111. Print(text + " : " + err.Message + "\r\n" + err.StackTrace);
  112. }
  113. //---------------------------------------------------------------------------------------
  114. public void Log(string msg)
  115. {
  116. LogLevelInternal(LoggerLevel.INFO, msg, null, null);
  117. }
  118. public void Info(string msg)
  119. {
  120. LogLevelInternal(LoggerLevel.INFO, msg, null, null);
  121. }
  122. public void Error(string msg)
  123. {
  124. LogLevelInternal(LoggerLevel.ERROR, msg, null, null);
  125. }
  126. public void Error(string msg, Exception err)
  127. {
  128. LogLevelInternal(LoggerLevel.ERROR, msg, null, err);
  129. }
  130. //---------------------------------------------------------------------------------------
  131. public void Debug(object message)
  132. {
  133. LogLevelInternal(LoggerLevel.DEBUG, message, null, null);
  134. }
  135. public void Debug(object message, Exception exception)
  136. {
  137. LogLevelInternal(LoggerLevel.DEBUG, message, null, exception);
  138. }
  139. public void DebugFormat(string format, params object[] args)
  140. {
  141. LogLevelInternal(LoggerLevel.DEBUG, format, args, null);
  142. }
  143. //---------------------------------------------------------------------------------------
  144. public void Error(object message)
  145. {
  146. LogLevelInternal(LoggerLevel.ERROR, message, null, null);
  147. }
  148. public void Error(object message, Exception exception)
  149. {
  150. LogLevelInternal(LoggerLevel.ERROR, message, null, exception);
  151. }
  152. public void ErrorFormat(string format, params object[] args)
  153. {
  154. LogLevelInternal(LoggerLevel.ERROR, format, args, null);
  155. }
  156. public void Info(object message)
  157. {
  158. LogLevelInternal(LoggerLevel.INFO, message, null, null);
  159. }
  160. public void Info(object message, Exception exception)
  161. {
  162. LogLevelInternal(LoggerLevel.INFO, message, null, exception);
  163. }
  164. public void InfoFormat(string format, params object[] args)
  165. {
  166. LogLevelInternal(LoggerLevel.INFO, format, args, null);
  167. }
  168. //---------------------------------------------------------------------------------------
  169. public void Warn(object message)
  170. {
  171. LogLevelInternal(LoggerLevel.WARNNING, message, null, null);
  172. }
  173. public void Warn(object message, Exception exception)
  174. {
  175. LogLevelInternal(LoggerLevel.WARNNING, message, null, exception);
  176. }
  177. public void WarnFormat(string format, params object[] args)
  178. {
  179. LogLevelInternal(LoggerLevel.WARNNING, format, args, null);
  180. }
  181. //---------------------------------------------------------------------------------------
  182. public void LogSelf(uint level, object message)
  183. {
  184. LogLevelInternal(level, message, null, null);
  185. }
  186. }
  187. public class BlankLogger : Logger
  188. {
  189. protected override void Print(string text)
  190. {
  191. }
  192. }
  193. public class TextWriterLogger : Logger
  194. {
  195. //private StreamWriter output;
  196. public TextWriterLogger(StreamWriter output, string name)
  197. {
  198. //this.output = output;
  199. //this.output.AutoFlush = true;
  200. //this.SetName(name);
  201. }
  202. protected override void Print(string text)
  203. {
  204. //output.WriteLine(mName + " - " + text);
  205. }
  206. }
  207. public class ConsoleLogger : Logger
  208. {
  209. #if ClientOnly
  210. public override void LogLevel(uint level, string msg, Exception err)
  211. {
  212. if (err != null)
  213. {
  214. msg = msg + " : " + err.Message + "\r\n" + err.StackTrace;
  215. }
  216. if(level == LoggerLevel.ERROR)
  217. {
  218. YXJDebug.logError(msg);
  219. }
  220. else if(level == LoggerLevel.WARNNING)
  221. {
  222. YXJDebug.logWarning(msg);
  223. }
  224. else if(level == LoggerLevel.INFO)
  225. {
  226. YXJDebug.logInfo(msg);
  227. }
  228. else
  229. {
  230. YXJDebug.logDebug(msg);
  231. }
  232. }
  233. #endif
  234. }
  235. public class ListLogger : Logger
  236. {
  237. public static bool EnableConsole = true;
  238. //private List<string> output = new List<string>();
  239. public ListLogger(string name)
  240. {
  241. this.SetName(name);
  242. }
  243. protected override void Print(string text)
  244. {
  245. //lock (output)
  246. //{
  247. // output.Add(text);
  248. // if (EnableConsole) Console.WriteLine(mName + " - " + text);
  249. //}
  250. }
  251. public void PopLogs(List<string> input)
  252. {
  253. //lock (output)
  254. //{
  255. // input.AddRange(output);
  256. // output.Clear();
  257. //}
  258. }
  259. }
  260. public class ClientLog
  261. {
  262. #if ClientOnly
  263. private static Logger mLog = null;
  264. #endif
  265. public static void Log(uint level, string msg, params object[] args)
  266. {
  267. #if ClientOnly
  268. if (mLog == null)
  269. {
  270. mLog = LoggerFactory.GetLogger("Client");
  271. }
  272. if (args != null && args.Length > 0)
  273. {
  274. msg = string.Format(msg, args);
  275. }
  276. mLog.LogLevel(level, msg, null);
  277. #endif
  278. }
  279. public static void LogError(string msg, params object[] args)
  280. {
  281. Log(LoggerLevel.ERROR, msg, args);
  282. }
  283. public static void LogWarning(string msg, params object[] args)
  284. {
  285. Log(LoggerLevel.WARNNING, msg, args);
  286. }
  287. public static void LogDebug(string msg, params object[] args)
  288. {
  289. Log(LoggerLevel.DEBUG, msg, args);
  290. }
  291. public static void LogInfo(string msg, params object[] args)
  292. {
  293. Log(LoggerLevel.INFO, msg, args);
  294. }
  295. }
  296. }