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

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