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 15 kB

9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. I18N.TranslateForm(this);
  131. trafficChart.Series["Inbound"].LegendText = I18N.GetString("Inbound");
  132. trafficChart.Series["Outbound"].LegendText = I18N.GetString("Outbound");
  133. }
  134. private void Timer_Tick(object sender, EventArgs e)
  135. {
  136. UpdateContent();
  137. UpdateTrafficChart();
  138. }
  139. private void InitContent()
  140. {
  141. using (StreamReader reader = new StreamReader(new FileStream(filename,
  142. FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
  143. {
  144. if (reader.BaseStream.Length > BACK_OFFSET)
  145. {
  146. reader.BaseStream.Seek(-BACK_OFFSET, SeekOrigin.End);
  147. reader.ReadLine();
  148. }
  149. string line = "";
  150. StringBuilder appendText = new StringBuilder(1024);
  151. while ((line = reader.ReadLine()) != null)
  152. appendText.Append(line + Environment.NewLine);
  153. LogMessageTextBox.AppendText(appendText.ToString());
  154. LogMessageTextBox.ScrollToCaret();
  155. lastOffset = reader.BaseStream.Position;
  156. }
  157. }
  158. private void UpdateContent()
  159. {
  160. try
  161. {
  162. using (StreamReader reader = new StreamReader(new FileStream(filename,
  163. FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
  164. {
  165. reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin);
  166. string line = "";
  167. bool changed = false;
  168. StringBuilder appendText = new StringBuilder(128);
  169. while ((line = reader.ReadLine()) != null)
  170. {
  171. changed = true;
  172. appendText.Append(line + Environment.NewLine);
  173. }
  174. if (changed)
  175. {
  176. LogMessageTextBox.AppendText(appendText.ToString());
  177. LogMessageTextBox.ScrollToCaret();
  178. }
  179. lastOffset = reader.BaseStream.Position;
  180. }
  181. }
  182. catch (FileNotFoundException)
  183. {
  184. }
  185. this.Text = I18N.GetString("Log Viewer") +
  186. $" [in: {Utils.FormatBytes(controller.InboundCounter)}, out: {Utils.FormatBytes(controller.OutboundCounter)}]";
  187. }
  188. private void LogForm_Load(object sender, EventArgs e)
  189. {
  190. InitContent();
  191. timer = new Timer();
  192. timer.Interval = 100;
  193. timer.Tick += Timer_Tick;
  194. timer.Start();
  195. LogViewerConfig config = controller.GetConfigurationCopy().logViewer;
  196. Height = config.Height;
  197. Width = config.Width;
  198. Top = config.BestTop;
  199. Left = config.BestLeft;
  200. if (config.Maximized)
  201. {
  202. WindowState = FormWindowState.Maximized;
  203. }
  204. topMostTriggerLock = true;
  205. TopMost = TopMostMenuItem.Checked = TopMostCheckBox.Checked = topMostTrigger;
  206. topMostTriggerLock = false;
  207. wrapTextTriggerLock = true;
  208. LogMessageTextBox.WordWrap = WrapTextMenuItem.Checked = WrapTextCheckBox.Checked = wrapTextTrigger;
  209. wrapTextTriggerLock = false;
  210. ToolbarFlowLayoutPanel.Visible = ShowToolbarMenuItem.Checked = toolbarTrigger;
  211. }
  212. private void LogForm_FormClosing(object sender, FormClosingEventArgs e)
  213. {
  214. timer.Stop();
  215. controller.TrafficChanged -= controller_TrafficChanged;
  216. LogViewerConfig config = controller.GetConfigurationCopy().logViewer;
  217. config.topMost = topMostTrigger;
  218. config.wrapText = wrapTextTrigger;
  219. config.toolbarShown = toolbarTrigger;
  220. config.Font = LogMessageTextBox.Font;
  221. config.BackgroundColor = LogMessageTextBox.BackColor;
  222. config.TextColor = LogMessageTextBox.ForeColor;
  223. if (WindowState != FormWindowState.Minimized && !(config.Maximized = WindowState == FormWindowState.Maximized))
  224. {
  225. config.Top = Top;
  226. config.Left = Left;
  227. config.Height = Height;
  228. config.Width = Width;
  229. }
  230. controller.SaveLogViewerConfig(config);
  231. }
  232. private void OpenLocationMenuItem_Click(object sender, EventArgs e)
  233. {
  234. string argument = "/select, \"" + filename + "\"";
  235. Logging.Debug(argument);
  236. System.Diagnostics.Process.Start("explorer.exe", argument);
  237. }
  238. private void ExitMenuItem_Click(object sender, EventArgs e)
  239. {
  240. Close();
  241. }
  242. private void LogForm_Shown(object sender, EventArgs e)
  243. {
  244. LogMessageTextBox.ScrollToCaret();
  245. }
  246. #region Clean up the content in LogMessageTextBox.
  247. private void DoClearLogs()
  248. {
  249. Logging.Clear();
  250. lastOffset = 0;
  251. LogMessageTextBox.Clear();
  252. }
  253. private void ClearLogsMenuItem_Click(object sender, EventArgs e)
  254. {
  255. DoClearLogs();
  256. }
  257. private void ClearLogsButton_Click(object sender, EventArgs e)
  258. {
  259. DoClearLogs();
  260. }
  261. #endregion
  262. #region Change the font settings applied in LogMessageTextBox.
  263. private void DoChangeFont()
  264. {
  265. try
  266. {
  267. FontDialog fd = new FontDialog();
  268. fd.Font = LogMessageTextBox.Font;
  269. if (fd.ShowDialog() == DialogResult.OK)
  270. {
  271. LogMessageTextBox.Font = new Font(fd.Font.FontFamily, fd.Font.Size, fd.Font.Style);
  272. }
  273. }
  274. catch (Exception ex)
  275. {
  276. Logging.LogUsefulException(ex);
  277. MessageBox.Show(ex.Message);
  278. }
  279. }
  280. private void ChangeFontMenuItem_Click(object sender, EventArgs e)
  281. {
  282. DoChangeFont();
  283. }
  284. private void ChangeFontButton_Click(object sender, EventArgs e)
  285. {
  286. DoChangeFont();
  287. }
  288. #endregion
  289. #region Trigger the log messages to wrapable, or not.
  290. bool wrapTextTrigger = false;
  291. bool wrapTextTriggerLock = false;
  292. private void TriggerWrapText()
  293. {
  294. wrapTextTriggerLock = true;
  295. wrapTextTrigger = !wrapTextTrigger;
  296. LogMessageTextBox.WordWrap = wrapTextTrigger;
  297. LogMessageTextBox.ScrollToCaret();
  298. WrapTextMenuItem.Checked = WrapTextCheckBox.Checked = wrapTextTrigger;
  299. wrapTextTriggerLock = false;
  300. }
  301. private void WrapTextMenuItem_Click(object sender, EventArgs e)
  302. {
  303. if (!wrapTextTriggerLock)
  304. {
  305. TriggerWrapText();
  306. }
  307. }
  308. private void WrapTextCheckBox_CheckedChanged(object sender, EventArgs e)
  309. {
  310. if (!wrapTextTriggerLock)
  311. {
  312. TriggerWrapText();
  313. }
  314. }
  315. #endregion
  316. #region Trigger the window to top most, or not.
  317. bool topMostTrigger = false;
  318. bool topMostTriggerLock = false;
  319. private void TriggerTopMost()
  320. {
  321. topMostTriggerLock = true;
  322. topMostTrigger = !topMostTrigger;
  323. TopMost = topMostTrigger;
  324. TopMostMenuItem.Checked = TopMostCheckBox.Checked = topMostTrigger;
  325. topMostTriggerLock = false;
  326. }
  327. private void TopMostCheckBox_CheckedChanged(object sender, EventArgs e)
  328. {
  329. if (!topMostTriggerLock)
  330. {
  331. TriggerTopMost();
  332. }
  333. }
  334. private void TopMostMenuItem_Click(object sender, EventArgs e)
  335. {
  336. if (!topMostTriggerLock)
  337. {
  338. TriggerTopMost();
  339. }
  340. }
  341. #endregion
  342. private bool toolbarTrigger = false;
  343. private void ShowToolbarMenuItem_Click(object sender, EventArgs e)
  344. {
  345. toolbarTrigger = !toolbarTrigger;
  346. ToolbarFlowLayoutPanel.Visible = toolbarTrigger;
  347. ShowToolbarMenuItem.Checked = toolbarTrigger;
  348. }
  349. private class TrafficInfo
  350. {
  351. public long inbound;
  352. public long outbound;
  353. public TrafficInfo(long inbound, long outbound)
  354. {
  355. this.inbound = inbound;
  356. this.outbound = outbound;
  357. }
  358. }
  359. }
  360. }