Add ChatBot logging to file and 'log' command

- log <text> will print '[BOT] text' to the console
- logged [BOT] lines can be written to a logfile
- chatbotlogfile INI setting is used to set the log file
This commit is contained in:
ORelio 2014-07-20 12:02:17 +02:00
parent 6f1fcb22ce
commit 162a1414bf
5 changed files with 63 additions and 15 deletions

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace MinecraftClient
{
@ -70,6 +71,7 @@ namespace MinecraftClient
/* =================================================================== */
/* ToolBox - Methods below might be useful while creating your bot. */
/* You should not need to interact with other classes of the program. */
/* All the methods in this ChatBot class should do the job for you. */
/* =================================================================== */
/// <summary>
@ -80,7 +82,7 @@ namespace MinecraftClient
protected bool SendText(string text)
{
ConsoleIO.WriteLineFormatted("§8BOT:" + text, false);
LogToConsole("Sending '" + text + "'");
return handler.SendText(text);
}
@ -257,6 +259,19 @@ namespace MinecraftClient
public static void LogToConsole(string text)
{
ConsoleIO.WriteLineFormatted("§8[BOT] " + text);
if (String.IsNullOrEmpty(Settings.chatbotLogFile))
{
if (!File.Exists(Settings.chatbotLogFile))
{
try { Directory.CreateDirectory(Path.GetDirectoryName(Settings.chatbotLogFile)); }
catch { return; /* Invalid path or access denied */ }
try { File.WriteAllText(Settings.chatbotLogFile, ""); }
catch { return; /* Invalid file name or access denied */ }
}
File.AppendAllLines(Settings.chatbotLogFile, new string[] { getTimestamp() + ' ' + text });
}
}
/// <summary>
@ -317,5 +332,24 @@ namespace MinecraftClient
{
handler.BotLoad(new ChatBots.Script(filename, playername));
}
/// <summary>
/// Get a D-M-Y h:m:s timestamp representing the current system date and time
/// </summary>
protected static string getTimestamp()
{
DateTime time = DateTime.Now;
string D = time.Day.ToString("00");
string M = time.Month.ToString("00");
string Y = time.Year.ToString("0000");
string h = time.Hour.ToString("00");
string m = time.Minute.ToString("00");
string s = time.Second.ToString("00");
return "" + D + '-' + M + '-' + Y + ' ' + h + ':' + m + ':' + s;
}
}
}

View file

@ -90,20 +90,7 @@ namespace MinecraftClient.ChatBots
private void save(string tosave)
{
if (dateandtime)
{
int day = DateTime.Now.Day, month = DateTime.Now.Month;
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
string D = day < 10 ? "0" + day : "" + day;
string M = month < 10 ? "0" + month : "" + day;
string Y = "" + DateTime.Now.Year;
string h = hour < 10 ? "0" + hour : "" + hour;
string m = minute < 10 ? "0" + minute : "" + minute;
string s = second < 10 ? "0" + second : "" + second;
tosave = "" + D + '-' + M + '-' + Y + ' ' + h + ':' + m + ':' + s + ' ' + tosave;
}
tosave = getTimestamp() + ' ' + tosave;
Directory.CreateDirectory(Path.GetDirectoryName(logfile));
FileStream stream = new FileStream(logfile, FileMode.OpenOrCreate);

View file

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Commands
{
public class Log : Command
{
public override string CMDName { get { return "log"; } }
public override string CMDDesc { get { return "log <text>: log some text to the console."; } }
public override string Run(McTcpClient handler, string command)
{
if (hasArg(command))
{
ChatBot.LogToConsole(getArg(command));
return "";
}
else return CMDDesc;
}
}
}

View file

@ -85,6 +85,7 @@
<Compile Include="Command.cs" />
<Compile Include="Commands\Connect.cs" />
<Compile Include="Commands\Exit.cs" />
<Compile Include="Commands\Log.cs" />
<Compile Include="Commands\Reco.cs" />
<Compile Include="Commands\Respawn.cs" />
<Compile Include="Commands\Script.cs" />

View file

@ -43,6 +43,7 @@ namespace MinecraftClient
public static bool exitOnFailure = false;
public static char internalCmdChar = '/';
public static bool playerHeadAsIcon = false;
public static string chatbotLogFile = "";
//AntiAFK Settings
public static bool AntiAFK_Enabled = false;
@ -147,6 +148,7 @@ namespace MinecraftClient
case "timestamps": chatTimeStamps = str2bool(argValue); break;
case "exitonfailure": exitOnFailure = str2bool(argValue); break;
case "playerheadicon": playerHeadAsIcon = str2bool(argValue); break;
case "chatbotlogfile": chatbotLogFile = argValue; break;
case "mcversion": ServerVersion = argValue; break;
case "botowners":
@ -328,6 +330,7 @@ namespace MinecraftClient
+ "consoletitle=%username% - Minecraft Console Client\r\n"
+ "internalcmdchar=slash #use 'none', 'slash' or 'backslash'\r\n"
+ "mcversion=auto #use 'auto' or '1.X.X' values\r\n"
+ "chatbotlogfile= #leave empty for no logfile\r\n"
+ "accountlist=accounts.txt\r\n"
+ "serverlist=servers.txt\r\n"
+ "playerheadicon=true\r\n"