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

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
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Shadowsocks.Controller;
  2. using Shadowsocks.Properties;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. namespace Shadowsocks.View
  13. {
  14. public partial class LogForm : Form
  15. {
  16. long lastOffset;
  17. string filename;
  18. Timer timer;
  19. const int BACK_OFFSET = 65536;
  20. public LogForm(string filename)
  21. {
  22. this.filename = filename;
  23. InitializeComponent();
  24. this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  25. UpdateTexts();
  26. }
  27. private void UpdateTexts()
  28. {
  29. FileMenuItem.Text = I18N.GetString("&File");
  30. OpenLocationMenuItem.Text = I18N.GetString("&Open Location");
  31. ExitMenuItem.Text = I18N.GetString("E&xit");
  32. CleanLogsButton.Text = I18N.GetString("&Clean logs");
  33. ChangeFontButton.Text = I18N.GetString("&Font");
  34. WrapTextCheckBox.Text = I18N.GetString("&Wrap text");
  35. TopMostCheckBox.Text = I18N.GetString("&Top most");
  36. this.Text = I18N.GetString("Log Viewer");
  37. }
  38. private void Timer_Tick(object sender, EventArgs e)
  39. {
  40. UpdateContent();
  41. }
  42. private void InitContent()
  43. {
  44. using (StreamReader reader = new StreamReader(new FileStream(filename,
  45. FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
  46. {
  47. if (reader.BaseStream.Length > BACK_OFFSET)
  48. {
  49. reader.BaseStream.Seek(-BACK_OFFSET, SeekOrigin.End);
  50. reader.ReadLine();
  51. }
  52. string line = "";
  53. while ((line = reader.ReadLine()) != null)
  54. LogMessageTextBox.AppendText(line + "\r\n");
  55. LogMessageTextBox.ScrollToCaret();
  56. lastOffset = reader.BaseStream.Position;
  57. }
  58. }
  59. private void UpdateContent()
  60. {
  61. using (StreamReader reader = new StreamReader(new FileStream(filename,
  62. FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
  63. {
  64. reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin);
  65. string line = "";
  66. bool changed = false;
  67. while ((line = reader.ReadLine()) != null)
  68. {
  69. changed = true;
  70. LogMessageTextBox.AppendText(line + "\r\n");
  71. }
  72. if (changed)
  73. {
  74. LogMessageTextBox.ScrollToCaret();
  75. }
  76. lastOffset = reader.BaseStream.Position;
  77. }
  78. }
  79. private void LogForm_Load(object sender, EventArgs e)
  80. {
  81. InitContent();
  82. timer = new Timer();
  83. timer.Interval = 300;
  84. timer.Tick += Timer_Tick;
  85. timer.Start();
  86. }
  87. private void LogForm_FormClosing(object sender, FormClosingEventArgs e)
  88. {
  89. timer.Stop();
  90. }
  91. private void OpenLocationMenuItem_Click(object sender, EventArgs e)
  92. {
  93. string argument = "/select, \"" + filename + "\"";
  94. Console.WriteLine(argument);
  95. System.Diagnostics.Process.Start("explorer.exe", argument);
  96. }
  97. private void ExitMenuItem_Click(object sender, EventArgs e)
  98. {
  99. this.Close();
  100. }
  101. private void LogForm_Shown(object sender, EventArgs e)
  102. {
  103. LogMessageTextBox.ScrollToCaret();
  104. }
  105. private void WrapTextCheckBox_CheckedChanged(object sender, EventArgs e)
  106. {
  107. LogMessageTextBox.WordWrap = WrapTextCheckBox.Checked;
  108. LogMessageTextBox.ScrollToCaret();
  109. }
  110. private void CleanLogsButton_Click(object sender, EventArgs e)
  111. {
  112. LogMessageTextBox.Clear();
  113. }
  114. private void ChangeFontButton_Click(object sender, EventArgs e)
  115. {
  116. FontDialog fd = new FontDialog();
  117. fd.Font = LogMessageTextBox.Font;
  118. if (fd.ShowDialog() == DialogResult.OK)
  119. {
  120. LogMessageTextBox.Font = fd.Font;
  121. }
  122. }
  123. private void TopMostCheckBox_CheckedChanged(object sender, EventArgs e)
  124. {
  125. this.TopMost = TopMostCheckBox.Checked;
  126. }
  127. }
  128. }