cloudflight
2023-11-23 2284ddda0e49d402ff901d6691e937af4c628858
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
 
namespace CommonBase
{
    public class Log
    {
        public static bool Enable = false;
        public static bool isAdded = false;
        public static object lockObj=new object();
        //private static StringBuilder sb = new StringBuilder();
        private static Dictionary<string, StringBuilder> sbs = new Dictionary<string, StringBuilder>();
        public static string lastTime=DateTime.Now.ToString("HH:mm:ss");
        private static Dictionary<string, List<DateTime>> ts = new Dictionary<string, List<DateTime>>();
        public static void Add(string txt,string name="default",int mode=0, int level=0)
        {
            
            if (!Enable) return;
            lock (lockObj)
            {
                isAdded = true;
                if (!sbs.ContainsKey(name))
                {
                    sbs.Add(name, new StringBuilder());
                    ts.Add(name,new List<DateTime>() { DateTime.Now });
                }
 
                var time = DateTime.Now;
                var now = time.ToString("HH:mm:ss");
                if (now != lastTime)
                {
                    sbs[name].AppendLine(now);
                }
 
                string tt = "";
                for (int i = 1;i<=level;i++)
                {
                    tt += "\t";
                    if (ts[name].Count<i+1)
                    {
                        ts[name].Add(default(DateTime));
                    }
                    if (ts[name][i] < ts[name][i-1])
                    {
                        ts[name][i] = ts[name][i-1];
                    }
                }
                string signtxt = "";
                if (mode == 1) signtxt = "{";
                else if (mode == 2)
                {
                    signtxt = "}\r\n>";
                }
                else if (level==0)
                {
                    signtxt = ":";
                }
                sbs[name].AppendLine($"{signtxt}{tt}{time.ToString("fff")}\t{txt}\t({(time - ts[name][level]).TotalMilliseconds})");
                lastTime = now;
                ts[name][level] = time;
            }           
        }
        public static void Clear()
        {
            sbs.Clear();
            ts.Clear();
        }
        //private static string Text { get { return sb.ToString(); } }
 
        public static void Output()
        {
            if (!Enable) return;
            lock (lockObj)
            {
                foreach (var key in sbs.Keys)
                {
                    var name = key == "default" ? "" : key;
                    var file = $@"Log\log_{name}.lua";
                    //if (File.Exists(file)) File.Copy(file, $@"Log\log_{name}_bk_{DateTime.Now.ToString("yyyyMMddHHmmss")}.lua", true);
                    if (!Directory.Exists("Log\\")) Directory.CreateDirectory("Log\\");
 
                    StreamWriter sw = null;
                    try
                    {
                        sw = new StreamWriter(file);
                        sw.WriteLine(sbs[key].ToString());
                        sw.Close();
                    }
                    catch
                    {
                        if (sw != null) sw.Close();
                    }
                }
            }
        }
 
        public static void OpenFile()
        {
            foreach (var key in sbs.Keys)
            {
                var name = key == "default" ? "" : key;
                var file = $@"Log\log_{name}.lua";
                
                StreamWriter sw = null;
                try
                {
                    System.Diagnostics.Process.Start(file);
                }
                catch
                {
                    
                }
            }
        }
    }
   
}