You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LogForm.cs 16 kB

10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using System.Windows.Forms.DataVisualization.Charting;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using Shadowsocks.Controller;
  9. using Shadowsocks.Properties;
  10. using Shadowsocks.Model;
  11. using Shadowsocks.Util;
  12. using System.Text;
  13. namespace Shadowsocks.View
  14. {
  15. public partial class LogForm : Form
  16. {
  17. long lastOffset;
  18. string filename;
  19. Timer timer;
  20. const int BACK_OFFSET = 65536;
  21. ShadowsocksController controller;
  22. // global traffic update lock, make it static
  23. private static readonly object _lock = new object();
  24. #region Traffic Chart
  25. Queue<TrafficInfo> trafficInfoQueue = new Queue<TrafficInfo>();
  26. const int queueMaxLength = 60;
  27. long lastInbound, lastOutbound;
  28. long maxSpeed = 0, lastMaxSpeed = 0;
  29. const long minScale = 50;
  30. BandwidthScaleInfo bandwidthScale;
  31. List<float> inboundPoints = new List<float>();
  32. List<float> outboundPoints = new List<float>();
  33. TextAnnotation inboundAnnotation = new TextAnnotation();
  34. TextAnnotation outboundAnnotation = new TextAnnotation();
  35. #endregion
  36. public LogForm(ShadowsocksController controller, string filename)
  37. {
  38. this.controller = controller;
  39. this.filename = filename;
  40. InitializeComponent();
  41. Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  42. LogViewerConfig config = controller.GetConfigurationCopy().logViewer;
  43. topMostTrigger = config.topMost;
  44. wrapTextTrigger = config.wrapText;
  45. toolbarTrigger = config.toolbarShown;
  46. LogMessageTextBox.BackColor = config.BackgroundColor;
  47. LogMessageTextBox.ForeColor = config.TextColor;
  48. LogMessageTextBox.Font = config.Font;
  49. controller.TrafficChanged += controller_TrafficChanged;
  50. UpdateTexts();
  51. }
  52. private void UpdateTrafficChart()
  53. {
  54. lock (_lock)
  55. {
  56. if (trafficInfoQueue.Count == 0)
  57. return;
  58. inboundPoints.Clear();
  59. outboundPoints.Clear();
  60. maxSpeed = 0;
  61. foreach (var trafficInfo in trafficInfoQueue)
  62. {
  63. inboundPoints.Add(trafficInfo.inbound);
  64. outboundPoints.Add(trafficInfo.outbound);
  65. maxSpeed = Math.Max(maxSpeed, Math.Max(trafficInfo.inbound, trafficInfo.outbound));
  66. }
  67. lastInbound = trafficInfoQueue.Last().inbound;
  68. lastOutbound = trafficInfoQueue.Last().outbound;
  69. }
  70. if (maxSpeed > 0)
  71. {
  72. lastMaxSpeed -= lastMaxSpeed / 32;
  73. maxSpeed = Math.Max(minScale, Math.Max(maxSpeed, lastMaxSpeed));
  74. lastMaxSpeed = maxSpeed;
  75. }
  76. else
  77. {
  78. maxSpeed = lastMaxSpeed = minScale;
  79. }
  80. bandwidthScale = Utils.GetBandwidthScale(maxSpeed);
  81. // re-scale the original data points, since it is List<float>, .ForEach does not work
  82. inboundPoints = inboundPoints.Select(p => p / bandwidthScale.unit).ToList();
  83. outboundPoints = outboundPoints.Select(p => p / bandwidthScale.unit).ToList();
  84. if (trafficChart.IsHandleCreated)
  85. {
  86. trafficChart.Series["Inbound"].Points.DataBindY(inboundPoints);
  87. trafficChart.Series["Outbound"].Points.DataBindY(outboundPoints);
  88. trafficChart.ChartAreas[0].AxisY.LabelStyle.Format = "{0:0.##} " + bandwidthScale.unitName;
  89. trafficChart.ChartAreas[0].AxisY.Maximum = bandwidthScale.value;
  90. inboundAnnotation.AnchorDataPoint = trafficChart.Series["Inbound"].Points.Last();
  91. inboundAnnotation.Text = Utils.FormatBandwidth(lastInbound);
  92. outboundAnnotation.AnchorDataPoint = trafficChart.Series["Outbound"].Points.Last();
  93. outboundAnnotation.Text = Utils.FormatBandwidth(lastOutbound);
  94. trafficChart.Annotations.Clear();
  95. trafficChart.Annotations.Add(inboundAnnotation);
  96. trafficChart.Annotations.Add(outboundAnnotation);
  97. }
  98. }
  99. private void controller_TrafficChanged(object sender, EventArgs e)
  100. {
  101. lock (_lock)
  102. {
  103. if (trafficInfoQueue.Count == 0)
  104. {
  105. // Init an empty queue
  106. for (int i = 0; i < queueMaxLength; i++)
  107. {
  108. trafficInfoQueue.Enqueue(new TrafficInfo(0, 0));
  109. }
  110. foreach (var trafficPerSecond in controller.trafficPerSecondQueue)
  111. {
  112. trafficInfoQueue.Enqueue(new TrafficInfo(trafficPerSecond.inboundIncreasement,
  113. trafficPerSecond.outboundIncreasement));
  114. if (trafficInfoQueue.Count > queueMaxLength)
  115. trafficInfoQueue.Dequeue();
  116. }
  117. }
  118. else
  119. {
  120. var lastTraffic = controller.trafficPerSecondQueue.Last();
  121. trafficInfoQueue.Enqueue(new TrafficInfo(lastTraffic.inboundIncreasement,
  122. lastTraffic.outboundIncreasement));
  123. if (trafficInfoQueue.Count > queueMaxLength)
  124. trafficInfoQueue.Dequeue();
  125. }
  126. }
  127. }
  128. private void UpdateTexts()
  129. {
  130. FileMenuItem.Text = I18N.GetString("&File");
  131. OpenLocationMenuItem.Text = I18N.GetString("&Open Location");
  132. ExitMenuItem.Text = I18N.GetString("E&xit");
  133. ClearLogsButton.Text = I18N.GetString("&Clear Logs");
  134. ChangeFontButton.Text = I18N.GetString("Change &Font");
  135. WrapTextCheckBox.Text = I18N.GetString("&Wrap Text");
  136. TopMostCheckBox.Text = I18N.GetString("&Top Most");
  137. ViewMenuItem.Text = I18N.GetString("&View");
  138. ClearLogsMenuItem.Text = I18N.GetString("&Clear Logs");
  139. ChangeFontMenuItem.Text = I18N.GetString("Change &Font");
  140. WrapTextMenuItem.Text = I18N.GetString("&Wrap Text");
  141. TopMostMenuItem.Text = I18N.GetString("&Top Most");
  142. ShowToolbarMenuItem.Text = I18N.GetString("&Show Toolbar");
  143. Text = I18N.GetString("Log Viewer");
  144. // traffic chart
  145. trafficChart.Series["Inbound"].LegendText = I18N.GetString("Inbound");
  146. trafficChart.Series["Outbound"].LegendText = I18N.GetString("Outbound");
  147. }
  148. private void Timer_Tick(object sender, EventArgs e)
  149. {
  150. UpdateContent();
  151. UpdateTrafficChart();
  152. }
  153. private void InitContent()
  154. {
  155. using (StreamReader reader = new StreamReader(new FileStream(filename,
  156. FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
  157. {
  158. if (reader.BaseStream.Length > BACK_OFFSET)
  159. {
  160. reader.BaseStream.Seek(-BACK_OFFSET, SeekOrigin.End);
  161. reader.ReadLine();
  162. }
  163. string line = "";
  164. StringBuilder appendText = new StringBuilder(1024);
  165. while ((line = reader.ReadLine()) != null)
  166. appendText.Append(line + Environment.NewLine);
  167. LogMessageTextBox.AppendText(appendText.ToString());
  168. LogMessageTextBox.ScrollToCaret();
  169. lastOffset = reader.BaseStream.Position;
  170. }
  171. }
  172. private void UpdateContent()
  173. {
  174. try
  175. {
  176. using (StreamReader reader = new StreamReader(new FileStream(filename,
  177. FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
  178. {
  179. reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin);
  180. string line = "";
  181. bool changed = false;
  182. StringBuilder appendText = new StringBuilder(128);
  183. while ((line = reader.ReadLine()) != null)
  184. {
  185. changed = true;
  186. appendText.Append(line + Environment.NewLine);
  187. }
  188. if (changed)
  189. {
  190. LogMessageTextBox.AppendText(appendText.ToString());
  191. LogMessageTextBox.ScrollToCaret();
  192. }
  193. lastOffset = reader.BaseStream.Position;
  194. }
  195. }
  196. catch (FileNotFoundException)
  197. {
  198. }
  199. this.Text = I18N.GetString("Log Viewer") +
  200. $" [in: {Utils.FormatBytes(controller.InboundCounter)}, out: {Utils.FormatBytes(controller.OutboundCounter)}]";
  201. }
  202. private void LogForm_Load(object sender, EventArgs e)
  203. {
  204. InitContent();
  205. timer = new Timer();
  206. timer.Interval = 100;
  207. timer.Tick += Timer_Tick;
  208. timer.Start();
  209. LogViewerConfig config = controller.GetConfigurationCopy().logViewer;
  210. Height = config.Height;
  211. Width = config.Width;
  212. Top = config.BestTop;
  213. Left = config.BestLeft;
  214. if (config.Maximized)
  215. {
  216. WindowState = FormWindowState.Maximized;
  217. }
  218. topMostTriggerLock = true;
  219. TopMost = TopMostMenuItem.Checked = TopMostCheckBox.Checked = topMostTrigger;
  220. topMostTriggerLock = false;
  221. wrapTextTriggerLock = true;
  222. LogMessageTextBox.WordWrap = WrapTextMenuItem.Checked = WrapTextCheckBox.Checked = wrapTextTrigger;
  223. wrapTextTriggerLock = false;
  224. ToolbarFlowLayoutPanel.Visible = ShowToolbarMenuItem.Checked = toolbarTrigger;
  225. }
  226. private void LogForm_FormClosing(object sender, FormClosingEventArgs e)
  227. {
  228. timer.Stop();
  229. controller.TrafficChanged -= controller_TrafficChanged;
  230. LogViewerConfig config = controller.GetConfigurationCopy().logViewer;
  231. config.topMost = topMostTrigger;
  232. config.wrapText = wrapTextTrigger;
  233. config.toolbarShown = toolbarTrigger;
  234. config.Font = LogMessageTextBox.Font;
  235. config.BackgroundColor = LogMessageTextBox.BackColor;
  236. config.TextColor = LogMessageTextBox.ForeColor;
  237. if (WindowState != FormWindowState.Minimized && !(config.Maximized = WindowState == FormWindowState.Maximized))
  238. {
  239. config.Top = Top;
  240. config.Left = Left;
  241. config.Height = Height;
  242. config.Width = Width;
  243. }
  244. controller.SaveLogViewerConfig(config);
  245. }
  246. private void OpenLocationMenuItem_Click(object sender, EventArgs e)
  247. {
  248. string argument = "/select, \"" + filename + "\"";
  249. Logging.Debug(argument);
  250. System.Diagnostics.Process.Start("explorer.exe", argument);
  251. }
  252. private void ExitMenuItem_Click(object sender, EventArgs e)
  253. {
  254. Close();
  255. }
  256. private void LogForm_Shown(object sender, EventArgs e)
  257. {
  258. LogMessageTextBox.ScrollToCaret();
  259. }
  260. #region Clean up the content in LogMessageTextBox.
  261. private void DoClearLogs()
  262. {
  263. Logging.Clear();
  264. lastOffset = 0;
  265. LogMessageTextBox.Clear();
  266. }
  267. private void ClearLogsMenuItem_Click(object sender, EventArgs e)
  268. {
  269. DoClearLogs();
  270. }
  271. private void ClearLogsButton_Click(object sender, EventArgs e)
  272. {
  273. DoClearLogs();
  274. }
  275. #endregion
  276. #region Change the font settings applied in LogMessageTextBox.
  277. private void DoChangeFont()
  278. {
  279. try
  280. {
  281. FontDialog fd = new FontDialog();
  282. fd.Font = LogMessageTextBox.Font;
  283. if (fd.ShowDialog() == DialogResult.OK)
  284. {
  285. LogMessageTextBox.Font = new Font(fd.Font.FontFamily, fd.Font.Size, fd.Font.Style);
  286. }
  287. }
  288. catch (Exception ex)
  289. {
  290. Logging.LogUsefulException(ex);
  291. MessageBox.Show(ex.Message);
  292. }
  293. }
  294. private void ChangeFontMenuItem_Click(object sender, EventArgs e)
  295. {
  296. DoChangeFont();
  297. }
  298. private void ChangeFontButton_Click(object sender, EventArgs e)
  299. {
  300. DoChangeFont();
  301. }
  302. #endregion
  303. #region Trigger the log messages to wrapable, or not.
  304. bool wrapTextTrigger = false;
  305. bool wrapTextTriggerLock = false;
  306. private void TriggerWrapText()
  307. {
  308. wrapTextTriggerLock = true;
  309. wrapTextTrigger = !wrapTextTrigger;
  310. LogMessageTextBox.WordWrap = wrapTextTrigger;
  311. LogMessageTextBox.ScrollToCaret();
  312. WrapTextMenuItem.Checked = WrapTextCheckBox.Checked = wrapTextTrigger;
  313. wrapTextTriggerLock = false;
  314. }
  315. private void WrapTextMenuItem_Click(object sender, EventArgs e)
  316. {
  317. if (!wrapTextTriggerLock)
  318. {
  319. TriggerWrapText();
  320. }
  321. }
  322. private void WrapTextCheckBox_CheckedChanged(object sender, EventArgs e)
  323. {
  324. if (!wrapTextTriggerLock)
  325. {
  326. TriggerWrapText();
  327. }
  328. }
  329. #endregion
  330. #region Trigger the window to top most, or not.
  331. bool topMostTrigger = false;
  332. bool topMostTriggerLock = false;
  333. private void TriggerTopMost()
  334. {
  335. topMostTriggerLock = true;
  336. topMostTrigger = !topMostTrigger;
  337. TopMost = topMostTrigger;
  338. TopMostMenuItem.Checked = TopMostCheckBox.Checked = topMostTrigger;
  339. topMostTriggerLock = false;
  340. }
  341. private void TopMostCheckBox_CheckedChanged(object sender, EventArgs e)
  342. {
  343. if (!topMostTriggerLock)
  344. {
  345. TriggerTopMost();
  346. }
  347. }
  348. private void TopMostMenuItem_Click(object sender, EventArgs e)
  349. {
  350. if (!topMostTriggerLock)
  351. {
  352. TriggerTopMost();
  353. }
  354. }
  355. #endregion
  356. private bool toolbarTrigger = false;
  357. private void ShowToolbarMenuItem_Click(object sender, EventArgs e)
  358. {
  359. toolbarTrigger = !toolbarTrigger;
  360. ToolbarFlowLayoutPanel.Visible = toolbarTrigger;
  361. ShowToolbarMenuItem.Checked = toolbarTrigger;
  362. }
  363. private class TrafficInfo
  364. {
  365. public long inbound;
  366. public long outbound;
  367. public TrafficInfo(long inbound, long outbound)
  368. {
  369. this.inbound = inbound;
  370. this.outbound = outbound;
  371. }
  372. }
  373. }
  374. }