add separate LogNetworkTraffic option

This commit is contained in:
Jesse Plamondon-Willard 2019-05-29 21:26:57 -04:00
parent d4e09c5a85
commit bf3738eacb
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
6 changed files with 25 additions and 9 deletions

View File

@ -25,6 +25,7 @@ These changes have not been released yet.
* Added `IContentPack.HasFile` method. * Added `IContentPack.HasFile` method.
* Added `Context.IsGameLaunched` field. * Added `Context.IsGameLaunched` field.
* Mods are now loaded much earlier in the game launch. This lets mods intercept any content asset, but the game is not fully initialised when `Entry` is called (use the `GameLaunched` event if you need to run code when the game is initialised). * Mods are now loaded much earlier in the game launch. This lets mods intercept any content asset, but the game is not fully initialised when `Entry` is called (use the `GameLaunched` event if you need to run code when the game is initialised).
* Added separate `LogNetworkTraffic` option to make verbose logging less overwhelmingly verbose.
* When a mod is incompatible, the trace logs now list all detected issues instead of the first one. * When a mod is incompatible, the trace logs now list all detected issues instead of the first one.
* Removed all deprecated APIs. * Removed all deprecated APIs.
* Removed the `Monitor.ExitGameImmediately` method. * Removed the `Monitor.ExitGameImmediately` method.

View File

@ -34,6 +34,9 @@ namespace StardewModdingAPI.Framework.Models
/// <summary>Whether SMAPI should log more information about the game context.</summary> /// <summary>Whether SMAPI should log more information about the game context.</summary>
public bool VerboseLogging { get; set; } public bool VerboseLogging { get; set; }
/// <summary>Whether SMAPI should log network traffic. Best combined with <see cref="VerboseLogging"/>, which includes network metadata.</summary>
public bool LogNetworkTraffic { get; set; }
/// <summary>Whether to generate a file in the mods folder with detailed metadata about the detected mods.</summary> /// <summary>Whether to generate a file in the mods folder with detailed metadata about the detected mods.</summary>
public bool DumpMetadata { get; set; } public bool DumpMetadata { get; set; }

View File

@ -223,7 +223,8 @@ namespace StardewModdingAPI.Framework
deprecationManager: SCore.DeprecationManager, deprecationManager: SCore.DeprecationManager,
onGameInitialised: this.InitialiseAfterGameStart, onGameInitialised: this.InitialiseAfterGameStart,
onGameExiting: this.Dispose, onGameExiting: this.Dispose,
cancellationToken: this.CancellationToken cancellationToken: this.CancellationToken,
logNetworkTraffic: this.Settings.LogNetworkTraffic
); );
StardewValley.Program.gamePtr = this.GameInstance; StardewValley.Program.gamePtr = this.GameInstance;

View File

