29 using System.Collections.Generic;
31 using System.Reflection;
36 namespace OpenSim.Framework.Monitoring
41 public static class StatsLogger
43 private static readonly ILog m_statsLog = LogManager.GetLogger(
"special.StatsLogger");
45 private static Timer m_loggingTimer;
46 private static int m_statsLogIntervalMs = 5000;
48 public static void RegisterConsoleCommands(ICommandConsole console)
50 console.Commands.AddCommand(
54 "stats record start|stop",
55 "Control whether stats are being regularly recorded to a separate file.",
56 "For debug purposes. Experimental.",
57 HandleStatsRecordCommand);
59 console.Commands.AddCommand(
64 "Save stats snapshot to a file. If the file already exists, then the report is appended.",
65 "For debug purposes. Experimental.",
66 HandleStatsSaveCommand);
69 public static void HandleStatsRecordCommand(
string module,
string[] cmd)
71 ICommandConsole con = MainConsole.Instance;
75 con.Output(
"Usage: stats record start|stop");
79 if (cmd[2] ==
"start")
82 con.OutputFormat(
"Now recording all stats to file every {0}ms", m_statsLogIntervalMs);
84 else if (cmd[2] ==
"stop")
87 con.Output(
"Stopped recording stats to file.");
91 public static void HandleStatsSaveCommand(
string module,
string[] cmd)
93 ICommandConsole con = MainConsole.Instance;
97 con.Output(
"Usage: stats save <path>");
101 string path = cmd[2];
103 using (StreamWriter sw =
new StreamWriter(path,
true))
105 foreach (
string line
in GetReport())
109 MainConsole.Instance.OutputFormat(
"Stats saved to file {0}", path);
112 public static void Start()
114 if (m_loggingTimer != null)
117 m_loggingTimer =
new Timer(m_statsLogIntervalMs);
118 m_loggingTimer.AutoReset =
false;
119 m_loggingTimer.Elapsed += Log;
120 m_loggingTimer.Start();
123 public static void Stop()
125 if (m_loggingTimer != null)
127 m_loggingTimer.Stop();
131 private static void Log(
object sender, ElapsedEventArgs e)
133 foreach (
string line
in GetReport())
134 m_statsLog.Info(line);
136 m_loggingTimer.Start();
139 private static List<string> GetReport()
141 List<string> lines =
new List<string>();
143 lines.Add(string.Format(
"*** STATS REPORT AT {0} ***", DateTime.Now));
145 foreach (
string report
in StatsManager.GetAllStatsReports())
System.Timers.Timer Timer