Skip to content

Commit

Permalink
Merge pull request #87 from erendrake/api_notification
Browse files Browse the repository at this point in the history
RemoteTech API now logs guid and all API calls, but only if VERBOSE_DEBUG_LOG = True in settings.cfg
  • Loading branch information
Starstrider42 committed Jun 5, 2014
2 parents ef0afd0 + d39b9d1 commit da9adb1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 deletions.
37 changes: 24 additions & 13 deletions src/RemoteTech2/API/API.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RemoteTech;
using UnityEngine;

namespace RemoteTech
{
Expand All @@ -13,7 +10,9 @@ public static bool HasFlightComputer(Guid id)
{
var satellite = RTCore.Instance.Satellites[id];
if (satellite == null) return false;
return satellite.FlightComputer != null;
var hasFlightComputer = satellite.FlightComputer != null;
RTLog.Verbose("Flight: {0} HasFlightComputer: {1}", id, hasFlightComputer);
return hasFlightComputer;
}

public static void AddSanctionedPilot(Guid id, Action<FlightCtrlState> autopilot)
Expand All @@ -24,6 +23,7 @@ public static void AddSanctionedPilot(Guid id, Action<FlightCtrlState> autopilot
{
if (spu.FlightComputer == null) continue;
if (spu.FlightComputer.SanctionedPilots.Contains(autopilot)) continue;
RTLog.Verbose("Flight: {0} Adding Sanctioned Pilot", id);
spu.FlightComputer.SanctionedPilots.Add(autopilot);
}
}
Expand All @@ -35,48 +35,59 @@ public static void RemoveSanctionedPilot(Guid id, Action<FlightCtrlState> autopi
foreach (var spu in satellite.SignalProcessors)
{
if (spu.FlightComputer == null) continue;
RTLog.Verbose("Flight: {0} Removing Sanctioned Pilot", id);
spu.FlightComputer.SanctionedPilots.Remove(autopilot);
}
}

public static bool HasAnyConnection(Guid id)
{
var satellite = RTCore.Instance.Satellites[id];
return RTCore.Instance.Network[satellite].Any();
var hasConnection = RTCore.Instance.Network[satellite].Any();
RTLog.Verbose("Flight: {0} Has Connection: {1}", id, hasConnection);
return hasConnection;
}

public static bool HasConnectionToKSC(Guid id)
{
var satellite = RTCore.Instance.Satellites[id];
return RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid));
var connectedToKerbin = RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid));
RTLog.Verbose("Flight: {0} Has Connection to Kerbin: {1}", id, connectedToKerbin);
return connectedToKerbin;
}

public static double GetShortestSignalDelay(Guid id)
{
var satellite = RTCore.Instance.Satellites[id];
if (!RTCore.Instance.Network[satellite].Any()) return Double.PositiveInfinity;
return RTCore.Instance.Network[satellite].Min().Delay;
var shortestDelay = RTCore.Instance.Network[satellite].Min().Delay;
RTLog.Verbose("Flight: Shortest signal delay from {0} to {1}", id, shortestDelay);
return shortestDelay;
}

public static double GetSignalDelayToKSC(Guid id)
{
var satellite = RTCore.Instance.Satellites[id];
if (!RTCore.Instance.Network[satellite].Any(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid))) return Double.PositiveInfinity;
return RTCore.Instance.Network[satellite].Where(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid)).Min().Delay;
var signalDelaytoKerbin = RTCore.Instance.Network[satellite].Where(r => RTCore.Instance.Network.GroundStations.ContainsKey(r.Goal.Guid)).Min().Delay;
RTLog.Verbose("Connection from {0} to Kerbin Delay: {1}", id, signalDelaytoKerbin);
return signalDelaytoKerbin;
}

public static double GetSignalDelayToSatellite(Guid a, Guid b)
{
var sat_a = RTCore.Instance.Satellites[a];
var sat_b = RTCore.Instance.Satellites[b];
if (sat_a == null || sat_b == null) return Double.PositiveInfinity;
var satelliteA = RTCore.Instance.Satellites[a];
var satelliteB = RTCore.Instance.Satellites[b];
if (satelliteA == null || satelliteB == null) return Double.PositiveInfinity;

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

var path = NetworkPathfinder.Solve(sat_a, sat_b, neighbors, cost, heuristic);
return path.Delay;
var path = NetworkPathfinder.Solve(satelliteA, satelliteB, neighbors, cost, heuristic);
var delayBetween = path.Delay;
RTLog.Verbose("Connection from {0} to {1} Delay: {2}", a, b, delayBetween);
return delayBetween;
}
}
}
50 changes: 23 additions & 27 deletions src/RemoteTech2/RTLog.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;

namespace RemoteTech
{
public static class RTLog
{
[Conditional("DEBUG")]
public static void Debug(String message)
{
UnityEngine.Debug.Log("RemoteTech: " + message);
}
private static readonly bool verboseLogging;

[Conditional("DEBUG")]
public static void Debug(String message, params System.Object[] param)
static RTLog()
{
UnityEngine.Debug.Log(String.Format("RemoteTech: " + message, param));
verboseLogging = GameSettings.VERBOSE_DEBUG_LOG;
}

[Conditional("DEBUG")]
public static void Debug(String message, params UnityEngine.Object[] param)
public static void Notify(string message)
{
UnityEngine.Debug.Log(String.Format("RemoteTech: " + message, param));
UnityEngine.Debug.Log("RemoteTech: " + message);
}

public static void Notify(String message)
public static void Notify(string message, params UnityEngine.Object[] param)
{
UnityEngine.Debug.Log("RemoteTech: " + message);
UnityEngine.Debug.Log(string.Format("RemoteTech: " + message, param));
}

public static void Notify(String message, params UnityEngine.Object[] param)
public static void Notify(string message, params object[] param)
{
UnityEngine.Debug.Log(String.Format("RemoteTech: " + message, param));
UnityEngine.Debug.Log(string.Format("RemoteTech: " + message, param));
}

public static void Notify(String message, params System.Object[] param)
public static void Verbose(string message, params object[] param)
{
UnityEngine.Debug.Log(String.Format("RemoteTech: " + message, param));
if (verboseLogging)
{
Notify(message, param);
}
}

public static string ToDebugString<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
public static void Verbose(string message, params UnityEngine.Object[] param)
{
return "{" +
string.Join(",",
dictionary.Select(kv => kv.Key.ToString() + "=" + kv.Value.ToString())
.ToArray()) + "}";
if (verboseLogging)
{
Notify(message, param);
}
}
}

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

0 comments on commit da9adb1

Please sign in to comment.