123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using System.Threading;
- using System.Windows.Forms;
- using System.Drawing;
- namespace _Log
- {
- public class Log
- {
- TextBox tbLog;
- public Log(TextBox aTbLog)
- {
- tbLog = aTbLog;
- tbLog.WordWrap = false;
- tbLog.Multiline = true;
- tbLog.ScrollBars = ScrollBars.Both;
- tbLog.Font = new Font("Consolas", 14);
- }
- public void l(string s, string from = null)
- {
- string s2 = Thread.CurrentThread.IsBackground ? "background" : "UI thread";
- s = $"{DateTime.Now.ToString("HH:mm:ss")}{" " + from} ({s2}) {s}\r\n";
- if (!tbLog.InvokeRequired)
- tbLog.AppendText(s);
- else
- tbLog.Invoke(new Action(() => tbLog.AppendText(s)));
- }
-
- public static Log cli, srv;
- public Log(TextBox aTbLogSrv, TextBox aTbLogCli)
- {
- srv = new Log(aTbLogSrv);
- cli = new Log(aTbLogCli);
- }
- public static void S(string s)
- {
- srv?.l(s); // to jest równoważne: if (srv != null) srv.l(s);
- }
- public static void Clear()
- {
- srv?.tbLog.Clear();
- cli?.tbLog.Clear();
- }
- public static void C(string s)
- {
- cli?.l(s);
- }
- public static readonly string HR = $"\r\n{new string('—', 100)} ";
- public static void B(string s)
- {
- cli?.l(s);
- }
- public static void L(string s)
- {
- srv?.l(s);
- }
-
- }
- }
|