My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package org.bluecove.tester.me;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import org.bluecove.tester.log.Logger;
import org.bluecove.tester.log.LoggerAppender;
import org.bluecove.tester.util.TimeUtils;

public class LoggerCanvas extends Canvas implements LoggerAppender {

protected boolean showLogDebug = true;

public static boolean logTimeStamp = false;

public static boolean logTimeStampMillisecond = true;

private int line;

private int lineOffsetY;

private int lineOffsetX;

private Vector logMessages = new Vector();

private int errorCount = 0;

private int logLine = 0;

private int logScrollX;

private int logVisibleLines = 0;

private int logMessagesSownSize = 0;

private boolean movingCursor = false;

private boolean logLastEvenVisible = true;

protected int backgroundColor = 255;

protected int fontColor = 0;

private long nextMemroyUpdate = 0;

private int nextMemroyStatusLen = 0;

public LoggerCanvas() {
Logger.addAppender(this);
}

protected String getCanvasTitleText() {
return null;
}

protected String getCanvasStatusText() {
return null;
}

protected void paintBackground(Graphics g, int width, int height) {

}

protected void paintMemoryStatus(Graphics g, int y, int width, int height) {
long now = System.currentTimeMillis();
if (nextMemroyUpdate < now) {
// Next Update in 5 secons
nextMemroyUpdate = now + 1000 * 5;
Runtime r = Runtime.getRuntime();
long total = r.totalMemory();
long used = total - r.freeMemory();
nextMemroyStatusLen = (int) ((used * width) / total);
}
g.drawLine(0, y, nextMemroyStatusLen, y);
}

public int writeln(Graphics g, String s) {
int h = (g.getFont().getHeight() + 1);
int y = lineOffsetY + h * line;
g.drawString(s, lineOffsetX, y, Graphics.LEFT | Graphics.TOP);
line++;
return y + h;
}

protected void paint(Graphics g) {
lineOffsetY = 0;
lineOffsetX = 0;
line = 0;
int width = getWidth();
int height = getHeight();

g.setGrayScale(backgroundColor);
g.fillRect(0, 0, width, height);
paintBackground(g, width, height);
g.setColor(fontColor);
int lastY = 0;
String title = getCanvasTitleText();
if (title != null) {
lastY = writeln(g, title);
}
paintMemoryStatus(g, lastY, width, height);

line = 0;
lineOffsetY = lastY + 2;
Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
g.setFont(font);

String status = getCanvasStatusText();
if (status != null) {
lastY = writeln(g, status);
}

int lineHeight = g.getFont().getHeight() + 1;
logVisibleLines = (height - lastY) / lineHeight;
lineOffsetX = logScrollX;

if (!movingCursor && logLastEvenVisible) {
if (((logLine + logVisibleLines) < logMessages.size())) {
setLogEndLine();
}
}
movingCursor = false;
logMessagesSownSize = logMessages.size();
int logIndex = logLine;
while (((lastY) < height) && (logIndex < logMessagesSownSize)) {
try {
String message = (String) logMessages.elementAt(logIndex);
lastY = writeln(g, message);
logIndex++;
} catch (ArrayIndexOutOfBoundsException e) {
logLastEvenVisible = true;
return;
}
}
logLastEvenVisible = (logIndex == logMessagesSownSize);
}

protected void logScrollBottom() {
logScrollX = 0;
setLogEndLine();
}

protected void logLinesMove(int action) {
switch (action) {
case UP:
if (logLine > 0) {
logLine--;
movingCursor = true;
}
break;
case DOWN:
if ((logLine + logVisibleLines - 1) < logMessages.size()) {
logLine++;
}
break;
case RIGHT:
if (logScrollX > -500) {
logScrollX -= 20;
}
break;
case LEFT:
if (logScrollX < 0) {
logScrollX += 20;
}
break;
default:
return;
}
}

public void appendLog(int level, String message, Throwable throwable) {
if (!showLogDebug && (level == Logger.DEBUG)) {
return;
}
StringBuffer buf = new StringBuffer();
if (logTimeStamp) {
TimeUtils.appendTimeStampNow(buf, logTimeStampMillisecond).append(' ');
}
switch (level) {
case Logger.ERROR:
errorCount++;
buf.append("e.");
break;
case Logger.WARN:
buf.append("w.");
break;
case Logger.INFO:
buf.append("i.");
break;
}
buf.append(message);
if (throwable != null) {
buf.append(' ');
String className = throwable.getClass().getName();
buf.append(className.substring(1 + className.lastIndexOf('.')));
buf.append(':');
buf.append(throwable.getMessage());
}
String m = buf.toString().replace('\t', ' ');
int cr = m.indexOf("\n");
while (cr != -1) {
logMessages.addElement(m.substring(0, cr));
m = m.substring(cr + 1);
cr = m.indexOf("\n");
}
logMessages.addElement(m);

int logMax = 500;
if (logMessages.size() >= logMax) {
Vector newLogMessages = new Vector();
for (int i = logMax - 5; i < logMax; i++) {
newLogMessages.addElement(logMessages.elementAt(i));
}
Vector prevLogMessages = logMessages;
logMessages = newLogMessages;
// GC helper
prevLogMessages.removeAllElements();
logLine = 0;
}

if (logLastEvenVisible) {
// BlueCoveTestMIDlet.display.flashBacklight(0);
repaint();
}
}

private void setLogEndLine() {
logLine = logMessages.size() - logVisibleLines;
if (logLine < 0) {
logLine = 0;
}
}

protected void clearLog() {
logMessages.removeAllElements();
errorCount = 0;
logLine = 0;
logScrollX = 0;
}
}

Change log

r2916 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2655 by skarzhevskyy on Dec 24, 2008   Diff
minimize memory usage
r2651 by skarzhevskyy on Dec 23, 2008   Diff
paintMemoryStatus
r2648 by skarzhevskyy on Dec 23, 2008   Diff
GC helper
All revisions of this file

File info

Size: 6465 bytes, 267 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting