ConsoleOutput.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using CommonLang.Log;
  9. using System.IO;
  10. using CommonLang;
  11. using Microsoft.Win32;
  12. namespace CommonFroms.Utils
  13. {
  14. public partial class ConsoleOutput : Form
  15. {
  16. private MyLogger log;
  17. private LoggerFactory old_factory;
  18. private Form m_DockTo;
  19. private Form m_OldActive;
  20. public bool IsDock
  21. {
  22. get { return toolDock.Checked; }
  23. }
  24. public Form DockTo
  25. {
  26. get { return m_DockTo; }
  27. set
  28. {
  29. m_DockTo = value;
  30. }
  31. }
  32. public ConsoleOutput(string name="Output")
  33. {
  34. InitializeComponent();
  35. bool dock = true;
  36. if (RegistUtils.TryGetAppRegistry("ConsoleOutput.Dock", out dock))
  37. {
  38. this.toolDock.Checked = dock;
  39. }
  40. this.old_factory = LoggerFactory.CurrentFactory;
  41. this.log = new MyLogger(name);
  42. LoggerFactory.SetFactory(new MyLoggerFactory());
  43. this.Disposed += ConsoleOutput_Disposed;
  44. }
  45. private void timer1_Tick(object sender, EventArgs e)
  46. {
  47. string text = MyLogger.GetLines();
  48. if (!string.IsNullOrEmpty(text))
  49. {
  50. this.textBox1.AppendText(text);
  51. }
  52. if (this.IsDock && m_DockTo != null)
  53. {
  54. var dsize = m_DockTo.Bounds;
  55. this.Bounds = new System.Drawing.Rectangle(
  56. dsize.X, dsize.Y + dsize.Height,
  57. dsize.Width, this.Height);
  58. if (m_DockTo == Form.ActiveForm)
  59. {
  60. if (m_OldActive != Form.ActiveForm)
  61. {
  62. this.Activate();
  63. m_DockTo.Activate();
  64. }
  65. }
  66. m_OldActive = Form.ActiveForm;
  67. }
  68. }
  69. private void ConsoleOutput_Disposed(object sender, EventArgs e)
  70. {
  71. RegistUtils.PutAppRegistry("ConsoleOutput.Dock", this.toolDock.Checked);
  72. LoggerFactory.SetFactory(old_factory);
  73. }
  74. public Logger GetLogger()
  75. {
  76. return log;
  77. }
  78. internal class MyLoggerFactory : LoggerFactory
  79. {
  80. override protected Logger CreateLogger(string name)
  81. {
  82. return new MyLogger(name);
  83. }
  84. }
  85. internal class MyLogger : Logger
  86. {
  87. static public List<string> lines = new List<string>();
  88. public MyLogger(string name)
  89. {
  90. base.SetName(name);
  91. }
  92. protected override void Print(string text)
  93. {
  94. lock (lines)
  95. {
  96. lines.Add(mName + " - " + text);
  97. }
  98. }
  99. static public string GetLines()
  100. {
  101. StringBuilder sb = new StringBuilder();
  102. lock (lines)
  103. {
  104. foreach (string line in lines)
  105. {
  106. sb.AppendLine(line);
  107. }
  108. lines.Clear();
  109. }
  110. return sb.ToString();
  111. }
  112. }
  113. }
  114. }