/// <summary>Tracks and logs a download upon it completing.</summary> public static void DebugDownload(UnityWebRequestAsyncOperation operation, LocalUser userData, string downloadLocation, int timeSent = -1) { #if DEBUG Debug.Assert(operation != null); UnityWebRequest webRequest = operation.webRequest; string userIdString = DebugUtilities.GenerateUserIdString(userData.profile); if (timeSent < 0) { timeSent = ServerTimeStamp.Now; } if (PluginSettings.REQUEST_LOGGING.logOnSend) { var logString = new System.Text.StringBuilder(); logString.AppendLine("[mod.io] Web Request Sent"); logString.Append("URL: "); logString.Append(webRequest.url); logString.Append(" ("); logString.Append(webRequest.method.ToUpper()); logString.AppendLine(")"); if (!string.IsNullOrEmpty(downloadLocation)) { logString.Append("Download Location: "); logString.AppendLine(downloadLocation); } if (timeSent >= 0) { logString.Append("Sent: "); logString.Append(ServerTimeStamp.ToLocalDateTime(timeSent).ToString()); logString.Append(" ["); logString.Append(timeSent.ToString()); logString.AppendLine("]"); } logString.AppendLine(); string requestString = DebugUtilities.GetRequestInfo(webRequest, userIdString); logString.AppendLine("------[ Request ]------"); logString.AppendLine(requestString); Debug.Log(logString.ToString()); } if (PluginSettings.REQUEST_LOGGING.logAllResponses || PluginSettings.REQUEST_LOGGING.errorsAsWarnings) { RequestDebugData debugData = new RequestDebugData() { userIdString = userIdString, timeSent = timeSent, downloadLocation = downloadLocation, }; DebugUtilities.webRequestDebugData.Add(webRequest, debugData); // handle completion if (operation.isDone) { DebugUtilities.OnOperationCompleted(operation); } else { operation.completed += DebugUtilities.OnOperationCompleted; } } #endif // DEBUG }
/// <summary>Callback upon request operation completion.</summary> private static void OnOperationCompleted(AsyncOperation operation) { if (operation == null) { return; } // get vars UnityWebRequestAsyncOperation o = operation as UnityWebRequestAsyncOperation; UnityWebRequest webRequest = o.webRequest; var now = ServerTimeStamp.Now; bool isError = (webRequest.isNetworkError || webRequest.isHttpError); // should we log? if (PluginSettings.REQUEST_LOGGING.logAllResponses || isError) { RequestDebugData debugData; if (!DebugUtilities.webRequestDebugData.TryGetValue(webRequest, out debugData)) { debugData = new RequestDebugData() { userIdString = "NONE_RECORDED", timeSent = -1, downloadLocation = null, }; } // generate strings string requestString = DebugUtilities.GetRequestInfo(webRequest, debugData.userIdString); string responseString = DebugUtilities.GetResponseInfo(webRequest); // generate log string var logString = new System.Text.StringBuilder(); if (!isError) { logString.AppendLine("[mod.io] Web Request Succeeded"); } else { logString.AppendLine("[mod.io] Web Request Failed"); } logString.Append("URL: "); logString.Append(webRequest.url); logString.Append(" ("); logString.Append(webRequest.method.ToUpper()); logString.AppendLine(")"); if (!string.IsNullOrEmpty(debugData.downloadLocation)) { logString.Append("Download Location: "); logString.AppendLine(debugData.downloadLocation); } if (debugData.timeSent >= 0) { logString.Append("Sent: "); logString.Append(ServerTimeStamp.ToLocalDateTime(debugData.timeSent).ToString()); logString.Append(" ["); logString.Append(debugData.timeSent.ToString()); logString.AppendLine("]"); } logString.Append("Completed: "); logString.Append(ServerTimeStamp.ToLocalDateTime(now).ToString()); logString.Append(" ["); logString.Append(now.ToString()); logString.AppendLine("]"); logString.AppendLine(); logString.AppendLine("------[ Request ]------"); logString.AppendLine(requestString); logString.AppendLine("------[ Response ]------"); logString.AppendLine(responseString); // log if (isError && PluginSettings.REQUEST_LOGGING.errorsAsWarnings) { Debug.LogWarning(logString.ToString()); } else { Debug.Log(logString.ToString()); } } DebugUtilities.webRequestDebugData.Remove(webRequest); }