forked from zorkian/dtorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msglist.cpp
126 lines (107 loc) · 2.52 KB
/
msglist.cpp
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
123
124
125
#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include "msglist.h"
#include "bttime.h"
#include "btconfig.h"
#if !defined(HAVE_VSNPRINTF)
#include "compat.h"
#endif
const dt_message *MessageList::GetBrief() const
{
dt_message *p, *first = m_firstmsg;
int count = 0;
for( p = m_firstmsg; p; p = p->next ){
if( ++count > 5 ) first = first->next;
}
return first;
}
const char *MessageList::AddMessage(int sev, const char *message)
{
dt_message *msg;
if( m_lastmsg && 0==strcmp(m_lastmsg->text, message) ){
m_lastmsg->timestamp = now;
if( sev > 0 && sev < m_lastmsg->severity )
m_lastmsg->severity = sev;
return m_lastmsg->text;
}
if( m_count > 99 ){
msg = m_firstmsg;
m_firstmsg = m_firstmsg->next;
delete msg;
m_count--;
}
if( (msg = new dt_message) && (msg->text = new char[strlen(message) + 1]) ){
msg->timestamp = now;
msg->severity = sev;
strcpy(msg->text, message);
if( m_lastmsg ){
msg->next = m_lastmsg->next;
m_lastmsg->next = msg;
m_lastmsg = msg;
}else{
m_firstmsg = m_lastmsg = msg;
}
m_count++;
return msg->text;
}else if( msg ) delete []msg;
return (char *)0;
}
const char *MessageList::AddMessage(size_t msglen, int sev, const char *format,
va_list ap)
{
char *message = (char *)0;
const char *result = (char *)0;
if( msglen > 0 ){
msglen++;
if( (message = new char[msglen]) ){
vsnprintf(message, msglen, format, ap);
}
}else if( *format ){
int ret;
msglen = 81;
while( (message = new char[msglen]) ){
ret = vsnprintf(message, msglen, format, ap);
if( ret > 0 && ret < (int)msglen ) break;
delete []message;
if( ret > (int)msglen ) msglen = ret;
else msglen += 80;
}
}
if( message ){
result = AddMessage(sev, message);
delete []message;
}
return result;
}
void MessageList::Expire()
{
dt_message *p;
if( 0==*cfg_msg_expiration ) return;
while( (p = m_firstmsg) ){
if( p->timestamp + *cfg_msg_expiration * 3600 < now ){
m_firstmsg = m_firstmsg->next;
delete p;
m_count--;
}else break;
}
if( !m_firstmsg ){
m_lastmsg = (dt_message *)0;
m_count = 0; // failsafe
}
}
void MessageList::Clear(dt_message *last)
{
dt_message *p;
bool done = false;
while( !done && (p = m_firstmsg) ){
if( last == m_firstmsg ) done = true;
m_firstmsg = m_firstmsg->next;
delete p;
m_count--;
}
if( !m_firstmsg ){
m_lastmsg = (dt_message *)0;
m_count = 0; // failsafe
}
}