NRDebugger.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System.Diagnostics;
  12. /// <summary> Values that represent log levels. </summary>
  13. public enum LogLevel
  14. {
  15. /// <summary> An enum constant representing all option. </summary>
  16. All = 0,
  17. /// <summary> An enum constant representing the debug option. </summary>
  18. Debug,
  19. /// <summary> An enum constant representing the Information option. </summary>
  20. Info,
  21. /// <summary> An enum constant representing the warning option. </summary>
  22. Warning,
  23. /// <summary> An enum constant representing the error option. </summary>
  24. Error,
  25. /// <summary> An enum constant representing the fatal option. </summary>
  26. Fatal,
  27. /// <summary> An enum constant representing the off option. </summary>
  28. Off
  29. }
  30. /// <summary> Interface for log helper. </summary>
  31. public interface ILogHelper
  32. {
  33. /// <summary> Logs. </summary>
  34. /// <param name="level"> The level.</param>
  35. /// <param name="message"> The message.</param>
  36. void Log(LogLevel level, object message);
  37. }
  38. /// <summary> A default log helper. </summary>
  39. public class DefaultLogHelper : ILogHelper
  40. {
  41. /// <summary> Logs. </summary>
  42. /// <exception cref="Exception"> Thrown when an exception error condition occurs.</exception>
  43. /// <param name="level"> The level.</param>
  44. /// <param name="message"> The message.</param>
  45. public void Log(LogLevel level, object message)
  46. {
  47. string log_info = string.Format("[{0}]{1}", level.ToString(), message.ToString());
  48. switch (level)
  49. {
  50. case LogLevel.Debug:
  51. UnityEngine.Debug.Log(log_info);
  52. break;
  53. case LogLevel.Info:
  54. UnityEngine.Debug.Log(log_info);
  55. break;
  56. case LogLevel.Warning:
  57. UnityEngine.Debug.LogWarning(log_info);
  58. break;
  59. case LogLevel.Error:
  60. UnityEngine.Debug.LogError(log_info);
  61. break;
  62. default:
  63. throw new System.Exception(log_info);
  64. }
  65. }
  66. }
  67. /// <summary> A tool for log. </summary>
  68. public class NRDebugger
  69. {
  70. private static ILogHelper m_LogHelper = new DefaultLogHelper();
  71. /// <summary> The log level. </summary>
  72. private static LogLevel m_LogLevel = LogLevel.Warning;
  73. /// <summary> Gets or sets the log level. </summary>
  74. /// <value> The log level. </value>
  75. public static LogLevel logLevel
  76. {
  77. get { return m_LogLevel; }
  78. set { m_LogLevel = value; }
  79. }
  80. /// <summary> Set the log helper. </summary>
  81. /// <param name="logHelper"> The log helper.</param>
  82. public static void SetLogHelper(ILogHelper logHelper)
  83. {
  84. m_LogHelper = logHelper;
  85. }
  86. /// <summary> (Only available in DEBUG builds) debugs. </summary>
  87. /// <param name="message"> The message.</param>
  88. [Conditional("DEBUG")]
  89. public static void Debug(object message)
  90. {
  91. if (m_LogHelper == null)
  92. {
  93. return;
  94. }
  95. if (m_LogLevel <= LogLevel.Debug)
  96. {
  97. m_LogHelper.Log(LogLevel.Debug, message);
  98. }
  99. }
  100. /// <summary> (Only available in DEBUG builds) debugs. </summary>
  101. /// <param name="message"> The message.</param>
  102. [Conditional("DEBUG")]
  103. public static void Debug(string message)
  104. {
  105. if (m_LogHelper == null)
  106. {
  107. return;
  108. }
  109. if (m_LogLevel <= LogLevel.Debug)
  110. {
  111. m_LogHelper.Log(LogLevel.Debug, message);
  112. }
  113. }
  114. /// <summary> (Only available in DEBUG builds) debugs. </summary>
  115. /// <param name="format"> Describes the format to use.</param>
  116. /// <param name="arg0"> The argument 0.</param>
  117. [Conditional("DEBUG")]
  118. public static void Debug(string format, object arg0)
  119. {
  120. if (m_LogHelper == null)
  121. {
  122. return;
  123. }
  124. if (m_LogLevel <= LogLevel.Debug)
  125. {
  126. m_LogHelper.Log(LogLevel.Debug, StringUtility.Format(format, arg0));
  127. }
  128. }
  129. /// <summary> (Only available in DEBUG builds) debugs. </summary>
  130. /// <param name="format"> Describes the format to use.</param>
  131. /// <param name="arg0"> The argument 0.</param>
  132. /// <param name="arg1"> The first argument.</param>
  133. [Conditional("DEBUG")]
  134. public static void Debug(string format, object arg0, object arg1)
  135. {
  136. if (m_LogHelper == null)
  137. {
  138. return;
  139. }
  140. if (m_LogLevel <= LogLevel.Debug)
  141. {
  142. m_LogHelper.Log(LogLevel.Debug, StringUtility.Format(format, arg0, arg1));
  143. }
  144. }
  145. /// <summary> (Only available in DEBUG builds) debugs. </summary>
  146. /// <param name="format"> Describes the format to use.</param>
  147. /// <param name="arg0"> The argument 0.</param>
  148. /// <param name="arg1"> The first argument.</param>
  149. /// <param name="arg2"> The second argument.</param>
  150. [Conditional("DEBUG")]
  151. public static void Debug(string format, object arg0, object arg1, object arg2)
  152. {
  153. if (m_LogHelper == null)
  154. {
  155. return;
  156. }
  157. if (m_LogLevel <= LogLevel.Debug)
  158. {
  159. m_LogHelper.Log(LogLevel.Debug, StringUtility.Format(format, arg0, arg1, arg2));
  160. }
  161. }
  162. /// <summary> (Only available in DEBUG builds) debugs. </summary>
  163. /// <param name="format"> Describes the format to use.</param>
  164. /// <param name="args"> A variable-length parameters list containing arguments.</param>
  165. [Conditional("DEBUG")]
  166. public static void Debug(string format, params object[] args)
  167. {
  168. if (m_LogHelper == null)
  169. {
  170. return;
  171. }
  172. if (m_LogLevel <= LogLevel.Debug)
  173. {
  174. m_LogHelper.Log(LogLevel.Debug, StringUtility.Format(format, args));
  175. }
  176. }
  177. /// <summary> Infoes. </summary>
  178. /// <param name="message"> The message.</param>
  179. public static void Info(object message)
  180. {
  181. if (m_LogHelper == null)
  182. {
  183. return;
  184. }
  185. if (m_LogLevel <= LogLevel.Info)
  186. {
  187. m_LogHelper.Log(LogLevel.Info, message);
  188. }
  189. }
  190. /// <summary> Infoes. </summary>
  191. /// <param name="message"> The message.</param>
  192. public static void Info(string message)
  193. {
  194. if (m_LogHelper == null)
  195. {
  196. return;
  197. }
  198. if (m_LogLevel <= LogLevel.Info)
  199. {
  200. m_LogHelper.Log(LogLevel.Info, message);
  201. }
  202. }
  203. /// <summary> Infoes. </summary>
  204. /// <param name="format"> Describes the format to use.</param>
  205. /// <param name="arg0"> The argument 0.</param>
  206. public static void Info(string format, object arg0)
  207. {
  208. if (m_LogHelper == null)
  209. {
  210. return;
  211. }
  212. if (m_LogLevel <= LogLevel.Info)
  213. {
  214. m_LogHelper.Log(LogLevel.Info, StringUtility.Format(format, arg0));
  215. }
  216. }
  217. /// <summary> Infoes. </summary>
  218. /// <param name="format"> Describes the format to use.</param>
  219. /// <param name="arg0"> The argument 0.</param>
  220. /// <param name="arg1"> The first argument.</param>
  221. public static void Info(string format, object arg0, object arg1)
  222. {
  223. if (m_LogHelper == null)
  224. {
  225. return;
  226. }
  227. if (m_LogLevel <= LogLevel.Info)
  228. {
  229. m_LogHelper.Log(LogLevel.Info, StringUtility.Format(format, arg0, arg1));
  230. }
  231. }
  232. /// <summary> Infoes. </summary>
  233. /// <param name="format"> Describes the format to use.</param>
  234. /// <param name="arg0"> The argument 0.</param>
  235. /// <param name="arg1"> The first argument.</param>
  236. /// <param name="arg2"> The second argument.</param>
  237. public static void Info(string format, object arg0, object arg1, object arg2)
  238. {
  239. if (m_LogHelper == null)
  240. {
  241. return;
  242. }
  243. if (m_LogLevel <= LogLevel.Info)
  244. {
  245. m_LogHelper.Log(LogLevel.Info, StringUtility.Format(format, arg0, arg1, arg2));
  246. }
  247. }
  248. /// <summary> Infoes. </summary>
  249. /// <param name="format"> Describes the format to use.</param>
  250. /// <param name="args"> A variable-length parameters list containing arguments.</param>
  251. public static void Info(string format, params object[] args)
  252. {
  253. if (m_LogHelper == null)
  254. {
  255. return;
  256. }
  257. if (m_LogLevel <= LogLevel.Info)
  258. {
  259. m_LogHelper.Log(LogLevel.Info, StringUtility.Format(format, args));
  260. }
  261. }
  262. /// <summary> Warnings. </summary>
  263. /// <param name="message"> The message.</param>
  264. public static void Warning(object message)
  265. {
  266. if (m_LogHelper == null)
  267. {
  268. return;
  269. }
  270. if (m_LogLevel <= LogLevel.Warning)
  271. {
  272. m_LogHelper.Log(LogLevel.Warning, message);
  273. }
  274. }
  275. /// <summary> Warnings. </summary>
  276. /// <param name="message"> The message.</param>
  277. public static void Warning(string message)
  278. {
  279. if (m_LogHelper == null)
  280. {
  281. return;
  282. }
  283. if (m_LogLevel <= LogLevel.Warning)
  284. {
  285. m_LogHelper.Log(LogLevel.Warning, message);
  286. }
  287. }
  288. /// <summary> Warnings. </summary>
  289. /// <param name="format"> Describes the format to use.</param>
  290. /// <param name="arg0"> The argument 0.</param>
  291. public static void Warning(string format, object arg0)
  292. {
  293. if (m_LogHelper == null)
  294. {
  295. return;
  296. }
  297. if (m_LogLevel <= LogLevel.Warning)
  298. {
  299. m_LogHelper.Log(LogLevel.Warning, StringUtility.Format(format, arg0));
  300. }
  301. }
  302. /// <summary> Warnings. </summary>
  303. /// <param name="format"> Describes the format to use.</param>
  304. /// <param name="arg0"> The argument 0.</param>
  305. /// <param name="arg1"> The first argument.</param>
  306. public static void Warning(string format, object arg0, object arg1)
  307. {
  308. if (m_LogHelper == null)
  309. {
  310. return;
  311. }
  312. if (m_LogLevel <= LogLevel.Warning)
  313. {
  314. m_LogHelper.Log(LogLevel.Warning, StringUtility.Format(format, arg0, arg1));
  315. }
  316. }
  317. /// <summary> Warnings. </summary>
  318. /// <param name="format"> Describes the format to use.</param>
  319. /// <param name="arg0"> The argument 0.</param>
  320. /// <param name="arg1"> The first argument.</param>
  321. /// <param name="arg2"> The second argument.</param>
  322. public static void Warning(string format, object arg0, object arg1, object arg2)
  323. {
  324. if (m_LogHelper == null)
  325. {
  326. return;
  327. }
  328. if (m_LogLevel <= LogLevel.Warning)
  329. {
  330. m_LogHelper.Log(LogLevel.Warning, StringUtility.Format(format, arg0, arg1, arg2));
  331. }
  332. }
  333. /// <summary> Warnings. </summary>
  334. /// <param name="format"> Describes the format to use.</param>
  335. /// <param name="args"> A variable-length parameters list containing arguments.</param>
  336. public static void Warning(string format, params object[] args)
  337. {
  338. if (m_LogHelper == null)
  339. {
  340. return;
  341. }
  342. if (m_LogLevel <= LogLevel.Warning)
  343. {
  344. m_LogHelper.Log(LogLevel.Warning, StringUtility.Format(format, args));
  345. }
  346. }
  347. /// <summary> Errors. </summary>
  348. /// <param name="message"> The message.</param>
  349. public static void Error(object message)
  350. {
  351. if (m_LogHelper == null)
  352. {
  353. return;
  354. }
  355. if (m_LogLevel <= LogLevel.Error)
  356. {
  357. m_LogHelper.Log(LogLevel.Error, message);
  358. }
  359. }
  360. /// <summary> Errors. </summary>
  361. /// <param name="message"> The message.</param>
  362. public static void Error(string message)
  363. {
  364. if (m_LogHelper == null)
  365. {
  366. return;
  367. }
  368. if (m_LogLevel <= LogLevel.Error)
  369. {
  370. m_LogHelper.Log(LogLevel.Error, message);
  371. }
  372. }
  373. /// <summary> Errors. </summary>
  374. /// <param name="format"> Describes the format to use.</param>
  375. /// <param name="arg0"> The argument 0.</param>
  376. public static void Error(string format, object arg0)
  377. {
  378. if (m_LogHelper == null)
  379. {
  380. return;
  381. }
  382. if (m_LogLevel <= LogLevel.Error)
  383. {
  384. m_LogHelper.Log(LogLevel.Error, StringUtility.Format(format, arg0));
  385. }
  386. }
  387. /// <summary> Errors. </summary>
  388. /// <param name="format"> Describes the format to use.</param>
  389. /// <param name="arg0"> The argument 0.</param>
  390. /// <param name="arg1"> The first argument.</param>
  391. public static void Error(string format, object arg0, object arg1)
  392. {
  393. if (m_LogHelper == null)
  394. {
  395. return;
  396. }
  397. if (m_LogLevel <= LogLevel.Error)
  398. {
  399. m_LogHelper.Log(LogLevel.Error, StringUtility.Format(format, arg0, arg1));
  400. }
  401. }
  402. /// <summary> Errors. </summary>
  403. /// <param name="format"> Describes the format to use.</param>
  404. /// <param name="arg0"> The argument 0.</param>
  405. /// <param name="arg1"> The first argument.</param>
  406. /// <param name="arg2"> The second argument.</param>
  407. public static void Error(string format, object arg0, object arg1, object arg2)
  408. {
  409. if (m_LogHelper == null)
  410. {
  411. return;
  412. }
  413. if (m_LogLevel <= LogLevel.Error)
  414. {
  415. m_LogHelper.Log(LogLevel.Error, StringUtility.Format(format, arg0, arg1, arg2));
  416. }
  417. }
  418. /// <summary> Errors. </summary>
  419. /// <param name="format"> Describes the format to use.</param>
  420. /// <param name="args"> A variable-length parameters list containing arguments.</param>
  421. public static void Error(string format, params object[] args)
  422. {
  423. if (m_LogHelper == null)
  424. {
  425. return;
  426. }
  427. if (m_LogLevel <= LogLevel.Error)
  428. {
  429. m_LogHelper.Log(LogLevel.Error, StringUtility.Format(format, args));
  430. }
  431. }
  432. /// <summary> Fatals. </summary>
  433. /// <param name="message"> The message.</param>
  434. public static void Fatal(object message)
  435. {
  436. if (m_LogHelper == null)
  437. {
  438. return;
  439. }
  440. if (m_LogLevel <= LogLevel.Fatal)
  441. {
  442. m_LogHelper.Log(LogLevel.Fatal, message);
  443. }
  444. }
  445. /// <summary> Fatals. </summary>
  446. /// <param name="message"> The message.</param>
  447. public static void Fatal(string message)
  448. {
  449. if (m_LogHelper == null)
  450. {
  451. return;
  452. }
  453. if (m_LogLevel <= LogLevel.Fatal)
  454. {
  455. m_LogHelper.Log(LogLevel.Fatal, message);
  456. }
  457. }
  458. /// <summary> Fatals. </summary>
  459. /// <param name="format"> Describes the format to use.</param>
  460. /// <param name="arg0"> The argument 0.</param>
  461. public static void Fatal(string format, object arg0)
  462. {
  463. if (m_LogHelper == null)
  464. {
  465. return;
  466. }
  467. if (m_LogLevel <= LogLevel.Fatal)
  468. {
  469. m_LogHelper.Log(LogLevel.Fatal, StringUtility.Format(format, arg0));
  470. }
  471. }
  472. /// <summary> Fatals. </summary>
  473. /// <param name="format"> Describes the format to use.</param>
  474. /// <param name="arg0"> The argument 0.</param>
  475. /// <param name="arg1"> The first argument.</param>
  476. public static void Fatal(string format, object arg0, object arg1)
  477. {
  478. if (m_LogHelper == null)
  479. {
  480. return;
  481. }
  482. if (m_LogLevel <= LogLevel.Fatal)
  483. {
  484. m_LogHelper.Log(LogLevel.Fatal, StringUtility.Format(format, arg0, arg1));
  485. }
  486. }
  487. /// <summary> Fatals. </summary>
  488. /// <param name="format"> Describes the format to use.</param>
  489. /// <param name="arg0"> The argument 0.</param>
  490. /// <param name="arg1"> The first argument.</param>
  491. /// <param name="arg2"> The second argument.</param>
  492. public static void Fatal(string format, object arg0, object arg1, object arg2)
  493. {
  494. if (m_LogHelper == null)
  495. {
  496. return;
  497. }
  498. if (m_LogLevel <= LogLevel.Fatal)
  499. {
  500. m_LogHelper.Log(LogLevel.Fatal, StringUtility.Format(format, arg0, arg1, arg2));
  501. }
  502. }
  503. /// <summary> Fatals. </summary>
  504. /// <param name="format"> Describes the format to use.</param>
  505. /// <param name="args"> A variable-length parameters list containing arguments.</param>
  506. public static void Fatal(string format, params object[] args)
  507. {
  508. if (m_LogHelper == null)
  509. {
  510. return;
  511. }
  512. if (m_LogLevel <= LogLevel.Fatal)
  513. {
  514. m_LogHelper.Log(LogLevel.Fatal, StringUtility.Format(format, args));
  515. }
  516. }
  517. }
  518. }