|
Britbot
|
00001 #define DEBUG 00002 #define PROFILING 00003 #define DUMPFILE 00004 #define WRITE 00005 #define NOSHUTUP 00006 #define FORCE_BEGIN_STOP 00007 00008 //comment the following lines if you want to enable debug and profiling. 00009 //WARNING IF DEBUG IS ENABLED WE WILL TIME OUT. IT TAKES LOOOOOOONG TIME 00010 #undef DEBUG 00011 #undef PROFILING 00012 #undef DUMPFILE 00013 #undef WRITE 00014 #undef FORCE_BEGIN_STOP 00015 #undef NOSHTUP 00016 00017 #region #Usings 00018 00019 using System.Collections.Generic; 00020 00021 #endregion 00022 00023 namespace Britbot 00024 { 00025 internal static class Logger 00026 { 00027 #region Static Fields & Consts 00028 00029 #if DEBUG 00030 private static Dictionary<string, Queue<long>> times = new Dictionary<string, Queue<long>>(); 00031 private static Dictionary<string, long> begins = new Dictionary<string, long>(); 00032 private static Dictionary<string, int> count = new Dictionary<string, int>(); 00033 00034 #if DUMPFILE 00035 private static FileStream _logFileStream = 00036 new FileStream(string.Format("C:\\log_{0}_{1}.txt", DateTime.Now, Commander.Version), FileMode.Create, FileAccess.ReadWrite); 00037 #endif 00038 #endif 00039 00040 #endregion 00041 00042 public static void BeginTime(string key) 00043 { 00044 #if FORCE_BEGIN_STOP 00045 Logger.Write("==> Debug Begining " + key, true); 00046 #else 00047 Logger.Write("==> Debug Begining " + key, false); 00048 #endif 00049 00050 #if DEBUG 00051 Logger.Write("==> Debug Begining " + key + " time: " + Bot.Game.TimeRemaining()); 00052 00053 00054 #if DUMPFILE 00055 using (StreamWriter logWriter = new StreamWriter(Logger._logFileStream)) 00056 { 00057 logWriter.WriteLine("==> Debug Begining " + key + " time: " + Bot.Game.TimeRemaining()); 00058 } 00059 #endif 00060 00061 00062 /*if (Logger.begins.ContainsKey(key)) 00063 { 00064 Logger.begins[key] = Commander.TurnTimer.ElapsedMilliseconds; 00065 } 00066 else 00067 { 00068 Logger.begins.Add(key, Commander.TurnTimer.ElapsedMilliseconds); 00069 }*/ 00070 #endif 00071 } 00072 00073 public static void StopTime(string key) 00074 { 00075 #if FORCE_BEGIN_STOP 00076 Logger.Write("==> Debug Stopping " + key, true); 00077 #else 00078 Logger.Write("==> Debug Stopping " + key); 00079 #endif 00080 00081 #if DEBUG 00082 Bot.Game.Debug("==> Debug Stopping " + key + " time: " + Bot.Game.TimeRemaining()); 00083 00084 00085 #if DUMPFILE 00086 using (StreamWriter logWriter = new StreamWriter(Logger._logFileStream)) 00087 { 00088 logWriter.WriteLine("==> Debug Stopping " + key + " time: " + Bot.Game.TimeRemaining()); 00089 } 00090 #endif 00091 00092 /*if (Logger.times.ContainsKey(key)) 00093 { 00094 Logger.times[key].Enqueue(Commander.TurnTimer.ElapsedMilliseconds - Logger.begins[key]); 00095 } 00096 else 00097 { 00098 Logger.times.Add(key, new Queue<long>()); 00099 Logger.times[key].Enqueue(Commander.TurnTimer.ElapsedMilliseconds - Logger.begins[key]); 00100 00101 //keep the logger small 00102 if (Logger.times[key].Count > 100) 00103 Logger.times[key].Dequeue(); 00104 }*/ 00105 #endif 00106 } 00107 00108 00109 public static void Count(string key) 00110 { 00111 #if DEBUG 00112 if (Logger.count.ContainsKey(key)) 00113 { 00114 Logger.count[key]++; 00115 } 00116 else 00117 { 00118 Logger.count.Add(key, 1); 00119 } 00120 #endif 00121 } 00122 00123 public static void Profile() 00124 { 00125 #if PROFILING 00126 Bot.Game.Debug("------------------------PROFILING-----------------------"); 00127 foreach (KeyValuePair<string, Queue<long>> kv in Logger.times) 00128 { 00129 double avg = 0; 00130 long[] arr = kv.Value.ToArray(); 00131 for (int i = 0; i < arr.Length; i++) 00132 avg += arr[i]; 00133 00134 Bot.Game.Debug(kv.Key + " Avg: " + avg/arr.Length ); 00135 } 00136 #endif 00137 00138 #if DUMPFILE 00139 using (StreamWriter logWriter = new StreamWriter(Logger._logFileStream)) 00140 { 00141 double avg = 0; 00142 00143 logWriter.WriteLine("------------------------PROFILING-----------------------"); 00144 00145 foreach (KeyValuePair<string, List<long>> kv in Logger.times) 00146 { 00147 avg += kv.Value.Average(); 00148 logWriter.WriteLine(kv.Key + " Avg: " + kv.Value.Average() + "\t Max: " + kv.Value.Max()); 00149 } 00150 00151 logWriter.WriteLine("Total avg: " + avg); 00152 } 00153 #endif 00154 } 00155 00156 public static void DumpDebug() 00157 { 00158 #if DEBUG 00159 Bot.Game.Debug(Logger.times.ToString()); 00160 #endif 00161 } 00162 00163 public static void Write(string str, bool force = false) 00164 { 00165 #if WRITE 00166 Bot.Game.Debug(str); 00167 #else 00168 #if NOSHUTUP 00169 if(force) 00170 Bot.Game.Debug(str); 00171 #endif 00172 #endif 00173 00174 } 00175 } 00176 }
1.7.6.1