This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MWMessageContainer.m
209 lines (171 loc) · 5.29 KB
/
MWMessageContainer.m
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* MWMessageContainer.m
*
* History:
* Paul Jankunas on 4/1/05 - Created.
*
* Copyright 2005 MIT. All rights reserved.
*/
#include "boost/date_time/posix_time/posix_time.hpp"
#import "MWMessageContainer.h"
@implementation MWMessageContainer
- (void)dealloc {
[message release];
message = nil;
[timeValue release];
timeValue = nil;
[altTimeValue release];
altTimeValue = nil;
messageTimeColor = nil;
[DefaultColor release];
DefaultColor = nil;
[WarningColor release];
WarningColor = nil;
[ErrorColor release];
ErrorColor = nil;
[FatalColor release];
FatalColor = nil;
[NetworkColor release];
NetworkColor = nil;
[LocalColor release];
LocalColor = nil;
[RemoteColor release];
RemoteColor = nil;
[super dealloc];
}
- (id)initMessageContainerWithMessage:(NSString *)msg
atTime:(NSNumber *)tme
ofType:(int)type
onLocalConsole:(BOOL)_localConsole {
self = [super init];
if(self) {
message = [msg retain];
altTimeValue = [tme retain];
msgType = type;
timeValue = nil;
DefaultColor = [[NSColor colorWithCalibratedRed:0.0 green:0.5
blue:0.0 alpha:1.0] retain];
WarningColor = [[NSColor brownColor] retain];
ErrorColor = [[NSColor redColor] retain];
FatalColor = [[NSColor darkGrayColor] retain];
// dark green
NetworkColor = [[NSColor colorWithCalibratedRed:0.0 green:0.5
blue:0.0 alpha:1.0] retain];
LocalColor = [[NSColor colorWithCalibratedRed:0.0 green:0.0
blue:0.0 alpha:1.0] retain];
RemoteColor = [[NSColor colorWithCalibratedRed:0.2 green:0.2
blue:0.2 alpha:1.0] retain];
//[self initMessageParagraphStyle];
[self setMessageTimeColor:msgType];
[self setMessageColor:_localConsole];
}
return self;
}
+ (NSMutableParagraphStyle *)sharedMessageParagraphStyle {
static NSMutableParagraphStyle *sharedMessageParagraphStyle = nil;
if(sharedMessageParagraphStyle == nil){
sharedMessageParagraphStyle = [[NSMutableParagraphStyle alloc] retain];
[sharedMessageParagraphStyle setHeadIndent:70.0];
}
return sharedMessageParagraphStyle;
}
- (NSAttributedString *)messageForConsole {
NSDictionary *timeAttributes = [NSDictionary
dictionaryWithObjectsAndKeys:
messageTimeColor,
NSForegroundColorAttributeName,
[NSFont boldSystemFontOfSize:0],
NSFontAttributeName,
[MWMessageContainer sharedMessageParagraphStyle],
NSParagraphStyleAttributeName,
nil];
NSDictionary *messageAttributes;
messageAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
messageColor,
NSForegroundColorAttributeName,
[MWMessageContainer sharedMessageParagraphStyle],
NSParagraphStyleAttributeName,
nil];
namespace bpt = boost::posix_time;
bpt::time_duration eventTime = bpt::seconds((long)([altTimeValue longLongValue] / 1000000LL));
NSMutableAttributedString *consoleMsg =
[[NSMutableAttributedString alloc]
initWithString:[NSString stringWithUTF8String:(bpt::to_simple_string(eventTime).c_str())]
attributes:timeAttributes];
/* NSString * consoleMsg = [NSString stringWithFormat:@"%@",
[[NSDate dateWithTimeIntervalSince1970:
[altTimeValue doubleValue]]
descriptionWithCalendarFormat:@"%H:%M:%S"
timeZone:nil
locale:nil]
];
consoleMsg = [consoleMsg stringByAppendingString:[NSString
stringWithFormat:@" %@\n", message]];
*/
NSAttributedString *appendstring =
[ [NSAttributedString alloc]
initWithString:
[NSString stringWithFormat:@": %@\n",message]
attributes: messageAttributes];
//[consoleMsg appendAttributedString:appendstring];
[consoleMsg appendAttributedString:[appendstring autorelease]];
[consoleMsg autorelease];
/*return [[[NSAttributedString alloc] initWithString:consoleMsg
attributes:[NSDictionary
dictionaryWithObject:messageColor
forKey:NSForegroundColorAttributeName]]
autorelease];*/
return consoleMsg;
}
- (void)setMessageTimeColor:(int)colorType {
switch(colorType) {
case MW_GENERIC_MESSAGE:
messageTimeColor = DefaultColor;
break;
case MW_ERROR_MESSAGE:
messageTimeColor = ErrorColor;
break;
case MW_WARNING_MESSAGE:
messageTimeColor = WarningColor;
break;
case MW_FATAL_ERROR_MESSAGE:
messageTimeColor = FatalColor;
break;
case MW_NETWORK_MESSAGE:
messageTimeColor = NetworkColor;
break;
default:
messageTimeColor = DefaultColor;
break;
}
}
- (void)setMessageColor:(BOOL)isLocal {
if (isLocal) {
messageColor = LocalColor;
} else {
messageColor = RemoteColor;
}
}
- (NSColor *)messageTimeColor {
return messageTimeColor;
}
- (NSColor *)messageColor {
return messageColor;
}
- (NSString *)message {
return message;
}
- (NSDate *)dateTimeValue {
if(timeValue == nil) {
timeValue = [[NSDate dateWithTimeIntervalSince1970:[altTimeValue
doubleValue]] retain];
}
return timeValue;
}
- (NSNumber *)numberTimeValue {
return altTimeValue;
}
- (int)type {
return msgType;
}
@end