Skip to content

Commit da9adb1

Browse files
committed
Merge pull request #87 from erendrake/api_notification
RemoteTech API now logs guid and all API calls, but only if VERBOSE_DEBUG_LOG = True in settings.cfg
2 parents ef0afd0 + d39b9d1 commit da9adb1

File tree

2 files changed

+47
-40
lines changed

2 files changed

+47
-40
lines changed

src/RemoteTech2/API/API.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
5-
using RemoteTech;
6-
using UnityEngine;
74

85
namespace RemoteTech
96
{
@@ -13,7 +10,9 @@ public static bool HasFlightComputer(Guid id)
1310
{
1411
var satellite = RTCore.Instance.Satellites[id];
1512
if (satellite == null) return false;
16-
return satellite.FlightComputer != null;
13+
var hasFlightComputer = satellite.FlightComputer != null;
14+
RTLog.Verbose("Flight: {0} HasFlightComputer: {1}", id, hasFlightComputer);
15+
return hasFlightComputer;
1716
}
1817

1918
public static void AddSanctionedPilot(Guid id, Action<FlightCtrlState> autopilot)
@@ -24,6 +23,7 @@ public static void AddSanctionedPilot(Guid id, Action<FlightCtrlState> autopilot
2423
{
2524
if (spu.FlightComputer == null) continue;
2625
if (spu.FlightComputer.SanctionedPilots.Contains(autopilot)) continue;
26+
RTLog.Verbose("Flight: {0} Adding Sanctioned Pilot", id);
2727
spu.FlightComputer.SanctionedPilots.Add(autopilot);
2828
}
2929
}
@@ -35,48 +35,59 @@ public static void RemoveSanctionedPilot(Guid id, Action<FlightCtrlState> autopi
3535
foreach (var spu in satellite.SignalProcessors)
3636
{
3737
if (spu.FlightComputer == null) continue;
38+
RTLog.Verbose("Flight: {0} Removing Sanctioned Pilot", id);
3839
spu.FlightComputer.SanctionedPilots.Remove(autopilot);
3940
}
4041
}
4142

4243
public static bool HasAnyConnection(Guid id)
4344
{
4445
var satellite = RTCore.Instance.Satellites[id];
45-
return RTCore.Instance.Network[satellite].Any();
46+
var hasConnection = RTCore.Instance.Network[satellite].Any();
47+
RTLog.Verbose("Flight: {0} Has Connection: {1}", id, hasConnection);
48+
return hasConnection;
4649
}
4750

4851
public static bool HasConnectionToKSC(Guid id)
4952
{
5053
var satellite = RTCore.Instance.Satellites[id];
51-
return RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid));
54+
var connectedToKerbin = RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid));
55+
RTLog.Verbose("Flight: {0} Has Connection to Kerbin: {1}", id, connectedToKerbin);
56+
return connectedToKerbin;
5257
}
5358

5459
public static double GetShortestSignalDelay(Guid id)
5560
{
5661
var satellite = RTCore.Instance.Satellites[id];
5762
if (!RTCore.Instance.Network[satellite].Any()) return Double.PositiveInfinity;
58-
return RTCore.Instance.Network[satellite].Min().Delay;
63+
var shortestDelay = RTCore.Instance.Network[satellite].Min().Delay;
64+
RTLog.Verbose("Flight: Shortest signal delay from {0} to {1}", id, shortestDelay);
65+
return shortestDelay;
5966
}
6067

6168
public static double GetSignalDelayToKSC(Guid id)
6269
{
6370
var satellite = RTCore.Instance.Satellites[id];
6471
if (!RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid))) return Double.PositiveInfinity;
65-
return RTCore.Instance.Network[satellite].Where(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid)).Min().Delay;
72+
var signalDelaytoKerbin = RTCore.Instance.Network[satellite].Where(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid)).Min().Delay;
73+
RTLog.Verbose("Connection from {0} to Kerbin Delay: {1}", id, signalDelaytoKerbin);
74+
return signalDelaytoKerbin;
6675
}
6776

