public bool EnqueueInFile()
        {
#if UNITY_STANDALONE || UNITY_EDITOR
            string firstFile = ToastFileManager.GetFirstFile(ToastInstanceLoggerCommonLogic.AppKey);

            if (!string.IsNullOrEmpty(firstFile))
            {
                LogBulkData bulkLog = new LogBulkData();

                string fileName = firstFile.Substring(firstFile.LastIndexOf("\\", StringComparison.OrdinalIgnoreCase) + 1);
                if (firstFile.LastIndexOf("\\", StringComparison.OrdinalIgnoreCase) == -1)
                {
                    fileName = firstFile.Substring(firstFile.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1);
                }
                string[] subString = fileName.Split('_');
                bulkLog.CreateTime    = long.Parse(subString[0]);
                bulkLog.TransactionId = subString[1];

                string strLogContents = ToastFileManager.FileLoad(ToastInstanceLoggerCommonLogic.AppKey, bulkLog.CreateTime, bulkLog.TransactionId);
                bulkLog.LogContents = strLogContents;
                _queueBulkLog.Enqueue(bulkLog);

                return(true);
            }
#endif

            return(false);
        }
Beispiel #2
0
        public IEnumerator SetToastLoggerSettingsByJson(string url)
        {
            string errorString = "";
            string jsonString  = "";

            float timeout   = 5.0f;
            bool  isTimeout = false;

#if UNITY_2017_2_OR_NEWER
            using (var request = UnityWebRequest.Get(url))
            {
                request.timeout = System.Convert.ToInt32(timeout);

                yield return(request.SendWebRequest());

                errorString = request.error;
                if (request.isNetworkError || request.isHttpError)
                {
                    isTimeout = true;
                }
                else
                {
                    jsonString = request.downloadHandler.text;
                }
            }
#else
            float timer = 0f;

            using (WWW www = new WWW(url))
            {
                do
                {
                    if (timer > timeout)
                    {
                        isTimeout = true;
                        break;
                    }
                    timer += Time.deltaTime;

                    yield return(null);
                }while (!www.isDone);

                if (isTimeout)
                {
                    www.Dispose();
                }
                else
                {
                    errorString = www.error;
                    jsonString  = www.text;
                }
            }
#endif  // UNITY_2017_2_OR_NEWER

            if (isTimeout == false && string.IsNullOrEmpty(errorString)) // success
            {
#if UNITY_STANDALONE || UNITY_EDITOR
                ToastFileManager.SettingFileSave(ToastLoggerCommonLogic.AppKey, jsonString);
#endif  // UNITY_STANDALONE || UNITY_EDITOR

                if (string.IsNullOrEmpty(jsonString) == false)
                {
                    LoadSettingsInfoByJson(jsonString);
                }
            }
            else
            {
#if UNITY_STANDALONE || UNITY_EDITOR
                // Get information from a file
                jsonString = ToastFileManager.SettingFileLoad(ToastLoggerCommonLogic.AppKey);

                if (string.IsNullOrEmpty(jsonString) == false)
                {
                    LoadSettingsInfoByJson(jsonString);
                }
#endif  // UNITY_STANDALONE || UNITY_EDITOR
            }
        }
        IEnumerator SendReport(ToastLoggerBulkLog bulkLog, string logContents, long createTime, string transactionId)
        {
            _couroutineCount++;

            string url = ToastLoggerCommonLogic.CollectorUrl;

            string errorString = "";
            string jsonString  = "";

            float timeout   = 5.0f;
            bool  isTimeout = false;

#if UNITY_2017_2_OR_NEWER
            var downloadHandler = new DownloadHandlerBuffer();
            var uploadHandler   = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(logContents));

            using (var request = new UnityWebRequest(url,
                                                     UnityWebRequest.kHttpVerbPOST,
                                                     downloadHandler, uploadHandler))
            {
                request.SetRequestHeader("Content-Type", "application/json");
                request.timeout = System.Convert.ToInt32(timeout);

                yield return(request.SendWebRequest());

                errorString = request.error;
                if (request.isNetworkError || request.isHttpError)
                {
                    isTimeout = true;
                }
                else
                {
                    jsonString = request.downloadHandler.text;
                }
            }
#else
            float timer = 0f;

            Dictionary <string, string> header = new Dictionary <string, string>();
            header.Add("Content-Type", "application/json");
            using (WWW www = new WWW(url, System.Text.Encoding.UTF8.GetBytes(logContents), header))
            {
                do
                {
                    if (timer > timeout)
                    {
                        isTimeout = true;
                        break;
                    }
                    timer += Time.deltaTime;

                    yield return(null);
                }while (!www.isDone);

                if (isTimeout)
                {
                    www.Dispose();
                }
                else
                {
                    errorString = www.error;
                    jsonString  = www.text;
                }
            }
#endif  // UNITY_2017_2_OR_NEWER

#if UNITY_STANDALONE || UNITY_EDITOR
            if (isTimeout == false && string.IsNullOrEmpty(errorString)) // success
            {
                if (ToastFileManager.FileCheck(ToastLoggerCommonLogic.AppKey, createTime, transactionId))
                {
                    ToastFileManager.FileDelete(ToastLoggerCommonLogic.AppKey, createTime, transactionId);
                }
                ToastLoggerSendQueue.Instance.EnqueueInFile();

                if (ToastLoggerCommonLogic.IsLoggerListener)
                {
                    foreach (ToastLoggerLogObject logData in bulkLog.Gets())
                    {
                        CrashLoggerListenerReceiver.Instance.OnLogSuccess(logData);
                    }
                }
            }
            else
            {
                if (ToastFileManager.GetProjectFileCount(ToastLoggerCommonLogic.AppKey) < MAX_FILE_SIZE)
                {
                    ToastFileManager.FileSave(ToastLoggerCommonLogic.AppKey, createTime, transactionId, logContents);

                    if (ToastLoggerCommonLogic.IsLoggerListener)
                    {
                        foreach (ToastLoggerLogObject logData in bulkLog.Gets())
                        {
                            CrashLoggerListenerReceiver.Instance.OnLogSave(logData);
                        }
                    }
                }
            }
#elif UNITY_WEBGL
            if (isTimeout == false && string.IsNullOrEmpty(errorString)) // success
            {
            }
            else
            {
                if (ToastLoggerCommonLogic.IsLoggerListener)
                {
                    foreach (ToastLoggerLogObject logData in bulkLog.Gets())
                    {
                        CrashLoggerListenerReceiver.Instance.OnLogError(logData, errorString);
                    }
                }
            }
#endif

            _couroutineCount--;

            yield return(null);
        }