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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2005 by Daniel Stenberg
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
/*
* logf() logs entries in a circular buffer. Each logged string is null-terminated.
*
* When the length of log exceeds MAX_LOGF_SIZE bytes, the buffer wraps.
*
*/
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "config.h"
#include "system.h"
#include "font.h"
#include "lcd.h"
#ifdef HAVE_REMOTE_LCD
#include "lcd-remote.h"
#endif
#include "logf.h"
#include "serial.h"
#include "vuprintf.h"
#ifdef HAVE_USBSTACK
#include "usb_core.h"
#include "usbstack/usb_serial.h"
#endif
#ifdef ROCKBOX_HAS_LOGDISKF
#include "logdiskf.h"
#include "file.h"
#include "rbpaths.h"
#include "ata_idle_notify.h"
unsigned char logdiskfbuffer[MAX_LOGDISKF_SIZE];
static int logdiskfindex;
#endif
/* Only provide all this if asked to */
#ifdef ROCKBOX_HAS_LOGF
#ifndef __PCTOOL__
unsigned char logfbuffer[MAX_LOGF_SIZE];
int logfindex;
bool logfwrap;
bool logfenabled = true;
#endif
#ifdef HAVE_REMOTE_LCD
static void displayremote(void)
{
/* TODO: we should have a debug option that enables/disables this! */
int w, h, i;
int fontnr;
int cur_x, cur_y, delta_y, delta_x;
struct font* font;
int nb_lines;
char buf[2];
/* Memorize the pointer to the beginning of the last ... lines
I assume delta_y >= 6 to avoid wasting memory and allocating memory dynamically
I hope there is no font with height < 6 ! */
const int NB_ENTRIES=LCD_REMOTE_HEIGHT / 6;
int line_start_ptr[NB_ENTRIES];
fontnr = lcd_getfont();
font = font_get(fontnr);
/* get the horizontal size of each line */
font_getstringsize("A", NULL, &delta_y, fontnr);
/* font too small ? */
if(delta_y < 6)
return;
/* nothing to print ? */
if(logfindex == 0 && !logfwrap)
return;
w = LCD_REMOTE_WIDTH;
h = LCD_REMOTE_HEIGHT;
nb_lines = 0;
if(logfwrap)
i = logfindex;
else
i = 0;
cur_x = 0;
line_start_ptr[0] = i;
do
{
if(logfbuffer[i] == '\0')
{
line_start_ptr[++nb_lines % NB_ENTRIES] = i+1;
cur_x = 0;
}
else
{
/* does character fit on this line ? */
delta_x = font_get_width(font, logfbuffer[i]);
if(cur_x + delta_x > w)
{
cur_x = 0;
line_start_ptr[++nb_lines % NB_ENTRIES] = i;
}
/* update pointer */
cur_x += delta_x;
}
i++;
if(i >= MAX_LOGF_SIZE)
i = 0;
} while(i != logfindex);
lcd_remote_clear_display();
i = line_start_ptr[ MAX(nb_lines - h / delta_y, 0) % NB_ENTRIES];
cur_x = 0;
cur_y = 0;
buf[1] = '\0';
do {
if(logfbuffer[i] == '\0')
{
cur_y += delta_y;
cur_x = 0;
}
else
{
/* does character fit on this line ? */
delta_x = font_get_width(font, logfbuffer[i]);
if(cur_x + delta_x > w)
{
cur_y += delta_y;
cur_x = 0;
}
buf[0] = logfbuffer[i];
lcd_remote_putsxy(cur_x, cur_y, buf);
cur_x += delta_x;
}
i++;
if(i >= MAX_LOGF_SIZE)
i = 0;
} while(i != logfindex);
lcd_remote_update();
}
#else
#define displayremote()
#endif
#ifdef __PCTOOL__
void _logf(const char *format, ...)
{
char buf[1024];
va_list ap;
va_start(ap, format);
vsnprintf(buf, sizeof buf, format, ap);
printf("DEBUG: %s\n", buf);
}
#else
static void check_logfindex(void)
{
if(logfindex >= MAX_LOGF_SIZE)
{
/* wrap */
logfwrap = true;
logfindex = 0;
}
}
static int logf_push(void *userp, int c)
{
(void)userp;
logfbuffer[logfindex++] = c;
check_logfindex();
#if defined(HAVE_SERIAL) && !defined(SIMULATOR) && defined(LOGF_SERIAL)
if(c != '\0')
{
char buf[2];
buf[0] = c;
buf[1] = '\0';
serial_tx(buf);
}
#endif
return 1;
}
void _logf(const char *fmt, ...)
{
if (!logfenabled)
return;
#ifdef USB_ENABLE_SERIAL
int old_logfindex = logfindex;
#endif
va_list ap;
va_start(ap, fmt);
#if (CONFIG_PLATFORM & PLATFORM_HOSTED)
char buf[1024];
vsnprintf(buf, sizeof buf, fmt, ap);
DEBUGF("%s\n", buf);
/* restart va_list otherwise the result if undefined when vuprintf is called */
va_end(ap);
va_start(ap, fmt);
#endif
vuprintf(logf_push, NULL, fmt, ap);
va_end(ap);
/* add trailing zero */
logf_push(NULL, '\0');
#if defined(HAVE_SERIAL) && !defined(SIMULATOR) && defined(LOGF_SERIAL)
serial_tx("\r\n");
#endif
#ifdef USB_ENABLE_SERIAL
if(logfindex < old_logfindex)
{
usb_serial_send(logfbuffer + old_logfindex, MAX_LOGF_SIZE - old_logfindex);
usb_serial_send(logfbuffer, logfindex - 1);
}
else
usb_serial_send(logfbuffer + old_logfindex, logfindex - old_logfindex - 1);
usb_serial_send("\r\n", 2);
#endif
displayremote();
}
#endif
void logf_panic_dump(int *y)
{
int i;
/* nothing to print ? */
if(logfindex == 0 && !logfwrap)
{
lcd_puts(1, (*y)++, "no logf data");
lcd_update();
return;
}
lcd_puts(1, (*y)++, "start of logf data");
lcd_update();
i = logfindex - 2; /* The last actual characer (i.e. not '\0') */
while(i >= 0)
{
while(logfbuffer[i] != 0 && i>=0)
{
i--;
}
if(strlen( &logfbuffer[i + 1]) > 0)
{
lcd_puts(1, (*y)++, &logfbuffer[i + 1]);
lcd_update();
}
i--;
}
if(logfwrap)
{
i = MAX_LOGF_SIZE - 1;
while(i >= logfindex)
{
while(logfbuffer[i] != 0 && i >= logfindex)
{
i--;
}
if(strlen( &logfbuffer[i + 1]) > 0)
{
lcd_putsf(1, (*y)++, "%*s", (MAX_LOGF_SIZE-i) &logfbuffer[i + 1]);
lcd_update();
}
}
i--;
}
lcd_puts(1, (*y)++, "end of logf data");
lcd_update();
}
#endif
#ifdef ROCKBOX_HAS_LOGDISKF
static int logdiskf_push(void *userp, int c)
{
(void)userp;
/*just stop logging if out of space*/
if(logdiskfindex>=MAX_LOGDISKF_SIZE-1)
{
strcpy(&logdiskfbuffer[logdiskfindex-8], "LOGFULL");
logdiskfindex=MAX_LOGDISKF_SIZE;
return 0;
}
logdiskfbuffer[logdiskfindex++] = c;
return 1;
}
static void flush_buffer(void);
void _logdiskf(const char* file, const char level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int len =strlen(file);
if(logdiskfindex +len + 4 > MAX_LOGDISKF_SIZE-1)
{
strcpy(&logdiskfbuffer[logdiskfindex-8], "LOGFULL");
logdiskfindex=MAX_LOGDISKF_SIZE;
va_end(ap);
return;
}
logdiskf_push(NULL, level);
logdiskf_push(NULL, ' ');
logdiskf_push(NULL, '[');
strcpy(&logdiskfbuffer[logdiskfindex], file);
logdiskfindex += len;
logdiskf_push(NULL, ']');
vuprintf(logdiskf_push, NULL, fmt, ap);
va_end(ap);
register_storage_idle_func(flush_buffer);
}
static void flush_buffer(void)
{
int fd;
if(logdiskfindex < 1)
return;
fd = open(HOME_DIR"/rockbox_log.txt", O_RDWR | O_CREAT | O_APPEND, 0666);
if (fd < 0)
return;
write(fd, logdiskfbuffer, logdiskfindex);
close(fd);
logdiskfindex = 0;
}
#endif
|