Daniel Stenberg | b033f6c | 2005-05-23 22:47:42 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Daniel Stenberg |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 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. |
Daniel Stenberg | b033f6c | 2005-05-23 22:47:42 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | /* |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 23 | * logf() logs entries in a circular buffer. Each logged string is null-terminated. |
Maurus Cuelenaere | 4c793fe | 2008-12-31 17:01:00 +0000 | [diff] [blame] | 24 | * |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 25 | * When the length of log exceeds MAX_LOGF_SIZE bytes, the buffer wraps. |
Maurus Cuelenaere | 4c793fe | 2008-12-31 17:01:00 +0000 | [diff] [blame] | 26 | * |
Daniel Stenberg | b033f6c | 2005-05-23 22:47:42 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdarg.h> |
Daniel Stenberg | b033f6c | 2005-05-23 22:47:42 +0000 | [diff] [blame] | 32 | #include "config.h" |
Thomas Martitz | 6ac6420 | 2010-05-06 22:17:34 +0000 | [diff] [blame] | 33 | #include "system.h" |
| 34 | #include "font.h" |
Daniel Stenberg | 9e9f58f | 2005-05-24 14:28:48 +0000 | [diff] [blame] | 35 | #include "lcd-remote.h" |
Daniel Stenberg | 11bf87f | 2005-05-24 14:26:54 +0000 | [diff] [blame] | 36 | #include "logf.h" |
Linus Nielsen Feltzing | 00d2182 | 2006-10-12 20:22:16 +0000 | [diff] [blame] | 37 | #include "serial.h" |
Jeffrey Goode | b8a51ad | 2010-05-15 03:47:06 +0000 | [diff] [blame^] | 38 | #include "format.h" |
Daniel Stenberg | b033f6c | 2005-05-23 22:47:42 +0000 | [diff] [blame] | 39 | |
Frank Gevaerts | 776d015 | 2008-03-02 20:45:33 +0000 | [diff] [blame] | 40 | #ifdef HAVE_USBSTACK |
| 41 | #include "usb_core.h" |
| 42 | #include "usbstack/usb_serial.h" |
| 43 | #endif |
| 44 | |
Daniel Stenberg | edc0792 | 2005-05-30 13:00:43 +0000 | [diff] [blame] | 45 | /* Only provide all this if asked to */ |
| 46 | #ifdef ROCKBOX_HAS_LOGF |
| 47 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 48 | #ifndef __PCTOOL__ |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 49 | unsigned char logfbuffer[MAX_LOGF_SIZE]; |
Linus Nielsen Feltzing | 056effc | 2005-05-24 14:01:16 +0000 | [diff] [blame] | 50 | int logfindex; |
Daniel Stenberg | b033f6c | 2005-05-23 22:47:42 +0000 | [diff] [blame] | 51 | bool logfwrap; |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 52 | #endif |
Daniel Stenberg | b033f6c | 2005-05-23 22:47:42 +0000 | [diff] [blame] | 53 | |
Daniel Stenberg | 11bf87f | 2005-05-24 14:26:54 +0000 | [diff] [blame] | 54 | #ifdef HAVE_REMOTE_LCD |
| 55 | static void displayremote(void) |
| 56 | { |
| 57 | /* TODO: we should have a debug option that enables/disables this! */ |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 58 | int w, h, i; |
| 59 | int fontnr; |
| 60 | int cur_x, cur_y, delta_y, delta_x; |
| 61 | struct font* font; |
| 62 | int nb_lines; |
| 63 | char buf[2]; |
| 64 | /* Memorize the pointer to the beginning of the last ... lines |
| 65 | I assume delta_y >= 6 to avoid wasting memory and allocating memory dynamically |
| 66 | I hope there is no font with height < 6 ! */ |
| 67 | const int NB_ENTRIES=LCD_REMOTE_HEIGHT / 6; |
| 68 | int line_start_ptr[NB_ENTRIES]; |
| 69 | |
| 70 | fontnr = lcd_getfont(); |
| 71 | font = font_get(fontnr); |
| 72 | |
| 73 | /* get the horizontal size of each line */ |
| 74 | font_getstringsize("A", NULL, &delta_y, fontnr); |
| 75 | |
| 76 | /* font too small ? */ |
| 77 | if(delta_y < 6) |
| 78 | return; |
| 79 | /* nothing to print ? */ |
| 80 | if(logfindex == 0 && !logfwrap) |
| 81 | return; |
| 82 | |
| 83 | w = LCD_REMOTE_WIDTH; |
| 84 | h = LCD_REMOTE_HEIGHT; |
| 85 | nb_lines = 0; |
| 86 | |
| 87 | if(logfwrap) |
| 88 | i = logfindex; |
| 89 | else |
| 90 | i = 0; |
| 91 | |
| 92 | cur_x = 0; |
| 93 | |
| 94 | line_start_ptr[0] = i; |
| 95 | |
| 96 | do |
| 97 | { |
| 98 | if(logfbuffer[i] == '\0') |
| 99 | { |
| 100 | line_start_ptr[++nb_lines % NB_ENTRIES] = i+1; |
| 101 | cur_x = 0; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | /* does character fit on this line ? */ |
| 106 | delta_x = font_get_width(font, logfbuffer[i]); |
| 107 | |
| 108 | if(cur_x + delta_x > w) |
| 109 | { |
| 110 | cur_x = 0; |
| 111 | line_start_ptr[++nb_lines % NB_ENTRIES] = i; |
| 112 | } |
| 113 | /* update pointer */ |
| 114 | cur_x += delta_x; |
| 115 | } |
| 116 | i++; |
| 117 | if(i >= MAX_LOGF_SIZE) |
| 118 | i = 0; |
| 119 | } while(i != logfindex); |
| 120 | |
Daniel Stenberg | 11bf87f | 2005-05-24 14:26:54 +0000 | [diff] [blame] | 121 | lcd_remote_clear_display(); |
| 122 | |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 123 | i = line_start_ptr[ MAX(nb_lines - h / delta_y, 0) % NB_ENTRIES]; |
| 124 | cur_x = 0; |
| 125 | cur_y = 0; |
| 126 | buf[1] = '\0'; |
| 127 | |
| 128 | do { |
| 129 | if(logfbuffer[i] == '\0') |
| 130 | { |
| 131 | cur_y += delta_y; |
| 132 | cur_x = 0; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | /* does character fit on this line ? */ |
| 137 | delta_x = font_get_width(font, logfbuffer[i]); |
| 138 | |
| 139 | if(cur_x + delta_x > w) |
| 140 | { |
| 141 | cur_y += delta_y; |
| 142 | cur_x = 0; |
| 143 | } |
| 144 | |
| 145 | buf[0] = logfbuffer[i]; |
| 146 | lcd_remote_putsxy(cur_x, cur_y, buf); |
| 147 | cur_x += delta_x; |
Daniel Stenberg | 11bf87f | 2005-05-24 14:26:54 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 150 | i++; |
| 151 | if(i >= MAX_LOGF_SIZE) |
| 152 | i = 0; |
| 153 | } while(i != logfindex); |
| 154 | |
Daniel Stenberg | 11bf87f | 2005-05-24 14:26:54 +0000 | [diff] [blame] | 155 | lcd_remote_update(); |
| 156 | } |
| 157 | #else |
| 158 | #define displayremote() |
| 159 | #endif |
| 160 | |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 161 | #ifdef __PCTOOL__ |
| 162 | void _logf(const char *format, ...) |
| 163 | { |
| 164 | char buf[1024]; |
| 165 | va_list ap; |
| 166 | va_start(ap, format); |
| 167 | |
| 168 | vsnprintf(buf, sizeof buf, format, ap); |
| 169 | printf("DEBUG: %s\n", buf); |
| 170 | } |
| 171 | #else |
Frank Gevaerts | 7ae9c8d | 2009-02-07 19:59:51 +0000 | [diff] [blame] | 172 | static void check_logfindex(void) |
| 173 | { |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 174 | if(logfindex >= MAX_LOGF_SIZE) |
| 175 | { |
Frank Gevaerts | 7ae9c8d | 2009-02-07 19:59:51 +0000 | [diff] [blame] | 176 | /* wrap */ |
| 177 | logfwrap = true; |
| 178 | logfindex = 0; |
| 179 | } |
| 180 | } |
| 181 | |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 182 | static int logf_push(void *userp, unsigned char c) |
Daniel Stenberg | b033f6c | 2005-05-23 22:47:42 +0000 | [diff] [blame] | 183 | { |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 184 | (void)userp; |
| 185 | |
| 186 | logfbuffer[logfindex++] = c; |
| 187 | check_logfindex(); |
| 188 | |
Maurus Cuelenaere | dd9a9e4 | 2009-03-11 16:42:25 +0000 | [diff] [blame] | 189 | #if defined(HAVE_SERIAL) && !defined(SIMULATOR) |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 190 | if(c != '\0') |
| 191 | { |
| 192 | char buf[2]; |
| 193 | buf[0] = c; |
| 194 | buf[1] = '\0'; |
| 195 | serial_tx(buf); |
| 196 | } |
| 197 | #endif |
| 198 | |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | void _logf(const char *fmt, ...) |
| 203 | { |
| 204 | #ifdef USB_ENABLE_SERIAL |
| 205 | int old_logfindex = logfindex; |
| 206 | #endif |
| 207 | va_list ap; |
| 208 | |
| 209 | va_start(ap, fmt); |
Jeffrey Goode | fade88a | 2009-11-02 15:30:06 +0000 | [diff] [blame] | 210 | |
| 211 | #ifdef SIMULATOR |
| 212 | char buf[1024]; |
| 213 | vsnprintf(buf, sizeof buf, fmt, ap); |
| 214 | DEBUGF("%s\n", buf); |
| 215 | #endif |
| 216 | |
Thomas Martitz | 1ddb91a | 2009-11-03 21:20:09 +0000 | [diff] [blame] | 217 | vuprintf(logf_push, NULL, fmt, ap); |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 218 | va_end(ap); |
| 219 | |
| 220 | /* add trailing zero */ |
| 221 | logf_push(NULL, '\0'); |
| 222 | |
| 223 | #if defined(HAVE_SERIAL) && !defined(SIMULATOR) |
Linus Nielsen Feltzing | 00d2182 | 2006-10-12 20:22:16 +0000 | [diff] [blame] | 224 | serial_tx("\r\n"); |
| 225 | #endif |
Frank Gevaerts | c61a751 | 2009-05-23 14:30:20 +0000 | [diff] [blame] | 226 | #ifdef USB_ENABLE_SERIAL |
Frank Gevaerts | 776d015 | 2008-03-02 20:45:33 +0000 | [diff] [blame] | 227 | |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 228 | if(logfindex < old_logfindex) |
Maurus Cuelenaere | 4c793fe | 2008-12-31 17:01:00 +0000 | [diff] [blame] | 229 | { |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 230 | usb_serial_send(logfbuffer + old_logfindex, MAX_LOGF_SIZE - old_logfindex); |
| 231 | usb_serial_send(logfbuffer, logfindex - 1); |
Maurus Cuelenaere | 4c793fe | 2008-12-31 17:01:00 +0000 | [diff] [blame] | 232 | } |
Maurus Cuelenaere | 20b0dd2 | 2009-08-21 22:54:23 +0000 | [diff] [blame] | 233 | else |
| 234 | usb_serial_send(logfbuffer + old_logfindex, logfindex - old_logfindex - 1); |
| 235 | usb_serial_send("\r\n", 2); |
| 236 | #endif |
Daniel Stenberg | 11bf87f | 2005-05-24 14:26:54 +0000 | [diff] [blame] | 237 | |
| 238 | displayremote(); |
Daniel Stenberg | b033f6c | 2005-05-23 22:47:42 +0000 | [diff] [blame] | 239 | } |
Miika Pekkarinen | 0dd7ea2 | 2006-11-10 08:03:33 +0000 | [diff] [blame] | 240 | #endif |
Daniel Stenberg | edc0792 | 2005-05-30 13:00:43 +0000 | [diff] [blame] | 241 | |
| 242 | #endif |