Skip to content

Commit ebf9d00

Browse files
committed
[增加]1. 增加日志打印的宏定义处理
1 parent a40a919 commit ebf9d00

3 files changed

Lines changed: 109 additions & 6 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//------------------------------------------------------------
2+
// Game Framework
3+
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
4+
// Homepage: https://gameframework.cn/
5+
// Feedback: mailto:ellan@gameframework.cn
6+
//------------------------------------------------------------
7+
8+
using GameFrameX.Editor;
9+
using UnityEditor;
10+
11+
namespace GameFrameX.Web.Editor
12+
{
13+
/// <summary>
14+
/// 网络日志脚本宏定义。
15+
/// </summary>
16+
public static class WebLogScriptingDefineSymbols
17+
{
18+
public const string EnableNetworkReceiveLogScriptingDefineSymbol = "ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG";
19+
public const string EnableNetworkSendLogScriptingDefineSymbol = "ENABLE_GAMEFRAMEX_WEB_SEND_LOG";
20+
21+
/// <summary>
22+
/// 禁用Web接收日志脚本宏定义。
23+
/// </summary>
24+
[MenuItem("GameFrameX/Scripting Define Symbols/Disable Web Receive Logs(关闭Web接收日志打印)", false, 460)]
25+
public static void DisableNetworkReceiveLogs()
26+
{
27+
ScriptingDefineSymbols.RemoveScriptingDefineSymbol(EnableNetworkReceiveLogScriptingDefineSymbol);
28+
}
29+
30+
/// <summary>
31+
/// 开启Web接收日志脚本宏定义。
32+
/// </summary>
33+
[MenuItem("GameFrameX/Scripting Define Symbols/Enable Web Receive Logs(开启Web接收日志打印)", false, 461)]
34+
public static void EnableNetworkReceiveLogs()
35+
{
36+
ScriptingDefineSymbols.AddScriptingDefineSymbol(EnableNetworkReceiveLogScriptingDefineSymbol);
37+
}
38+
39+
/// <summary>
40+
/// 禁用Web发送日志脚本宏定义。
41+
/// </summary>
42+
[MenuItem("GameFrameX/Scripting Define Symbols/Disable Web Send Logs(关闭Web发送日志打印)", false, 450)]
43+
public static void DisableNetworkSendLogs()
44+
{
45+
ScriptingDefineSymbols.RemoveScriptingDefineSymbol(EnableNetworkSendLogScriptingDefineSymbol);
46+
}
47+
48+
/// <summary>
49+
/// 开启Web发送日志脚本宏定义。
50+
/// </summary>
51+
[MenuItem("GameFrameX/Scripting Define Symbols/Enable Web Send Logs(开启Web发送日志打印)", false, 451)]
52+
public static void EnableNetworkSendLogs()
53+
{
54+
ScriptingDefineSymbols.AddScriptingDefineSymbol(EnableNetworkSendLogScriptingDefineSymbol);
55+
}
56+
}
57+
}

