Added "-logpath" command line parameter.

Added "LogPath" config setting.
This commit is contained in:
CoderCow 2013-06-29 12:15:32 +02:00
parent 8af4061606
commit 863082dfc3
2 changed files with 63 additions and 31 deletions

View file

@ -249,6 +249,8 @@ namespace TShockAPI
[Description("Allows stacks in chests to be beyond the stack limit")] public bool IgnoreChestStacksOnLoad = false; [Description("Allows stacks in chests to be beyond the stack limit")] public bool IgnoreChestStacksOnLoad = false;
[Description("The path of the directory where logs should be written into.")] public string LogPath = "tshock";
/// <summary> /// <summary>
/// Reads a configuration file from a given path /// Reads a configuration file from a given path
/// </summary> /// </summary>

View file

@ -40,13 +40,15 @@ namespace TShockAPI
[APIVersion(1, 13)] [APIVersion(1, 13)]
public class TShock : TerrariaPlugin public class TShock : TerrariaPlugin
{ {
private const string LogFormatDefault = "yyyy-MM-dd_HH-mm-ss";
private static string LogFormat = LogFormatDefault;
private static bool LogClear = false;
public static readonly Version VersionNum = Assembly.GetExecutingAssembly().GetName().Version; public static readonly Version VersionNum = Assembly.GetExecutingAssembly().GetName().Version;
public static readonly string VersionCodename = "Welcome to the future."; public static readonly string VersionCodename = "Welcome to the future.";
public static string SavePath = "tshock"; public static string SavePath = "tshock";
private const string LogFormatDefault = "yyyy-MM-dd_HH-mm-ss";
private static string LogFormat = LogFormatDefault;
private const string LogPathDefault = "tshock";
private static string LogPath = LogPathDefault;
private static bool LogClear = false;
public static TSPlayer[] Players = new TSPlayer[Main.maxPlayers]; public static TSPlayer[] Players = new TSPlayer[Main.maxPlayers];
public static BanManager Bans; public static BanManager Bans;
@ -112,35 +114,57 @@ namespace TShockAPI
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")] [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
public override void Initialize() public override void Initialize()
{ {
HandleCommandLine(Environment.GetCommandLineArgs());
if (!Directory.Exists(SavePath))
Directory.CreateDirectory(SavePath);
DateTime now = DateTime.Now;
string logFilename;
try try
{ {
logFilename = Path.Combine(SavePath, now.ToString(LogFormat)+".log"); HandleCommandLine(Environment.GetCommandLineArgs());
}
catch(Exception) if (Version.Major >= 4)
{ getTShockAscii();
// Problem with the log format use the default
logFilename = Path.Combine(SavePath, now.ToString(LogFormatDefault) + ".log"); if (!Directory.Exists(SavePath))
} Directory.CreateDirectory(SavePath);
ConfigFile.ConfigRead += OnConfigRead;
FileTools.SetupConfig();
DateTime now = DateTime.Now;
string logFilename;
string logPathSetupWarning = null;
// Log path was not already set by the command line parameter?
if (LogPath == LogPathDefault)
LogPath = Config.LogPath;
try
{
logFilename = Path.Combine(LogPath, now.ToString(LogFormat)+".log");
if (!Directory.Exists(LogPath))
Directory.CreateDirectory(LogPath);
}
catch(Exception ex)
{
logPathSetupWarning = "Could not apply the given log path / log format, defaults will be used. Exception details:\n" + ex;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(logPathSetupWarning);
Console.ForegroundColor = ConsoleColor.Gray;
// Problem with the log path or format use the default
logFilename = Path.Combine(LogPathDefault, now.ToString(LogFormatDefault) + ".log");
}
#if DEBUG #if DEBUG
Log.Initialize(logFilename, LogLevel.All, false); Log.Initialize(logFilename, LogLevel.All, false);
#else #else
Log.Initialize(logFilename, LogLevel.All & ~LogLevel.Debug, LogClear); Log.Initialize(logFilename, LogLevel.All & ~LogLevel.Debug, LogClear);
#endif #endif
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; if (logPathSetupWarning != null)
Log.Warn(logPathSetupWarning);
if (Version.Major >= 4)
{
getTShockAscii();
}
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
catch(Exception ex)
{
// Will be handled by the server api and written to its crashlog.txt.
throw new Exception("Fatal TShock initialization exception. See inner exception for details.", ex);
}
// Further exceptions are written to TShock's log from now on.
try try
{ {
if (File.Exists(Path.Combine(SavePath, "tshock.pid"))) if (File.Exists(Path.Combine(SavePath, "tshock.pid")))
@ -151,9 +175,6 @@ namespace TShockAPI
} }
File.WriteAllText(Path.Combine(SavePath, "tshock.pid"), Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture)); File.WriteAllText(Path.Combine(SavePath, "tshock.pid"), Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture));
ConfigFile.ConfigRead += OnConfigRead;
FileTools.SetupConfig();
HandleCommandLinePostConfigLoad(Environment.GetCommandLineArgs()); HandleCommandLinePostConfigLoad(Environment.GetCommandLineArgs());
if (Config.StorageType.ToLower() == "sqlite") if (Config.StorageType.ToLower() == "sqlite")
@ -446,9 +467,13 @@ namespace TShockAPI
} }
break; break;
case "-dump": case "-logpath":
ConfigFile.DumpDescriptions(); path = parms[++i];
Permissions.DumpDescriptions(); if (path.IndexOfAny(Path.GetInvalidPathChars()) == -1)
{
LogPath = path;
Log.ConsoleInfo("Log path has been set to " + path);
}
break; break;
case "-logformat": case "-logformat":
@ -458,6 +483,11 @@ namespace TShockAPI
case "-logclear": case "-logclear":
bool.TryParse(parms[++i], out LogClear); bool.TryParse(parms[++i], out LogClear);
break; break;
case "-dump":
ConfigFile.DumpDescriptions();
Permissions.DumpDescriptions();
break;
} }
} }
} }