/* * Logger.cs * Authors: August Zinsser * * Copyright Matthew Belmonte 2007 */ using System; using System.Collections.Generic; using System.Text; using System.IO; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework; namespace tAC_Engine { /// /// This is the class that actually writes timestamped experimental codes to a file. /// Client classes can call LogCode(...) which makes this class log a timestamped code. /// Minigames should not call the LogKeyDown/Up(...). They should instead register keydowns/ups /// with the InputState class which will call LogKeyDown/Up(...) at a time closer to when the key /// was actually pressed. /// public static class Logger { /// /// All possible experimental codes. /// The first word/code (in caps followed by an underscore) should denote the minigame /// that code will typically be used for or indicate a debugging code. /// public enum ExperimentalCode { DEBUG_DrawFunctionStart = -1, DEBUG_VBlank = -2, DEBUG_Misc = -3, MM_GameBegin = 0, MM_DotPhaseBegin, MM_DotTrialBegin, MM_DotGimmeNextTrial, MM_DotTrialExpire, MM_DotTrialUserRespondLeft, MM_DotTrialUserRespondRight, MM_DotCoherenceEstimate, MM_ShipIdentityTestBegin, MM_ShipIdentitySelectLeft, MM_ShipIdentitySelectRight, MM_ShipIdentityConfirmSelection, MM_ShipIdentityTestEndSuccess, MM_ShipIdentityTestEndFailure, MM_ShooterPhaseBegin, MM_ShooterActivateOpenWormholeBeam, MM_ShooterCeaseOpenWormholeBeam, MM_ShooterOpenWormholeSuccess, MM_ShooterActivateMovePort, MM_ShooterCeaseMovePort, MM_ShooterActivateMoveStarboard, MM_ShooterCeaseMoveStarboard, MM_ShooterPresentFriendly, MM_ShooterPresentEnemy, MM_ShooterPresentWormhole, MM_ShooterActivateFireWeapon, MM_ShooterCeaseFireWeapon, MM_ShooterPlayerWeaponFired, MM_ShooterEnemyWeaponFired, MM_ShooterCollectibleSpawned, MM_ShooterPlayerCollectibleCollision, MM_ShooterMeteorExplosion, MM_ShooterPlayerGetsHit, MM_BossPhaseBegin, MM_GameEndSuccess, MM_GameEndFailure, APP_Pause, APP_UnPause, SVN_REVISION }; private static string mLogFilePath; //private static FileStream mFileStream; private static StreamWriter mOutputStream; private static bool mParOut = true; // Whether to also output codes to the parallel port /// /// Where the log file should be saved /// public static string LogFilePath { set { // Update the location mLogFilePath = value; // Reset the file stream and stream writer mOutputStream.Close(); // mFileStream.Close(); // mFileStream = new FileStream(mLogFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); mOutputStream = new StreamWriter(mLogFilePath, false, System.Text.Encoding.ASCII); mOutputStream.WriteLine(System.DateTime.Now); mOutputStream.Flush(); } get { return mLogFilePath; } } /// /// Constructor /// static Logger() { // Define a default path and create that file mLogFilePath = @"LogFiles\ExperimentLog.txt"; //mFileStream = new FileStream(mLogFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); mOutputStream = new StreamWriter(mLogFilePath, false, System.Text.Encoding.ASCII); mOutputStream.WriteLine(System.DateTime.Now); mOutputStream.Flush(); } /// /// Flushes the current log stream from memory to the file path specified by LogFilePath /// public static void FlushLog() { mOutputStream.Flush(); } /// /// Outputs a snapshot of the pressed keys (used mostly for debugging) /// /// The keys that are currently down public static void LogSnapshot(List downedKeys) { mOutputStream.WriteLine(GenericBaseApplication.GameManager.TotalSeconds + "\tSnapshot {"); foreach (Keys key in downedKeys) { mOutputStream.WriteLine("\t\t" + key); } mOutputStream.WriteLine("\t}"); } /// /// Logs the time of this input check /// public static void LogInputCheck() { mOutputStream.WriteLine(GenericBaseApplication.GameManager.TotalSeconds + "\tInputCheck"); } /// /// Logs the time the specified key was pressed /// /// public static void LogKeyDown(Keys key) { mOutputStream.WriteLine(GenericBaseApplication.GameManager.TotalSeconds + "\tKeyDown: " + key); } /// /// Logs the time the specified key was released /// /// public static void LogKeyUp(Keys key) { mOutputStream.WriteLine(GenericBaseApplication.GameManager.TotalSeconds + "\tKeyUp: " + key); } /// /// Logs the specified code /// /// public static void LogCode(ExperimentalCode code) { #if !DEBUG // Don't log debug codes if ((int) code < 0) return; #endif string seconds = string.Format("{0:###000.000}", GenericBaseApplication.GameManager.TotalSeconds); mOutputStream.WriteLine(seconds + "\t" + (int)code + "\t" + code); if (mParOut) POut((short)code); } /// /// Logs the specified code, tagged with extra information /// /// public static void LogCode(ExperimentalCode code, string tag) { #if !DEBUG // Don't log debug codes if ((int)code < 0) return; #endif string seconds; float time = GenericBaseApplication.GameManager.TotalSeconds; /*if (time != null) { seconds = "0:000000.000"; } else {*/ seconds = string.Format("{0:###000.000}", GenericBaseApplication.GameManager.TotalSeconds); // } mOutputStream.WriteLine(seconds + "\t" + (int)code + "\t" + code + "(" + tag + ")"); if (mParOut) POut((short)code); } /// /// Outputs data through the parallel port /// /// private static void POut(short data) { if (GenericBaseApplication.OutputPanel.ParallellPortPresent) { GenericBaseApplication.OutputPanel.Out(data); } } } }