unity3d 获取logcat android

废话不多先上代码
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)
                        {

                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
        }
#elif UNITY_IPHONE
#endif
        return logCatOrFilePath;
    }

猜你喜欢

转载自blog.csdn.net/qqo_aa/article/details/79046231