@ -141,7 +141,8 @@ namespace StardewModdingAPI.Framework
/// <param name="onGameInitialised">A callback to invoke after the game finishes initialising.</param> /// <param name="onGameInitialised">A callback to invoke after the game finishes initialising.</param>
/// <param name="onGameExiting">A callback to invoke when the game exits.</param> /// <param name="onGameExiting">A callback to invoke when the game exits.</param>
/// <param name="cancellationToken">Propagates notification that SMAPI should exit.</param> /// <param name="cancellationToken">Propagates notification that SMAPI should exit.</param>
internal SGame(Monitor monitor, IMonitor monitorForGame, Reflector reflection, EventManager eventManager, JsonHelper jsonHelper, ModRegistry modRegistry, DeprecationManager deprecationManager, Action onGameInitialised, Action onGameExiting, CancellationTokenSource cancellationToken) /// <param name="logNetworkTraffic">Whether to log network traffic.</param>
internal SGame(Monitor monitor, IMonitor monitorForGame, Reflector reflection, EventManager eventManager, JsonHelper jsonHelper, ModRegistry modRegistry, DeprecationManager deprecationManager, Action onGameInitialised, Action onGameExiting, CancellationTokenSource cancellationToken, bool logNetworkTraffic)
{ {
this.OnLoadingFirstAsset = SGame.ConstructorHack.OnLoadingFirstAsset; this.OnLoadingFirstAsset = SGame.ConstructorHack.OnLoadingFirstAsset;
SGame.ConstructorHack = null; SGame.ConstructorHack = null;
@ -163,7 +164,7 @@ namespace StardewModdingAPI.Framework
this.OnGameInitialised = onGameInitialised; this.OnGameInitialised = onGameInitialised;
this.OnGameExiting = onGameExiting; this.OnGameExiting = onGameExiting;
Game1.input = new SInputState(); Game1.input = new SInputState();
Game1.multiplayer = new SMultiplayer(monitor, eventManager, jsonHelper, modRegistry, reflection, this.OnModMessageReceived); Game1.multiplayer = new SMultiplayer(monitor, eventManager, jsonHelper, modRegistry, reflection, this.OnModMessageReceived, logNetworkTraffic);
Game1.hooks = new SModHooks(this.OnNewDayAfterFade); Game1.hooks = new SModHooks(this.OnNewDayAfterFade);
this.CancellationToken = cancellationToken; this.CancellationToken = cancellationToken;

View File

@ -51,6 +51,9 @@ namespace StardewModdingAPI.Framework
/// <summary>A callback to invoke when a mod message is received.</summary> /// <summary>A callback to invoke when a mod message is received.</summary>
private readonly Action<ModMessageModel> OnModMessageReceived; private readonly Action<ModMessageModel> OnModMessageReceived;
/// <summary>Whether to log network traffic.</summary>
private readonly bool LogNetworkTraffic;
/********* /*********
** Accessors ** Accessors
@ -72,7 +75,8 @@ namespace StardewModdingAPI.Framework
/// <param name="modRegistry">Tracks the installed mods.</param> /// <param name="modRegistry">Tracks the installed mods.</param>
/// <param name="reflection">Simplifies access to private code.</param> /// <param name="reflection">Simplifies access to private code.</param>
/// <param name="onModMessageReceived">A callback to invoke when a mod message is received.</param> /// <param name="onModMessageReceived">A callback to invoke when a mod message is received.</param>
public SMultiplayer(IMonitor monitor, EventManager eventManager, JsonHelper jsonHelper, ModRegistry modRegistry, Reflector reflection, Action<ModMessageModel> onModMessageReceived) /// <param name="logNetworkTraffic">Whether to log network traffic.</param>
public SMultiplayer(IMonitor monitor, EventManager eventManager, JsonHelper jsonHelper, ModRegistry modRegistry, Reflector reflection, Action<ModMessageModel> onModMessageReceived, bool logNetworkTraffic)
{ {
this.Monitor = monitor; this.Monitor = monitor;
this.EventManager = eventManager; this.EventManager = eventManager;
@ -80,6 +84,7 @@ namespace StardewModdingAPI.Framework
this.ModRegistry = modRegistry; this.ModRegistry = modRegistry;
this.Reflection = reflection; this.Reflection = reflection;
this.OnModMessageReceived = onModMessageReceived; this.OnModMessageReceived = onModMessageReceived;
this.LogNetworkTraffic = logNetworkTraffic;
} }
/// <summary>Perform cleanup needed when a multiplayer session ends.</summary> /// <summary>Perform cleanup needed when a multiplayer session ends.</summary>
@ -143,7 +148,7 @@ namespace StardewModdingAPI.Framework
/// <param name="resume">Resume sending the underlying message.</param> /// <param name="resume">Resume sending the underlying message.</param>
protected void OnClientSendingMessage(OutgoingMessage message, Action<OutgoingMessage> sendMessage, Action resume) protected void OnClientSendingMessage(OutgoingMessage message, Action<OutgoingMessage> sendMessage, Action resume)
{ {
if (this.Monitor.IsVerbose) if (this.LogNetworkTraffic)
this.Monitor.Log($"CLIENT SEND {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace); this.Monitor.Log($"CLIENT SEND {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace);
switch (message.MessageType) switch (message.MessageType)
@ -167,7 +172,7 @@ namespace StardewModdingAPI.Framework
/// <param name="resume">Process the message using the game's default logic.</param> /// <param name="resume">Process the message using the game's default logic.</param>
public void OnServerProcessingMessage(IncomingMessage message, Action<OutgoingMessage> sendMessage, Action resume) public void OnServerProcessingMessage(IncomingMessage message, Action<OutgoingMessage> sendMessage, Action resume)
{ {
if (this.Monitor.IsVerbose) if (this.LogNetworkTraffic)
this.Monitor.Log($"SERVER RECV {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace); this.Monitor.Log($"SERVER RECV {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace);
switch (message.MessageType) switch (message.MessageType)
@ -247,7 +252,7 @@ namespace StardewModdingAPI.Framework
/// <returns>Returns whether the message was handled.</returns> /// <returns>Returns whether the message was handled.</returns>
public void OnClientProcessingMessage(IncomingMessage message, Action<OutgoingMessage> sendMessage, Action resume) public void OnClientProcessingMessage(IncomingMessage message, Action<OutgoingMessage> sendMessage, Action resume)
{ {
if (this.Monitor.IsVerbose) if (this.LogNetworkTraffic)
this.Monitor.Log($"CLIENT RECV {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace); this.Monitor.Log($"CLIENT RECV {(MessageType)message.MessageType} {message.FarmerID}", LogLevel.Trace);
switch (message.MessageType) switch (message.MessageType)
@ -371,7 +376,7 @@ namespace StardewModdingAPI.Framework
string data = JsonConvert.SerializeObject(model, Formatting.None); string data = JsonConvert.SerializeObject(model, Formatting.None);
// log message // log message
if (this.Monitor.IsVerbose) if (this.LogNetworkTraffic)
this.Monitor.Log($"Broadcasting '{messageType}' message: {data}.", LogLevel.Trace); this.Monitor.Log($"Broadcasting '{messageType}' message: {data}.", LogLevel.Trace);
// send message // send message
@ -432,7 +437,7 @@ namespace StardewModdingAPI.Framework
string json = message.Reader.ReadString(); string json = message.Reader.ReadString();
ModMessageModel model = this.JsonHelper.Deserialise<ModMessageModel>(json); ModMessageModel model = this.JsonHelper.Deserialise<ModMessageModel>(json);
HashSet<long> playerIDs = new HashSet<long>(model.ToPlayerIDs ?? this.GetKnownPlayerIDs()); HashSet<long> playerIDs = new HashSet<long>(model.ToPlayerIDs ?? this.GetKnownPlayerIDs());
if (this.Monitor.IsVerbose) if (this.LogNetworkTraffic)
this.Monitor.Log($"Received message: {json}.", LogLevel.Trace); this.Monitor.Log($"Received message: {json}.", LogLevel.Trace);
// notify local mods // notify local mods

View File

@ -61,6 +61,11 @@ This file contains advanced configuration for SMAPI. You generally shouldn't cha
*/ */
"VerboseLogging": false, "VerboseLogging": false,
/**
* Whether SMAPI should log network traffic (may be very verbose). Best combined with VerboseLogging, which includes network metadata.
*/
"LogNetworkTraffic": false,
/** /**
* Whether to generate a 'SMAPI-latest.metadata-dump.json' file in the logs folder with the full mod * Whether to generate a 'SMAPI-latest.metadata-dump.json' file in the logs folder with the full mod
* metadata for detected mods. This is only needed when troubleshooting some cases. * metadata for detected mods. This is only needed when troubleshooting some cases.