Loggin.cs 9.8 KB

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