Skip to content

Commit

Permalink
Saner logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisias committed Sep 29, 2021
1 parent d0bcf9e commit a393b38
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 64 deletions.
1 change: 1 addition & 0 deletions Source/DistantObject/DistantObject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<DependentUpon>Version.tt</DependentUpon>
</Compile>
<Compile Include="Startup.cs" />
<Compile Include="Log.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Properties\Version.tt">
Expand Down
16 changes: 5 additions & 11 deletions Source/DistantObject/FlareDraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,17 +657,17 @@ private void Awake()
}
else
{
UnityEngine.Debug.LogWarning(Constants.DistantObject + " -- Unable to find situation '" + sit + "' in my known situations atlas");
Log.trace("Unable to find situation '{0}' in my known situations atlas", sit);
}
}

if (DistantObjectSettings.DistantFlare.flaresEnabled)
{
UnityEngine.Debug.Log(Constants.DistantObject + " -- FlareDraw enabled");
Log.trace("FlareDraw enabled");
}
else
{
UnityEngine.Debug.Log(Constants.DistantObject + " -- FlareDraw disabled");
Log.trace("FlareDraw disabled");
}

sunRadiusSquared = FlightGlobals.Bodies[0].Radius * FlightGlobals.Bodies[0].Radius;
Expand Down Expand Up @@ -749,10 +749,7 @@ private void RemoveVesselFlare(Vessel v)
// Update visible vessel list
public void FixedUpdate()
{
if (DistantObjectSettings.debugMode)
{
UnityEngine.Debug.Log(Constants.DistantObject + " -- FixedUpdate");
}
Log.dbg("FixedUpdate");

if (DistantObjectSettings.DistantFlare.flaresEnabled && !MapView.MapIsEnabled)
{
Expand Down Expand Up @@ -826,10 +823,7 @@ private void Update()
camFOV = FlightCamera.fetch.mainCamera.fieldOfView;
}

if (DistantObjectSettings.debugMode)
{
UnityEngine.Debug.Log(Constants.DistantObject + " -- Update");
}
Log.dbg("Update");

foreach (BodyFlare flare in bodyFlares)
{
Expand Down
76 changes: 76 additions & 0 deletions Source/DistantObject/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
This file is part of Distant Object Enhancement /L
© 2021 LisiasT
© 2019-2021 TheDarkBadger
© 2014-2019 MOARdV
© 2014 Rubber Ducky
THIS FILE is licensed to you under:
* WTFPL - http://www.wtfpl.net
* Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
THIS FILE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
using System;
using System.Diagnostics;
using D = UnityEngine.Debug;

namespace DistantObject
{
public static class Log
{
private static readonly string PREFIX = string.Format("[{0}] ", typeof(Log).Namespace);
public static void force (string msg, params object [] @params)
{
D.LogFormat(PREFIX + msg, @params);
}

public static void info(string msg, params object[] @params)
{
D.LogFormat(PREFIX + "INFO : " + msg, @params);
}

public static void warn(string msg, params object[] @params)
{
D.LogWarningFormat(PREFIX + "WARN : " + msg, @params);
}

public static void detail(string msg, params object[] @params)
{
D.LogFormat(PREFIX + "DETAIL : " + msg, @params);
}

public static void trace(string msg, params object[] @params)
{
if (!DistantObjectSettings.debugMode) return;
D.LogFormat(PREFIX + "TRACE : " + msg, @params);
}

public static void error(string msg, params object[] @params)
{
D.LogErrorFormat(PREFIX + "ERROR : " + msg, @params);
}

public static void error(Exception e)
{
D.LogException(e);
}

public static void error(Exception e, string msg, params object[] @params)
{
error(msg, @params);
error(e);
}

[ConditionalAttribute("DEBUG")]
public static void dbg(string msg, params object[] @params)
{
D.LogFormat(PREFIX + "TRACE : " + msg, @params);
}
}
}
24 changes: 8 additions & 16 deletions Source/DistantObject/SettingsGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void onAppLauncherTrue()
{
if (appLauncherButton == null)
{
Debug.LogError(Constants.DistantObject + " -- onAppLauncherTrue called without a button?!?");
Log.warn("onAppLauncherTrue called without a button?!?");
return;
}

Expand All @@ -103,7 +103,7 @@ void onAppLauncherFalse()
{
if (appLauncherButton == null)
{
Debug.LogError(Constants.DistantObject + " -- onAppLauncherFalse called without a button?!?");
Log.warn("onAppLauncherFalse called without a button?!?");
return;
}

Expand All @@ -115,18 +115,16 @@ ApplicationLauncherButton InitAppLauncherButton()
{
ApplicationLauncherButton button = null;
Texture2D iconTexture = null;
if (DistantObjectSettings.debugMode)
{
Debug.Log(Constants.DistantObject + " -- InitAppLauncherButton");
}
Log.trace("InitAppLauncherButton");

if (GameDatabase.Instance.ExistsTexture("DistantObject/Icons/toolbar_disabled_38"))
{
iconTexture = GameDatabase.Instance.GetTexture("DistantObject/Icons/toolbar_disabled_38", false);
}

if (iconTexture == null)
{
Debug.LogError(Constants.DistantObject + " -- Failed to load toolbar_disabled_38");
Log.error("Failed to load toolbar_disabled_38");
}
else
{
Expand All @@ -137,7 +135,7 @@ ApplicationLauncherButton InitAppLauncherButton()

if (button == null)
{
Debug.LogError(Constants.DistantObject + " -- Unable to create AppLauncher button");
Log.warn("Unable to create AppLauncher button");
}
}

Expand All @@ -148,10 +146,7 @@ private void AddAppLauncherButton()
{
if (useAppLauncher && appLauncherButton == null)
{
if (DistantObjectSettings.debugMode)
{
Debug.Log(Constants.DistantObject + " -- creating new appLauncher instance - " + this.GetInstanceID());
}
Log.trace("creating new appLauncher instance - " + this.GetInstanceID());
appLauncherButton = InitAppLauncherButton();
}
}
Expand All @@ -170,10 +165,7 @@ private void Awake()
//Load settings
ReadSettings();

if (DistantObjectSettings.debugMode)
{
Debug.Log(Constants.DistantObject + " -- SettingsGui awake - " + this.GetInstanceID());
}
Log.trace("SettingsGui awake - " + this.GetInstanceID());

GameEvents.onGUIApplicationLauncherReady.Add(AddAppLauncherButton);
GameEvents.onGUIApplicationLauncherDestroyed.Add(RemoveAppLauncherButton);
Expand Down
7 changes: 2 additions & 5 deletions Source/DistantObject/ToolbarDOE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class SettingsGui : MonoBehaviour

private void toolbarButton()
{
print(Constants.DistantObject + " -- Drawing toolbar icon...");
Log.dbg("Drawing toolbar icon...");
buttonDOSettings = ToolbarManager.Instance.add("test", "buttonDOSettings");
buttonDOSettings.Visibility = new GameScenesVisibility(GameScenes.SPACECENTER, GameScenes.FLIGHT);
if (activated)
Expand Down Expand Up @@ -69,10 +69,7 @@ private void ToggleIcon()

private void OnDestroy()
{
if (DistantObjectSettings.debugMode)
{
Debug.Log(Constants.DistantObject + " -- SettingsGui OnDestroy - " + this.GetInstanceID());
}
Log.trace("OnDestroy - {0}", this.GetInstanceID());

if (buttonDOSettings != null)
{
Expand Down
17 changes: 1 addition & 16 deletions Source/DistantObject/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,7 @@ public static Vector4 RGB2HSL(Color rgba)

class Constants
{
static private string _DistantObject = null;

static public string DistantObject
{
get
{
if (_DistantObject == null)
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;

_DistantObject = "Distant Object Enhancement v" + version.Major + "." + version.Minor + "." + version.Build;
}

return _DistantObject;
}
}
static internal readonly string DistantObject = "Distant Object Enhancement v" + Version.Text;
}

class DistantObjectSettings
Expand Down
23 changes: 7 additions & 16 deletions Source/DistantObject/VesselDraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static void DrawVessel(Vessel shipToDraw)
VesselRanges.Situation situation = shipToDraw.vesselRanges.GetSituationRanges(shipToDraw.situation);
if (Vector3d.Distance(cloneMesh.transform.position, FlightGlobals.ship_position) < situation.load)
{
Debug.LogError(Constants.DistantObject + " -- Tried to draw part " + partName + " within rendering distance of active vessel!");
Log.error("Tried to draw part {0} within rendering distance of active vessel!", partName);
continue;
}
cloneMesh.SetActive(true);
Expand Down Expand Up @@ -356,32 +356,23 @@ public void Awake()
}
else
{
if (DistantObjectSettings.debugMode)
{
Debug.LogError(Constants.DistantObject + " -- Could not find a model for part " + partName + ". Part will not render for VesselDraw.");
}
Log.trace("Could not find a model for part {0}. Part will not render for VesselDraw.", partName);
sawErrors = true;
}
}
else
{
if (DistantObjectSettings.debugMode)
{
Debug.LogError(Constants.DistantObject + " -- Could not find ConfigNode for part " + urlConfig.name + ". Part will not render for VesselDraw.");
}
Log.trace("Could not find ConfigNode for part {0}. Part will not render for VesselDraw.", urlConfig.name);
sawErrors = true;
}
}

print(Constants.DistantObject + " -- VesselDraw initialized");
if (sawErrors)
{
Debug.LogError(Constants.DistantObject + " -- Some parts do not have ConfigNode entries in the game database. Some distant vessels will be missing pieces.");
}
Log.dbg("VesselDraw initialized");
if (sawErrors) Log.error("Some parts do not have ConfigNode entries in the game database. Some distant vessels will be missing pieces.");
}
else if (DistantObjectSettings.debugMode)
else
{
print(Constants.DistantObject + " -- VesselDraw disabled");
Log.trace("VesselDraw disabled");
}
}

Expand Down

0 comments on commit a393b38

Please sign in to comment.