CrasheyeForIOS.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using UnityEngine;
  2. using System.Collections;
  3. #if !UNITY_STANDALONE_WIN
  4. using com.xsj.Crasheye.U3D;
  5. #endif
  6. using System.Runtime.InteropServices;
  7. using System;
  8. public class CrasheyeForIOS : MonoBehaviour
  9. {
  10. #if UNITY_IOS && !UNITY_STANDALONE_WIN
  11. [DllImport("__Internal")]
  12. private static extern void crasheyeInit(string appkey);
  13. [DllImport("__Internal")]
  14. private static extern void crasheyeInitWithChannel(string appkey, string channel);
  15. [DllImport("__Internal")]
  16. private static extern void crasheyeAddExtraData(string key, string value);
  17. [DllImport("__Internal")]
  18. private static extern void crasheyeRemoveExtraData(string key);
  19. [DllImport("__Internal")]
  20. private static extern void crasheyeClearExtraData();
  21. [DllImport("__Internal")]
  22. private static extern void crasheyeLeaveBreadcrumb(string breadcrumb);
  23. [DllImport("__Internal")]
  24. private static extern void crasheyeSetUserid(string userid);
  25. [DllImport("__Internal")]
  26. private static extern void crasheyeSetAppVersion(string version);
  27. [DllImport("__Internal")]
  28. private static extern void crasheyeSetFlushOnlyOverWiFi (int enabled);
  29. [DllImport("__Internal")]
  30. private static extern void crasheyeAddLog(string log);
  31. [DllImport("__Internal")]
  32. private static extern void crasheyeSetURL(string url);
  33. [DllImport("__Internal")]
  34. private static extern void crasheyeRemoveLog();
  35. [DllImport("__Internal")]
  36. private static extern void crasheyeSetBeta (int enabled);
  37. /// <summary>
  38. /// 最大保存log日志数量
  39. /// </summary>
  40. private static int MAX_LOG_COUNT = 300;
  41. private static Queue m_saveLog = new Queue();
  42. /// <summary>
  43. /// iOS系统的初始
  44. /// </summary>
  45. /// <param name="appKeyForIOS"></param>
  46. /// <param name="channIdForIOS"></param>
  47. public static void Init(string appKeyForIOS, string channIdForIOS)
  48. {
  49. if (string.IsNullOrEmpty(appKeyForIOS))
  50. {
  51. Debug.LogError("app key is null or empty!");
  52. return;
  53. }
  54. crasheyeInitWithChannel(appKeyForIOS, channIdForIOS);
  55. Crasheye.crasheyeLib.InitCrasheyeLib();
  56. Crasheye.crasheyeLib.SetExceptionCallback();
  57. }
  58. /// <summary>
  59. /// 设置是否打印调试日志
  60. /// </summary>
  61. /// <param name="debugLog">默认为False为不打印日志;True为打印日志</param>
  62. public static void SetDebugLog(bool debugLog)
  63. {
  64. // 暂时未实现
  65. }
  66. /// <summary>
  67. /// 设置版本号信息
  68. /// </summary>
  69. /// <param name="appVersion"></param>
  70. public static void SetAppVersion(string appVersion)
  71. {
  72. if (string.IsNullOrEmpty(appVersion))
  73. {
  74. Debug.LogError("appVersion is null or empty!");
  75. return;
  76. }
  77. try
  78. {
  79. crasheyeSetAppVersion(appVersion);
  80. }
  81. catch (Exception ex)
  82. {
  83. Debug.LogError(ex.Message);
  84. }
  85. }
  86. public static void SetUserIdentifier(string userIdentifier)
  87. {
  88. if (string.IsNullOrEmpty(userIdentifier))
  89. {
  90. Debug.LogError("user identifier is null or empty!");
  91. return;
  92. }
  93. try
  94. {
  95. crasheyeSetUserid(userIdentifier);
  96. }
  97. catch (Exception ex)
  98. {
  99. Debug.LogError(ex.Message);
  100. }
  101. }
  102. private static int m_lines = -1;
  103. /// <summary> 指定获取应用程序log日志的行数
  104. /// </summary>
  105. /// <param name="lines">设置获取行号</param>
  106. public static void SetLogging(int lines)
  107. {
  108. if (lines < 0)
  109. {
  110. return;
  111. }
  112. if (lines > MAX_LOG_COUNT)
  113. {
  114. m_lines = MAX_LOG_COUNT;
  115. }
  116. else
  117. {
  118. m_lines = lines;
  119. }
  120. }
  121. /// <summary>
  122. /// 返回设置log行数
  123. /// </summary>
  124. /// <returns></returns>
  125. public static int GetLoggingLines()
  126. {
  127. return m_lines;
  128. }
  129. private static string m_filter = "";
  130. /// <summary> 获取应用程序log日志关键字过滤
  131. /// </summary>
  132. /// <param name="filter">设置获取关键字</param>
  133. public static void SetLogging(string filter)
  134. {
  135. if (string.IsNullOrEmpty(filter))
  136. {
  137. return;
  138. }
  139. m_filter = filter;
  140. }
  141. /// <summary>
  142. /// 返回log过虑
  143. /// </summary>
  144. /// <returns></returns>
  145. public static string GetLoggingFilter()
  146. {
  147. return m_filter;
  148. }
  149. /// <summary> 获取应用程序log日志(过滤条件:关键字过滤+行数)
  150. /// </summary>
  151. /// <param name="lines">设置获取行数</param>
  152. /// <param name="filter">设置获取关键字</param>
  153. public static void SetLogging(int lines, string filter)
  154. {
  155. if (lines < 0 || string.IsNullOrEmpty(filter))
  156. {
  157. return;
  158. }
  159. SetLogging(lines);
  160. SetLogging(filter);
  161. }
  162. public static void SetFlushOnlyOverWiFi(bool enabled)
  163. {
  164. if(enabled)
  165. crasheyeSetFlushOnlyOverWiFi (1);
  166. else
  167. crasheyeSetFlushOnlyOverWiFi (0);
  168. }
  169. public static void SetBeta(bool enabled)
  170. {
  171. if(enabled)
  172. crasheyeSetBeta (1);
  173. else
  174. crasheyeSetBeta (0);
  175. }
  176. /// <summary>
  177. /// 添加自定义数据
  178. /// </summary>
  179. /// <param name="key">Key.</param>
  180. /// <param name="value">Value.</param>
  181. public static void AddExtraData(string key, string value)
  182. {
  183. if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
  184. {
  185. Debug.LogError("add extra data err: key or value IsNullOrEmpty!");
  186. return;
  187. }
  188. crasheyeAddExtraData(key, value);
  189. }
  190. /// <summary>
  191. /// 获取自定义数据
  192. /// </summary>
  193. /// <param name="key">Key</param>
  194. public static string GetExtraData(string key)
  195. {
  196. // 暂时未实现
  197. return "";
  198. }
  199. /// <summary>
  200. /// 移除自定义数据
  201. /// </summary>
  202. /// <param name="key">Key.</param>
  203. public static void RemoveExtraData(string key)
  204. {
  205. if (string.IsNullOrEmpty(key))
  206. {
  207. return;
  208. }
  209. crasheyeRemoveExtraData(key);
  210. }
  211. /// <summary>
  212. /// 添除数据
  213. /// </summary>
  214. public static void ClearExtraData()
  215. {
  216. crasheyeClearExtraData();
  217. }
  218. /// <summary>
  219. /// 打点数据
  220. /// </summary>
  221. /// <param name="breadcrumb">Breadcrumb.</param>
  222. public static void LeaveBreadcrumb(string breadcrumb)
  223. {
  224. if (string.IsNullOrEmpty(breadcrumb))
  225. {
  226. return;
  227. }
  228. crasheyeLeaveBreadcrumb(breadcrumb);
  229. }
  230. /// <summary> 获取SDK版本信息
  231. /// </summary>
  232. /// <returns>返回sdk版本号</returns>
  233. public static string GetAppVersion()
  234. {
  235. // 此功能暂未实现
  236. string sdkVersion = "NA";
  237. return sdkVersion;
  238. }
  239. public static void SaveLogger(string logMsg)
  240. {
  241. if (CrasheyeForIOS.GetLoggingLines() <= 0)
  242. {
  243. return;
  244. }
  245. if (logMsg.IndexOf(CrasheyeForIOS.GetLoggingFilter()) < 0)
  246. {
  247. return;
  248. }
  249. if (m_saveLog.Count >= CrasheyeForIOS.GetLoggingLines() && m_saveLog.Count > 0)
  250. {
  251. m_saveLog.Dequeue();
  252. }
  253. m_saveLog.Enqueue(logMsg);
  254. }
  255. public static string GetLogger()
  256. {
  257. string allLogger = "";
  258. foreach (string strTemp in m_saveLog)
  259. {
  260. allLogger += strTemp + "\n";
  261. }
  262. return allLogger;
  263. }
  264. public static void addLog(string log)
  265. {
  266. crasheyeAddLog(log);
  267. }
  268. public static void removeLog()
  269. {
  270. crasheyeRemoveLog();
  271. }
  272. /// <summary> SetURL
  273. /// </summary>
  274. /// <param name="SetURL"></param>
  275. public static void SetURL(string url)
  276. {
  277. if (String.IsNullOrEmpty(url))
  278. {
  279. Debug.LogError("set url is null or empty!");
  280. return;
  281. }
  282. crasheyeSetURL(url);
  283. }
  284. #endif
  285. }