unity3d 获取logcat android
2018-01-12 17:10
246 查看
废话不多先上代码
java
C#
java
public static String GetSDPath() { String sdPath = ""; try { File sdDir = null; boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);// 判断sd卡是否存在 if (sdCardExist) { sdDir = Environment.getExternalStorageDirectory();// 获取跟目录 } sdPath = sdDir.toString(); } catch (Exception e) { } return sdPath; } public static String GetLogcat(boolean saveToFile, String fileName) { String strLog = ""; try { Process process = Runtime.getRuntime().exec("logcat -d"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder log = new StringBuilder(); String line = ""; while ((line = bufferedReader.readLine()) != null) { log.append(line + "\n"); } strLog = log.toString(); if (saveToFile) { if (fileName == null || fileName.isEmpty()) { fileName = "Logcat.txt"; } FileOutputStream fos = null; try { String filePath = GetSDPath() + File.separator + fileName; fos = new FileOutputStream(filePath); byte[] byLog = strLog.getBytes(); fos.write(byLog, 0, byLog.length); } catch (Exception e) { } finally { try { if (null != fos) { fos.close(); } } catch (Exception e) { } } } } catch (Exception ex) { } return strLog; }
C#
public static void MakeDir(string directory) { try { string s = Path.GetDirectoryName(directory); if (Directory.Exists(s)) { return; } Directory.CreateDirectory(s); } catch (System.Exception ex) { } } public static void SaveLog(string filePath, string data) { try { string path = Path.GetDirectoryName(filePath); if (false == Directory.Exists(path)) { Directory.CreateDirectory(path); } FileStream file = new FileStream(filePath, FileMode.Append); StreamWriter sw = new StreamWriter(file); sw.WriteLine(data); sw.Flush(); sw.Close(); file.Close(); } catch (System.Exception e) { } } // 如果saveToFile为true,则返回保存logcat文件的全路径 // 否则返回logcat字符串 public static string l(bool saveToFile) { string logCatOrFilePath = string.Empty; #if UNITY_EDITOR #elif UNITY_ANDROID try { AndroidJavaClass androidUtile = new AndroidJavaClass("你的java类带包名的全名"); if (androidUtile != null) { string logCat = androidUtile.CallStatic<string>("GetLogcat", saveToFile, ""); if (!string.IsNullOrEmpty(logCat)) { if (!saveToFile) { logCatOrFilePath = logCat; } else { try { string logDir = ResPathMgr.m_LocalPersistentPath + "log/"; MakeDir(logDir); string nowDate = System.DateTime.Now.ToString(); nowDate = nowDate.Replace(":", ""); nowDate = nowDate.Replace(" ", ""); nowDate = nowDate.Replace("/", ""); nowDate = nowDate.Replace("\\", ""); string fileName = "Logcat.txt"; logCatOrFilePath = logDir + fileName; SaveLog(logCatOrFilePath, nowDate + "\n" + logCat); } catch (System.Exception e1) { } } } } 4000 } catch (Exception e) { } #elif UNITY_IPHONE #endif return logCatOrFilePath; }
相关文章推荐
- android adb logcat获取日志文件
- android studio logcat 被拖出monitor如何还原
- Android -- junit测试框架,logcat获取log信息
- android studio logcat看不到日志
- Android调试之Logcat
- android/mydroid/system/core/logcat/logcat.cpp
- android studio logcat一闪而过
- Android Logcat使用技巧
- Android 让adb logcat打印内核调试信息
- android studio logcat 中文乱码解决
- android-logging-log4j - Logging with Log4J in Android | providing LogCat appender, configuration facade and slf4j support - Google Project Hosting
- 【Android】Android调试获取Log
- android adb logcat打印内核调试信息
- Android -- junit测试框架,logcat获取log信息
- Eclipse for android logcat无输出
- Android:调试之LogCat
- 如何获取android程序LOG
- Android开发--开发调试Root权限的获取以及用adb 抓取log
- 我的Android进阶之旅------>Android使用cmd窗口进行adb logcat时出现中文乱码问题的解决办法
- 解读Android LOG机制的实现:(5)获取LOG的应用程序LogCat