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

11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Shadowsocks.Properties;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace Shadowsocks.View
  12. {
  13. public partial class LogForm : Form
  14. {
  15. long lastOffset;
  16. string filename;
  17. Timer timer;
  18. const int BACK_OFFSET = 65536;
  19. public LogForm(string filename)
  20. {
  21. this.filename = filename;
  22. InitializeComponent();
  23. this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  24. }
  25. private void Timer_Tick(object sender, EventArgs e)
  26. {
  27. UpdateContent();
  28. }
  29. private void InitContent()
  30. {
  31. using (StreamReader reader = new StreamReader(new FileStream(filename,
  32. FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
  33. {
  34. if (reader.BaseStream.Length > BACK_OFFSET)
  35. {
  36. reader.BaseStream.Seek(-BACK_OFFSET, SeekOrigin.End);
  37. reader.ReadLine();
  38. }
  39. string line = "";
  40. while ((line = reader.ReadLine()) != null)
  41. textBox1.AppendText(line + "\r\n");
  42. textBox1.ScrollToCaret();
  43. lastOffset = reader.BaseStream.Position;
  44. }
  45. }
  46. private void UpdateContent()
  47. {
  48. using (StreamReader reader = new StreamReader(new FileStream(filename,
  49. FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
  50. {
  51. reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin);
  52. string line = "";
  53. bool changed = false;
  54. while ((line = reader.ReadLine()) != null)
  55. {
  56. changed = true;
  57. textBox1.AppendText(line + "\r\n");
  58. }
  59. if (changed)
  60. {
  61. textBox1.ScrollToCaret();
  62. }
  63. lastOffset = reader.BaseStream.Position;
  64. }
  65. }
  66. private void LogForm_Load(object sender, EventArgs e)
  67. {
  68. InitContent();
  69. timer = new Timer();
  70. timer.Interval = 300;
  71. timer.Tick += Timer_Tick;
  72. timer.Start();
  73. }
  74. private void LogForm_FormClosing(object sender, FormClosingEventArgs e)
  75. {
  76. timer.Stop();
  77. }
  78. private void menuItem2_Click(object sender, EventArgs e)
  79. {
  80. string argument = @"/select, " + filename;
  81. System.Diagnostics.Process.Start("explorer.exe", argument);
  82. }
  83. private void menuItem3_Click(object sender, EventArgs e)
  84. {
  85. }
  86. private void menuItem4_Click(object sender, EventArgs e)
  87. {
  88. this.Close();
  89. }
  90. private void LogForm_Shown(object sender, EventArgs e)
  91. {
  92. textBox1.ScrollToCaret();
  93. }
  94. }
  95. }