Editor/WebLogScriptingDefineSymbols.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Web/WebManager.cs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ public partial class WebManager : GameFrameworkModule, IWebManager
2121
{
2222
// 用于构建URL的StringBuilder
2323
private readonly StringBuilder m_StringBuilder = new StringBuilder(256);
24+
2425
// 等待处理的普通请求队列
2526
private readonly Queue<WebJsonData> m_WaitingNormalQueue = new Queue<WebJsonData>(256);
27+
2628
// 正在处理的普通请求列表
2729
private readonly List<WebJsonData> m_SendingNormalList = new List<WebJsonData>(16);
2830

@@ -31,6 +33,7 @@ public partial class WebManager : GameFrameworkModule, IWebManager
3133

3234
// JSON内容类型常量
3335
private const string JsonContentType = "application/json; charset=utf-8";
36+
3437
// 超时时间(秒)
3538
private float m_Timeout = 5f;
3639

@@ -198,6 +201,10 @@ public Task<WebStringResult> GetToString(string url, Dictionary<string, string>
198201
/// </summary>
199202
private async void MakeJsonStringRequest(WebJsonData webJsonData)
200203
{
204+
#if ENABLE_GAMEFRAMEX_WEB_SEND_LOG
205+
Log.Debug($"Web Request: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)}");
206+
#endif
207+
201208
#if UNITY_WEBGL
202209
UnityWebRequest unityWebRequest;
203210
if (webJsonData.IsGet)
@@ -232,10 +239,15 @@ private async void MakeJsonStringRequest(WebJsonData webJsonData)
232239
m_SendingNormalList.Remove(webJsonData);
233240
if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError || unityWebRequest.error != null)
234241
{
242+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
243+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {unityWebRequest.error}");
244+
#endif
235245
webJsonData.UniTaskCompletionStringSource.TrySetException(new Exception(unityWebRequest.error));
236246
return;
237247
}
238-
248+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
249+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {unityWebRequest.downloadHandler.text}");
250+
#endif
239251
webJsonData.UniTaskCompletionStringSource.SetResult(new WebStringResult(webJsonData.UserData, unityWebRequest.downloadHandler.text));
240252
};
241253
#else
@@ -269,6 +281,9 @@ private async void MakeJsonStringRequest(WebJsonData webJsonData)
269281
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
270282
{
271283
string content = await reader.ReadToEndAsync();
284+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
285+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {content}");
286+
#endif
272287
webJsonData.UniTaskCompletionStringSource.SetResult(new WebStringResult(webJsonData.UserData, content));
273288
}
274289
}
@@ -281,15 +296,23 @@ private async void MakeJsonStringRequest(WebJsonData webJsonData)
281296
webJsonData.UniTaskCompletionStringSource.SetException(new TimeoutException(e.Message));
282297
return;
283298
}
284-
299+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
300+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {e.Message}");
301+
#endif
285302
webJsonData.UniTaskCompletionStringSource.SetException(e);
286303
}
287304
catch (IOException e)
288305
{
306+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
307+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {e.Message}");
308+
#endif
289309
webJsonData.UniTaskCompletionStringSource.SetException(e);
290310
}
291311
catch (Exception e)
292312
{
313+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
314+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {e.Message}");
315+
#endif
293316
webJsonData.UniTaskCompletionStringSource.SetException(e);
294317
}
295318
finally
@@ -304,6 +327,10 @@ private async void MakeJsonStringRequest(WebJsonData webJsonData)
304327
/// </summary>
305328
private async void MakeJsonBytesRequest(WebJsonData webJsonData)
306329
{
330+
#if ENABLE_GAMEFRAMEX_WEB_SEND_LOG
331+
Log.Debug($"Web Request: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)}");
332+
#endif
333+
307334
#if UNITY_WEBGL
308335
UnityWebRequest unityWebRequest;
309336
if (webJsonData.IsGet)
@@ -338,10 +365,15 @@ private async void MakeJsonBytesRequest(WebJsonData webJsonData)
338365
m_SendingNormalList.Remove(webJsonData);
339366
if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError || unityWebRequest.error != null)
340367
{
368+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
369+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {unityWebRequest.error}");
370+
#endif
341371
webJsonData.UniTaskCompletionBytesSource.TrySetException(new Exception(unityWebRequest.error));
342372
return;
343373
}
344-
374+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
375+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {unityWebRequest.downloadHandler.data}");
376+
#endif
345377
webJsonData.UniTaskCompletionBytesSource.SetResult(new WebBufferResult(webJsonData.UserData, unityWebRequest.downloadHandler.data));
346378
};
347379
#else
@@ -377,12 +409,19 @@ private async void MakeJsonBytesRequest(WebJsonData webJsonData)
377409
m_MemoryStream.SetLength(responseStream.Length);
378410
m_MemoryStream.Position = 0;
379411
await responseStream.CopyToAsync(m_MemoryStream);
380-
webJsonData.UniTaskCompletionBytesSource.SetResult(new WebBufferResult(webJsonData.UserData, m_MemoryStream.ToArray())); // 将流的内容复制到内存流中并转换为byte数组
412+
var resultData = m_MemoryStream.ToArray();
413+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
414+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {resultData}");
415+
#endif
416+
webJsonData.UniTaskCompletionBytesSource.SetResult(new WebBufferResult(webJsonData.UserData, resultData)); // 将流的内容复制到内存流中并转换为byte数组
381417
}
382418
}
383419
}
384420
catch (WebException e)
385421
{
422+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
423+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {e.Message}");
424+
#endif
386425
// 捕获超时异常
387426
if (e.Status == WebExceptionStatus.Timeout)
388427
{
@@ -394,10 +433,16 @@ private async void MakeJsonBytesRequest(WebJsonData webJsonData)
394433
}
395434
catch (IOException e)
396435
{
436+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
437+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {e.Message}");
438+
#endif
397439
webJsonData.UniTaskCompletionBytesSource.SetException(e);
398440
}
399441
catch (Exception e)
400442
{
443+
#if ENABLE_GAMEFRAMEX_WEB_RECEIVE_LOG
444+
Log.Debug($"Web Response: {webJsonData.URL} \n Header: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Header)} \n Form: {GameFrameX.Runtime.Utility.Json.ToJson(webJsonData.Form)} \n Content: {e.Message}");
445+
#endif
401446
webJsonData.UniTaskCompletionBytesSource.SetException(e);
402447
}
403448
finally
@@ -426,7 +471,6 @@ public Task<WebBufferResult> GetToBytes(string url, Dictionary<string, string> q
426471
return uniTaskCompletionSource.Task;
427472
}
428473

429-
430474
/// <summary>
431475
/// 发送Post 请求
432476
/// </summary>
@@ -481,7 +525,6 @@ public Task<WebBufferResult> PostToBytes(string url, Dictionary<string, object>
481525
return PostToBytes(url, from, queryString, null, userData);
482526
}
483527

484-
485528
/// <summary>
486529
/// 发送Post 请求
487530
/// </summary>

0 commit comments

Comments
 (0)