OSDN Git Service

c705682b85f4de0cec5dd3fab27291ae33f8cfd0
[opentween/open-tween.git] / OpenTween / ToolStripLabelHistory.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 // 
10 // This file is part of OpenTween.
11 // 
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 // 
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details. 
21 // 
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 using System;
28 using System.Collections.Generic;
29 using System.Linq;
30 using System.Text;
31 using System.Windows.Forms;
32
33 namespace OpenTween.OpenTweenCustomControl
34 {
35     public class ToolStripLabelHistory : ToolStripStatusLabel
36     {
37         public enum LogLevel
38         {
39             Lowest = 0,
40             Debug = 16,
41             Info = 32,
42             Notice = 64,
43             Warn = 128,
44             Err = 192,
45             Fatal = 255,
46             Highest = 256,
47         }
48
49         public class LogEntry
50         {
51             public LogLevel LogLevel { get; }
52             public DateTimeUtc Timestamp { get; }
53             public string Summary { get; }
54             public string Detail { get; }
55
56             public LogEntry(LogLevel logLevel, DateTimeUtc timestamp, string summary, string detail)
57             {
58                 this.LogLevel = logLevel;
59                 this.Timestamp = timestamp;
60                 this.Summary = summary;
61                 this.Detail = detail;
62             }
63
64             public LogEntry(DateTimeUtc timestamp, string summary) : this(LogLevel.Debug, timestamp, summary, summary)
65             {
66             }
67
68             public override string ToString()
69                 => Timestamp.ToLocalTime().ToString("T") + ": " + Summary;
70         }
71
72         readonly LinkedList<LogEntry> _logs;
73
74         const int MAXCNT = 20;
75
76         public override string Text
77         {
78             get => base.Text;
79             set
80             {
81                 _logs.AddLast(new LogEntry(DateTimeUtc.Now, value));
82                 while (_logs.Count > MAXCNT)
83                 {
84                     _logs.RemoveFirst();
85                 }
86                 base.Text = value;
87             }
88         }
89
90         public string TextHistory
91         {
92             get
93             {
94                 var sb = new StringBuilder();
95                 foreach (var e in _logs)
96                 {
97                     sb.AppendLine(e.ToString());
98                 }
99                 return sb.ToString();
100             }
101         }
102
103         public ToolStripLabelHistory()
104             => this._logs = new LinkedList<LogEntry>();
105     }
106 }