Log.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Threading;
  3. using System.Windows.Forms;
  4. using System.Drawing;
  5. namespace _Log
  6. {
  7. public class Log
  8. {
  9. TextBox tbLog;
  10. public Log(TextBox aTbLog)
  11. {
  12. tbLog = aTbLog;
  13. tbLog.WordWrap = false;
  14. tbLog.Multiline = true;
  15. tbLog.ScrollBars = ScrollBars.Both;
  16. tbLog.Font = new Font("Consolas", 14);
  17. }
  18. public void l(string s, string from = null)
  19. {
  20. string s2 = Thread.CurrentThread.IsBackground ? "background" : "UI thread";
  21. s = $"{DateTime.Now.ToString("HH:mm:ss")}{" " + from} ({s2}) {s}\r\n";
  22. if (!tbLog.InvokeRequired)
  23. tbLog.AppendText(s);
  24. else
  25. tbLog.Invoke(new Action(() => tbLog.AppendText(s)));
  26. }
  27. public static Log cli, srv;
  28. public Log(TextBox aTbLogSrv, TextBox aTbLogCli)
  29. {
  30. srv = new Log(aTbLogSrv);
  31. cli = new Log(aTbLogCli);
  32. }
  33. public static void S(string s)
  34. {
  35. srv?.l(s); // to jest równoważne: if (srv != null) srv.l(s);
  36. }
  37. public static void Clear()
  38. {
  39. srv?.tbLog.Clear();
  40. cli?.tbLog.Clear();
  41. }
  42. public static void C(string s)
  43. {
  44. cli?.l(s);
  45. }
  46. public static readonly string HR = $"\r\n{new string('—', 100)} ";
  47. public static void B(string s)
  48. {
  49. cli?.l(s);
  50. }
  51. public static void L(string s)
  52. {
  53. srv?.l(s);
  54. }
  55. }
  56. }