6877
public static double GetSignalDelayToSatellite(Guid a, Guid b)
6978
{
70-
var sat_a = RTCore.Instance.Satellites[a];
71-
var sat_b = RTCore.Instance.Satellites[b];
72-
if (sat_a == null || sat_b == null) return Double.PositiveInfinity;
79+
var satelliteA = RTCore.Instance.Satellites[a];
80+
var satelliteB = RTCore.Instance.Satellites[b];
81+
if (satelliteA == null || satelliteB == null) return Double.PositiveInfinity;
7382

7483
Func<ISatellite, IEnumerable<NetworkLink<ISatellite>>> neighbors = RTCore.Instance.Network.FindNeighbors;
7584
Func<ISatellite, NetworkLink<ISatellite>, double> cost = RangeModelExtensions.DistanceTo;
7685
Func<ISatellite, ISatellite, double> heuristic = RangeModelExtensions.DistanceTo;
7786

78-
var path = NetworkPathfinder.Solve(sat_a, sat_b, neighbors, cost, heuristic);
79-
return path.Delay;
87+
var path = NetworkPathfinder.Solve(satelliteA, satelliteB, neighbors, cost, heuristic);
88+
var delayBetween = path.Delay;
89+
RTLog.Verbose("Connection from {0} to {1} Delay: {2}", a, b, delayBetween);
90+
return delayBetween;
8091
}
8192
}
8293
}

src/RemoteTech2/RTLog.cs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
1-
using System;
1+
using System.Collections.Generic;
22
using System.Linq;
3-
using System.Text;
4-
using System.Collections.Generic;
5-
using UnityEngine;
6-
using System.Diagnostics;
73

84
namespace RemoteTech
95
{
106
public static class RTLog
117
{
12-
[Conditional("DEBUG")]
13-
public static void Debug(String message)
14-
{
15-
UnityEngine.Debug.Log("RemoteTech: " + message);
16-
}
8+
private static readonly bool verboseLogging;
179

18-
[Conditional("DEBUG")]
19-
public static void Debug(String message, params System.Object[] param)
10+
static RTLog()
2011
{
21-
UnityEngine.Debug.Log(String.Format("RemoteTech: " + message, param));
12+
verboseLogging = GameSettings.VERBOSE_DEBUG_LOG;
2213
}
2314

24-
[Conditional("DEBUG")]
25-
public static void Debug(String message, params UnityEngine.Object[] param)
15+
public static void Notify(string message)
2616
{
27-
UnityEngine.Debug.Log(String.Format("RemoteTech: " + message, param));
17+
UnityEngine.Debug.Log("RemoteTech: " + message);
2818
}
2919

30-
public static void Notify(String message)
20+
public static void Notify(string message, params UnityEngine.Object[] param)
3121
{
32-
UnityEngine.Debug.Log("RemoteTech: " + message);
22+
UnityEngine.Debug.Log(string.Format("RemoteTech: " + message, param));
3323
}
3424

35-
public static void Notify(String message, params UnityEngine.Object[] param)
25+
public static void Notify(string message, params object[] param)
3626
{
37-
UnityEngine.Debug.Log(String.Format("RemoteTech: " + message, param));
27+
UnityEngine.Debug.Log(string.Format("RemoteTech: " + message, param));
3828
}
3929

40-
public static void Notify(String message, params System.Object[] param)
30+
public static void Verbose(string message, params object[] param)
4131
{
42-
UnityEngine.Debug.Log(String.Format("RemoteTech: " + message, param));
32+
if (verboseLogging)
33+
{
34+
Notify(message, param);
35+
}
4336
}
4437

45-
public static string ToDebugString<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
38+
public static void Verbose(string message, params UnityEngine.Object[] param)
4639
{
47-
return "{" +
48-
string.Join(",",
49-
dictionary.Select(kv => kv.Key.ToString() + "=" + kv.Value.ToString())
50-
.ToArray()) + "}";
40+
if (verboseLogging)
41+
{
42+
Notify(message, param);
43+
}
5144
}
45+
}
5246

47+
public static class LoggingExtenstions
48+
{
5349
public static string ToDebugString<T>(this List<T> list)
5450
{
5551
return "{" + string.Join(",", list.Select(x => x.ToString()).ToArray()) + "}";

0 commit comments

Comments
 (0)