Server log, ai characters steer away from the abyss

This commit is contained in:
Regalis
2016-02-14 16:47:23 +02:00
parent ea6824a60d
commit bec6d95198
27 changed files with 306 additions and 71 deletions

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Barotrauma.Networking
{
class ServerLog
{
const int LinesPerFile = 500;
const string SavePath = "ServerLogs";
private string serverName;
private Queue<string> lines;
public ServerLog(string serverName)
{
this.serverName = serverName;
lines = new Queue<string>();
}
public void WriteLine(string line)
{
string logLine = "[" + DateTime.Now.ToLongTimeString() + "] " + line;
lines.Enqueue(logLine);
if (lines.Count>=LinesPerFile)
{
Save();
}
}
public void Save()
{
if (!Directory.Exists(SavePath))
{
try
{
Directory.CreateDirectory(SavePath);
}
catch (Exception e)
{
DebugConsole.ThrowError("Failed to create a folder for server logs", e);
return;
}
}
string fileName = serverName+"_"+DateTime.Now.ToShortDateString()+"_"+DateTime.Now.ToShortTimeString()+".txt";
fileName = fileName.Replace(":", "");
string filePath = Path.Combine(SavePath, fileName);
try
{
File.WriteAllLines(filePath, lines);
}
catch (Exception e)
{
DebugConsole.ThrowError("Saving the server log to " + filePath + " failed", e);
}
}
}
}