Skip to content

Commit

Permalink
LogToConsole, LogToFile added, fixed log
Browse files Browse the repository at this point in the history
  • Loading branch information
japanese-concept committed Aug 27, 2018
1 parent 57ccfa3 commit 81f9d73
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions CommandLogger.csproj
Expand Up @@ -7,8 +7,8 @@
<ProjectGuid>{602882B6-F16C-4247-9F5D-21846BC4C09A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CoolPuppy24.CommandLogger</RootNamespace>
<AssemblyName>CoolPuppy24.CommandLogger</AssemblyName>
<RootNamespace>CoolPuppy24.WisserTg.CommandLogger</RootNamespace>
<AssemblyName>CoolPuppy24.WisserTg.CommandLogger</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SccProjectName>SAK</SccProjectName>
Expand Down
6 changes: 6 additions & 0 deletions CommandLoggerConfiguration.cs
Expand Up @@ -32,6 +32,9 @@ public CommandLoggerEntry(string Name, params string[] Aliases)
}
public sealed class CommandLoggerConfiguration : IDefaultable, IRocketPluginConfiguration
{
public bool LogToConsole;
public bool LogToFile;

[XmlElement("LogFormat")]
public string LogFormat;

Expand All @@ -40,6 +43,9 @@ public sealed class CommandLoggerConfiguration : IDefaultable, IRocketPluginConf

public void LoadDefaults()
{
this.LogToConsole = true;
this.LogToFile = true;

this.LogFormat = "[{0}] {1} ({2}) executed \"{3}\" command as \"{4}\" with \"{5}\" arguments.";
this.Entries = new CommandLoggerEntry[]
{
Expand Down
5 changes: 3 additions & 2 deletions CommandLoggerCore.cs
Expand Up @@ -30,13 +30,14 @@ private void OnPlayerChatted(Rocket.Unturned.Player.UnturnedPlayer player, ref U
if (!message.StartsWith("/"))
return;
// So, player entered the command and we'll deleting "/" letter from message.
message = message.Remove(0);
message = message.Remove(0, 1);
// Deleting command arguments bcuz we only need the command.
string[] args = message.Split(' ').Skip(1).ToArray();
// Set message to command name.
message = message.Split(' ')[0];

CommandLoggerEntry Entry = this.Configuration.Instance.Entries.Where(entry => entry.Name == message || entry.GetAliases().Contains(message)).FirstOrDefault();
CommandLoggerEntry Entry = this.Configuration.Instance.Entries.Where(entry => entry.Name == message ^ entry.GetAliases().Contains(message)).FirstOrDefault();

if (Entry.Equals(default(CommandLoggerEntry)))
return;

Expand Down
23 changes: 13 additions & 10 deletions CommandLoggerLogger.cs
@@ -1,4 +1,5 @@
using System.IO;
using Logger = Rocket.Core.Logging.Logger;

namespace coolpuppy24.commandlogger
{
Expand All @@ -12,27 +13,29 @@ public sealed class CommandLoggerLogger
private FileAccess m_fileaccess;
public CommandLoggerLogger()
{
this.m_filepath = CommandLoggerCore.Instance.Directory + @"\Log.txt";
this.m_filepath = CommandLoggerCore.Instance.Directory + @"\CoolPuppy24.WisserTg.CommandLogger.log";

this.m_filemode = FileMode.Append;
this.m_fileaccess = FileAccess.Write;
}
public CommandLoggerLogger(string FilePath) : this()
{
this.m_filepath = CommandLoggerCore.Instance.Directory + FilePath;

this.m_filemode = FileMode.Append;
this.m_fileaccess = FileAccess.Write;
}

public void Log(string Message)
{
if (!File.Exists(this.m_filepath))
this.m_filemode = FileMode.CreateNew;
using (StreamWriter sw = new StreamWriter(new FileStream(this.m_filepath, this.m_filemode, this.m_fileaccess)))
sw.WriteLine(Message);
if (this.m_filemode == FileMode.CreateNew)
this.m_filemode = FileMode.Append;
if (CommandLoggerCore.Instance.Configuration.Instance.LogToFile)
{
if (!File.Exists(this.m_filepath))
this.m_filemode = FileMode.CreateNew;
using (StreamWriter sw = new StreamWriter(new FileStream(this.m_filepath, this.m_filemode, this.m_fileaccess)))
sw.WriteLine(Message);
if (this.m_filemode == FileMode.CreateNew)
this.m_filemode = FileMode.Append;
}
if (CommandLoggerCore.Instance.Configuration.Instance.LogToConsole)
Logger.Log(Message);
}
}
}

0 comments on commit 81f9d73

Please sign in to comment.