1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Daniel Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 * logf() logs entries in a circular buffer. Each logged string is null-terminated.
25 * When the length of log exceeds MAX_LOGF_SIZE bytes, the buffer wraps.
35 #ifdef HAVE_REMOTE_LCD
36 #include "lcd-remote.h"
44 #include "usbstack/usb_serial.h"
47 #ifdef ROCKBOX_HAS_LOGDISKF
51 #include "ata_idle_notify.h"
53 unsigned char logdiskfbuffer
[MAX_LOGDISKF_SIZE
];
57 /* Only provide all this if asked to */
58 #ifdef ROCKBOX_HAS_LOGF
61 unsigned char logfbuffer
[MAX_LOGF_SIZE
];
66 #ifdef HAVE_REMOTE_LCD
67 static void displayremote(void)
69 /* TODO: we should have a debug option that enables/disables this! */
72 int cur_x
, cur_y
, delta_y
, delta_x
;
76 /* Memorize the pointer to the beginning of the last ... lines
77 I assume delta_y >= 6 to avoid wasting memory and allocating memory dynamically
78 I hope there is no font with height < 6 ! */
79 const int NB_ENTRIES
=LCD_REMOTE_HEIGHT
/ 6;
80 int line_start_ptr
[NB_ENTRIES
];
82 fontnr
= lcd_getfont();
83 font
= font_get(fontnr
);
85 /* get the horizontal size of each line */
86 font_getstringsize("A", NULL
, &delta_y
, fontnr
);
88 /* font too small ? */
91 /* nothing to print ? */
92 if(logfindex
== 0 && !logfwrap
)
96 h
= LCD_REMOTE_HEIGHT
;
106 line_start_ptr
[0] = i
;
110 if(logfbuffer
[i
] == '\0')
112 line_start_ptr
[++nb_lines
% NB_ENTRIES
] = i
+1;
117 /* does character fit on this line ? */
118 delta_x
= font_get_width(font
, logfbuffer
[i
]);
120 if(cur_x
+ delta_x
> w
)
123 line_start_ptr
[++nb_lines
% NB_ENTRIES
] = i
;
129 if(i
>= MAX_LOGF_SIZE
)
131 } while(i
!= logfindex
);
133 lcd_remote_clear_display();
135 i
= line_start_ptr
[ MAX(nb_lines
- h
/ delta_y
, 0) % NB_ENTRIES
];
141 if(logfbuffer
[i
] == '\0')
148 /* does character fit on this line ? */
149 delta_x
= font_get_width(font
, logfbuffer
[i
]);
151 if(cur_x
+ delta_x
> w
)
157 buf
[0] = logfbuffer
[i
];
158 lcd_remote_putsxy(cur_x
, cur_y
, buf
);
163 if(i
>= MAX_LOGF_SIZE
)
165 } while(i
!= logfindex
);
170 #define displayremote()
174 void _logf(const char *format
, ...)
178 va_start(ap
, format
);
180 vsnprintf(buf
, sizeof buf
, format
, ap
);
181 printf("DEBUG: %s\n", buf
);
184 static void check_logfindex(void)
186 if(logfindex
>= MAX_LOGF_SIZE
)
194 static int logf_push(void *userp
, unsigned char c
)
198 logfbuffer
[logfindex
++] = c
;
201 #if defined(HAVE_SERIAL) && !defined(SIMULATOR) && defined(LOGF_SERIAL)
214 void _logf(const char *fmt
, ...)
216 #ifdef USB_ENABLE_SERIAL
217 int old_logfindex
= logfindex
;
223 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
225 vsnprintf(buf
, sizeof buf
, fmt
, ap
);
227 /* restart va_list otherwise the result if undefined when vuprintf is called */
232 vuprintf(logf_push
, NULL
, fmt
, ap
);
235 /* add trailing zero */
236 logf_push(NULL
, '\0');
238 #if defined(HAVE_SERIAL) && !defined(SIMULATOR) && defined(LOGF_SERIAL)
241 #ifdef USB_ENABLE_SERIAL
243 if(logfindex
< old_logfindex
)
245 usb_serial_send(logfbuffer
+ old_logfindex
, MAX_LOGF_SIZE
- old_logfindex
);
246 usb_serial_send(logfbuffer
, logfindex
- 1);
249 usb_serial_send(logfbuffer
+ old_logfindex
, logfindex
- old_logfindex
- 1);
250 usb_serial_send("\r\n", 2);
259 #ifdef ROCKBOX_HAS_LOGDISKF
260 static int logdiskf_push(void *userp
, unsigned char c
)
264 /*just stop logging if out of space*/
265 if(logdiskfindex
>=MAX_LOGDISKF_SIZE
-1)
267 strcpy(&logdiskfbuffer
[logdiskfindex
-8], "LOGFULL");
268 logdiskfindex
=MAX_LOGDISKF_SIZE
;
271 logdiskfbuffer
[logdiskfindex
++] = c
;
276 void _logdiskf(const char* file
, const char level
, const char *fmt
, ...)
282 int len
=strlen(file
);
283 if(logdiskfindex
+len
+ 4 > MAX_LOGDISKF_SIZE
-1)
285 strcpy(&logdiskfbuffer
[logdiskfindex
-8], "LOGFULL");
286 logdiskfindex
=MAX_LOGDISKF_SIZE
;
290 logdiskf_push(NULL
, level
);
291 logdiskf_push(NULL
, ' ');
292 logdiskf_push(NULL
, '[');
293 strcpy(&logdiskfbuffer
[logdiskfindex
], file
);
294 logdiskfindex
+= len
;
295 logdiskf_push(NULL
, ']');
297 vuprintf(logdiskf_push
, NULL
, fmt
, ap
);
302 static void flush_buffer(void* data
)
306 if(logdiskfindex
< 1)
309 fd
= open(HOME_DIR
"/rockbox_log.txt", O_RDWR
| O_CREAT
| O_APPEND
, 0666);
313 write(fd
, logdiskfbuffer
, logdiskfindex
);
321 register_storage_idle_func(flush_buffer
);