Skip to content

Commit

Permalink
Refactor logging to use LogError method
Browse files Browse the repository at this point in the history
Replaced Debug.WriteLine and MessageBox.Show calls in AIManager.cs with a new LogError method for consistent error logging. LogError writes to debug.txt if "Debug Mode" is enabled in Dictionary.toggleState. Specific changes include:
- Commented out Debug.WriteLine for average loop iteration time and added LogError.
- Replaced MessageBox.Show for screen errors with LogError.
- Added LogError for device removal and frame acquisition failures.
- Replaced Debug.WriteLine for SharpGenException and screen capture errors with LogError.
- Introduced private LogError method to handle logging.
  • Loading branch information
TaylorIsBlue committed Aug 13, 2024
1 parent cce7a1e commit 5a2272e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions Aimmy2/AILogic/AIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@ private async void AiLoop()
if (Dictionary.toggleState["Debug Mode"])
{
double averageTime = totalTime / 1000.0;
Debug.WriteLine($"Average loop iteration time: {averageTime} ms");
//Debug.WriteLine($"Average loop iteration time: {averageTime} ms");
MessageBox.Show($"Average loop iteration time: {averageTime} ms", "Share this iteration time on our discord!");
LogError($"Average loop iteration time: {averageTime} ms");
totalTime = 0;
iterationCount = 0;
}
Expand Down Expand Up @@ -658,7 +659,7 @@ private void HandlePredictions(KalmanPrediction kalmanPrediction, Prediction clo
}
catch (Exception e)
{
MessageBox.Show("Error capturing screen. " + e);
LogError("Error capturing screen:" + e);
return null;
}
return null;
Expand Down Expand Up @@ -694,9 +695,11 @@ private Rectangle ClampRectangle(Rectangle rect, int screenWidth, int screenHeig
{
if (result == Vortice.DXGI.ResultCode.DeviceRemoved) // This usually happens when using closest to mouse
{
LogError("Device removed, reinitializing D3D11.");
ReinitializeD3D11();
return null;
}
LogError("Failed to acquire next frame: " + result);
ReinitializeD3D11();
return null;
}
Expand Down Expand Up @@ -776,16 +779,24 @@ private Rectangle ClampRectangle(Rectangle rect, int screenWidth, int screenHeig
}
catch (SharpGenException ex)
{
Debug.WriteLine("SharpGenException: " + ex);
LogError("SharpGenException: " + ex);
return null;
}
catch (Exception e)
{
Debug.WriteLine("Error capturing screen. " + e);
LogError("Error capturing screen: " + e);
return null;
}
}

private void LogError(string message)
{
if (Dictionary.toggleState["Debug Mode"])
{
string logFilePath = "debug.txt";
using StreamWriter writer = new StreamWriter(logFilePath, true);
writer.WriteLine($"[{DateTime.Now}]: {message}");
}
}
//private Bitmap? DeprecatedScreen(Rectangle detectionBox) // if for some reason they want to use the old method...
//{
// if (_screenCaptureBitmap == null || _screenCaptureBitmap.Width != detectionBox.Width || _screenCaptureBitmap.Height != detectionBox.Height)
Expand Down

0 comments on commit 5a2272e

Please sign in to comment.