293 lines
8.8 KiB
C#
293 lines
8.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
public class InternetVolumeLogger : MonoBehaviour
|
|
{
|
|
[Header("Logging Settings")]
|
|
public float logInterval = 5f; // Intervall in Sekunden
|
|
public string logFileName = "internet_volume_log.txt";
|
|
public bool enableLogging = true;
|
|
|
|
[Header("Display Settings")]
|
|
public bool showInConsole = true;
|
|
|
|
private long lastBytesReceived = 0;
|
|
private long lastBytesSent = 0;
|
|
private long totalBytesReceived = 0;
|
|
private long totalBytesSent = 0;
|
|
|
|
private float sessionStartTime;
|
|
private string logFilePath;
|
|
|
|
private void Start()
|
|
{
|
|
sessionStartTime = Time.time;
|
|
|
|
// Android-spezifischer Pfad für externe Speicherung
|
|
if (Application.platform == RuntimePlatform.Android)
|
|
{
|
|
logFilePath = Path.Combine(Application.persistentDataPath, logFileName);
|
|
}
|
|
else
|
|
{
|
|
logFilePath = Path.Combine(Application.persistentDataPath, logFileName);
|
|
}
|
|
|
|
Debug.Log($"Log-Datei wird gespeichert unter: {logFilePath}");
|
|
|
|
// Initialisiere die Baseline-Werte
|
|
InitializeBaseline();
|
|
|
|
// Starte das Logging
|
|
if (enableLogging)
|
|
{
|
|
StartCoroutine(LogInternetVolume());
|
|
}
|
|
|
|
// Schreibe Header in die Log-Datei
|
|
WriteLogHeader();
|
|
}
|
|
|
|
private void InitializeBaseline()
|
|
{
|
|
// Setze die Startwerte für die Messung
|
|
lastBytesReceived = GetTotalBytesReceived();
|
|
lastBytesSent = GetTotalBytesSent();
|
|
totalBytesReceived = 0;
|
|
totalBytesSent = 0;
|
|
}
|
|
|
|
private long GetTotalBytesReceived()
|
|
{
|
|
// Android-optimierte Netzwerk-Statistiken
|
|
if (Application.platform == RuntimePlatform.Android)
|
|
{
|
|
try
|
|
{
|
|
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
|
using (AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
|
|
using (AndroidJavaClass trafficStats = new AndroidJavaClass("android.net.TrafficStats"))
|
|
{
|
|
// Verwende Android TrafficStats für präzise Netzwerk-Messung
|
|
long totalRx = trafficStats.CallStatic<long>("getTotalRxBytes");
|
|
return totalRx;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogWarning($"Android TrafficStats nicht verfügbar: {e.Message}");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
return GC.GetTotalMemory(false);
|
|
}
|
|
|
|
private long GetTotalBytesSent()
|
|
{
|
|
// Android-optimierte Netzwerk-Statistiken
|
|
if (Application.platform == RuntimePlatform.Android)
|
|
{
|
|
try
|
|
{
|
|
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
|
using (AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
|
|
using (AndroidJavaClass trafficStats = new AndroidJavaClass("android.net.TrafficStats"))
|
|
{
|
|
// Verwende Android TrafficStats für präzise Netzwerk-Messung
|
|
long totalTx = trafficStats.CallStatic<long>("getTotalTxBytes");
|
|
return totalTx;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogWarning($"Android TrafficStats nicht verfügbar: {e.Message}");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
return GC.GetTotalMemory(false);
|
|
}
|
|
|
|
private IEnumerator LogInternetVolume()
|
|
{
|
|
while (enableLogging)
|
|
{
|
|
yield return new WaitForSeconds(logInterval);
|
|
|
|
// Aktuelle Netzwerk-Statistiken abrufen
|
|
long currentBytesReceived = GetTotalBytesReceived();
|
|
long currentBytesSent = GetTotalBytesSent();
|
|
|
|
// Berechne die Differenz seit der letzten Messung
|
|
long deltaReceived = currentBytesReceived - lastBytesReceived;
|
|
long deltaSent = currentBytesSent - lastBytesSent;
|
|
|
|
// Addiere zur Gesamtsumme
|
|
totalBytesReceived += deltaReceived;
|
|
totalBytesSent += deltaSent;
|
|
|
|
// Aktualisiere die letzten Werte
|
|
lastBytesReceived = currentBytesReceived;
|
|
lastBytesSent = currentBytesSent;
|
|
|
|
// Erstelle Log-Eintrag
|
|
LogEntry logEntry = new LogEntry
|
|
{
|
|
timestamp = DateTime.Now,
|
|
sessionTime = Time.time - sessionStartTime,
|
|
bytesReceivedDelta = deltaReceived,
|
|
bytesSentDelta = deltaSent,
|
|
totalBytesReceived = totalBytesReceived,
|
|
totalBytesSent = totalBytesSent,
|
|
totalBytes = totalBytesReceived + totalBytesSent
|
|
};
|
|
|
|
// Logge die Daten
|
|
LogData(logEntry);
|
|
}
|
|
}
|
|
|
|
private void LogData(LogEntry entry)
|
|
{
|
|
string logLine = FormatLogEntry(entry);
|
|
|
|
// Console Log
|
|
if (showInConsole)
|
|
{
|
|
Debug.Log($"[Internet Volume] {logLine}");
|
|
}
|
|
|
|
// Datei Log
|
|
WriteToFile(logLine);
|
|
}
|
|
|
|
private string FormatLogEntry(LogEntry entry)
|
|
{
|
|
return $"{entry.timestamp:yyyy-MM-dd HH:mm:ss} | " +
|
|
$"Session: {entry.sessionTime:F1}s | " +
|
|
$"Δ↓: {FormatBytes(entry.bytesReceivedDelta)} | " +
|
|
$"Δ↑: {FormatBytes(entry.bytesSentDelta)} | " +
|
|
$"Total↓: {FormatBytes(entry.totalBytesReceived)} | " +
|
|
$"Total↑: {FormatBytes(entry.totalBytesSent)} | " +
|
|
$"Total: {FormatBytes(entry.totalBytes)}";
|
|
}
|
|
|
|
private string FormatBytes(double bytes)
|
|
{
|
|
string[] suffixes = { "B", "KB", "MB", "GB", "TB" };
|
|
int suffixIndex = 0;
|
|
|
|
while (bytes >= 1024 && suffixIndex < suffixes.Length - 1)
|
|
{
|
|
bytes /= 1024;
|
|
suffixIndex++;
|
|
}
|
|
|
|
return $"{bytes:F2} {suffixes[suffixIndex]}";
|
|
}
|
|
|
|
private void WriteLogHeader()
|
|
{
|
|
string header = $"=== Internet Volume Log gestartet am {DateTime.Now:yyyy-MM-dd HH:mm:ss} ===\n" +
|
|
$"App: {Application.productName} v{Application.version}\n" +
|
|
$"Platform: {Application.platform}\n" +
|
|
$"Log Intervall: {logInterval}s\n" +
|
|
$"{'=',-80}\n";
|
|
|
|
WriteToFile(header);
|
|
}
|
|
|
|
private void WriteToFile(string content)
|
|
{
|
|
try
|
|
{
|
|
// Stelle sicher, dass das Verzeichnis existiert
|
|
string directory = Path.GetDirectoryName(logFilePath);
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
using (StreamWriter writer = new StreamWriter(logFilePath, true))
|
|
{
|
|
writer.WriteLine(content);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"Fehler beim Schreiben in Log-Datei: {e.Message}");
|
|
}
|
|
}
|
|
|
|
private void OnApplicationPause(bool pauseStatus)
|
|
{
|
|
if (pauseStatus)
|
|
{
|
|
WriteToFile($"App pausiert um {DateTime.Now:HH:mm:ss}");
|
|
}
|
|
else
|
|
{
|
|
WriteToFile($"App fortgesetzt um {DateTime.Now:HH:mm:ss}");
|
|
}
|
|
}
|
|
|
|
private void OnApplicationQuit()
|
|
{
|
|
WriteToFile($"=== Session beendet um {DateTime.Now:yyyy-MM-dd HH:mm:ss} ===");
|
|
WriteToFile($"Gesamte Session-Dauer: {(Time.time - sessionStartTime):F1}s");
|
|
WriteToFile($"Gesamt empfangen: {FormatBytes(totalBytesReceived)}");
|
|
WriteToFile($"Gesamt gesendet: {FormatBytes(totalBytesSent)}");
|
|
WriteToFile($"Gesamt-Volumen: {FormatBytes(totalBytesReceived + totalBytesSent)}");
|
|
}
|
|
|
|
// Öffentliche Methoden für externe Aufrufe
|
|
public void StartLogging()
|
|
{
|
|
if (!enableLogging)
|
|
{
|
|
enableLogging = true;
|
|
StartCoroutine(LogInternetVolume());
|
|
}
|
|
}
|
|
|
|
public void StopLogging()
|
|
{
|
|
enableLogging = false;
|
|
}
|
|
|
|
public void ResetStats()
|
|
{
|
|
totalBytesReceived = 0;
|
|
totalBytesSent = 0;
|
|
InitializeBaseline();
|
|
WriteToFile($"Statistiken zurückgesetzt um {DateTime.Now:HH:mm:ss}");
|
|
}
|
|
|
|
public long GetTotalVolume()
|
|
{
|
|
return totalBytesReceived + totalBytesSent;
|
|
}
|
|
|
|
public string GetLogFilePath()
|
|
{
|
|
return logFilePath;
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class LogEntry
|
|
{
|
|
public DateTime timestamp;
|
|
public float sessionTime;
|
|
public long bytesReceivedDelta;
|
|
public long bytesSentDelta;
|
|
public long totalBytesReceived;
|
|
public long totalBytesSent;
|
|
public long totalBytes;
|
|
}
|