From 162a1414bf6ac829170ea3e2740cdfb501b479cb Mon Sep 17 00:00:00 2001 From: ORelio Date: Sun, 20 Jul 2014 12:02:17 +0200 Subject: [PATCH] Add ChatBot logging to file and 'log' command - log 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 --- MinecraftClient/ChatBot.cs | 36 +++++++++++++++++++++++++- MinecraftClient/ChatBots/ChatLog.cs | 15 +---------- MinecraftClient/Commands/Log.cs | 23 ++++++++++++++++ MinecraftClient/MinecraftClient.csproj | 1 + MinecraftClient/Settings.cs | 3 +++ 5 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 MinecraftClient/Commands/Log.cs diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index 8955eca6..b632863a 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -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. */ /* =================================================================== */ /// @@ -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 }); + } } /// @@ -317,5 +332,24 @@ namespace MinecraftClient { handler.BotLoad(new ChatBots.Script(filename, playername)); } + + /// + /// Get a D-M-Y h:m:s timestamp representing the current system date and time + /// + + 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; + } } } diff --git a/MinecraftClient/ChatBots/ChatLog.cs b/MinecraftClient/ChatBots/ChatLog.cs index ebc6246c..c20d6d4d 100644 --- a/MinecraftClient/ChatBots/ChatLog.cs +++ b/MinecraftClient/ChatBots/ChatLog.cs @@ -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); diff --git a/MinecraftClient/Commands/Log.cs b/MinecraftClient/Commands/Log.cs new file mode 100644 index 00000000..afc2dcef --- /dev/null +++ b/MinecraftClient/Commands/Log.cs @@ -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 : 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; + } + } +} diff --git a/MinecraftClient/MinecraftClient.csproj b/MinecraftClient/MinecraftClient.csproj index ce1d3b27..c6465719 100644 --- a/MinecraftClient/MinecraftClient.csproj +++ b/MinecraftClient/MinecraftClient.csproj @@ -85,6 +85,7 @@ + diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 3b2b95fd..98e2004f 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -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"