50 #define STRING_LENGTH 4096
51 #define MAX_ANDROID_CONSOLE_MESSAGE_SLOTS 100 //5 max number of message lines per frame
52 #define MAX_LINE_LENGTH 80 //wrap text here to make it easy for GUI frontends
57 char **androidMessageSlot;
58 int androidHaveUnreadMessages;
59 char FWbuffer [STRING_LENGTH];
63 void(*callback[2])(
char *);
64 void(*callbackB[4])(
void*,
char*);
68 static void *ConsoleMessage_constructor(){
73 void ConsoleMessage_init(
struct tConsoleMessage *t){
76 t->prv = ConsoleMessage_constructor();
80 p->androidFreeSlot = 0;
81 p->androidHaveUnreadMessages = 0;
82 p->maxLineLength = MAX_LINE_LENGTH;
83 p->maxLines = MAX_ANDROID_CONSOLE_MESSAGE_SLOTS;
84 p->tabSpaces = TAB_SPACES;
85 p->androidMessageSlot = (
char**)malloc(MAX_ANDROID_CONSOLE_MESSAGE_SLOTS *
sizeof(
char*));
86 for (i = 0; i < p->maxLines; i++) p->androidMessageSlot[i] = (
char*)NULL;
87 p->callback[0] = NULL;
88 p->callback[1] = NULL;
95 void closeConsoleMessage() {
96 gglobal()->ConsoleMessage.consMsgCount = 0;
102 void fwg_updateConsoleStatus()
112 nlines = fwg_get_unread_message_count();
113 for (i = 0; i<nlines; i++)
115 buffer = fwg_get_last_message();
122 void fwg_setConsoleParam_maxLines(
int maxLines);
123 void fwg_setConsoleParam_maxLineLength(
int maxLineLength);
124 void fwg_setConsoleParam_replaceTabs(
int tabSpaces);
125 int fwg_get_unread_message_count();
126 char *fwg_get_last_message();
127 int fwl_StringConsoleMessage(
char* consoleBuffer);
128 void fwg_updateConsoleStatus();
129 void fwg_register_consolemessage_callback(
void(*callback)(
char *));
130 void fwg_register_consolemessage_callbackB(
void* data,
void(*callback)(
void *data,
char *));
133 void fwg_setConsoleParam_maxLines(
int maxLines)
141 p->androidMessageSlot = realloc(p->androidMessageSlot, maxLines*
sizeof(
char*));
142 for (i = p->maxLines; i<maxLines; i++) p->androidMessageSlot[i] = (
char *)NULL;
143 p->maxLines = maxLines;
147 void fwg_setConsoleParam_maxLineLength(
int maxLineLength)
153 if (maxLineLength > 0)
154 p->maxLineLength = maxLineLength;
156 void fwg_setConsoleParam_replaceTabs(
int tabSpaces)
163 p->tabSpaces = tabSpaces;
166 void fwg_register_consolemessage_callback(
void(*callback)(
char *))
181 if (p->callback[iback]) iback++;
182 p->callback[iback] = callback;
184 void fwg_register_consolemessage_callbackB(
void* data,
void(*callback)(
void*,
char *))
198 p->callbackB[p->nbackB] = callback;
199 p->dataB[p->nbackB] = data;
204 int fwg_get_unread_message_count() {
210 return p->androidHaveUnreadMessages;
213 char *fwg_get_last_message() {
228 if (!tg)
return "NO GGLOBAL - NO MESSAGES";
232 nmess = p->androidHaveUnreadMessages;
233 p->androidHaveUnreadMessages--;
236 whm = p->androidFreeSlot - nmess;
237 if (whm < 0) whm += p->maxLines;
240 if (p->androidMessageSlot[whm] == NULL)
243 return strdup(p->androidMessageSlot[whm]);
246 int fwl_StringConsoleMessage(
char* consoleBuffer) {
247 return ConsoleMessage(
"%s",consoleBuffer);
252 static void android_save_log(
char *thislog) {
269 for (i = 0; i<(int)strlen(thislog); i++) {
270 thislog[i] = thislog[i] & 0x7f;
274 more = (buf && *buf >
'\0');
279 ln = strchr(buf,
'\n');
297 if (p->androidMessageSlot[p->androidFreeSlot]){
299 char *catsize, *oldsize;
301 len1 = strlen(p->androidMessageSlot[p->androidFreeSlot]);
305 catsize = (
char*)malloc(len1 + len2 + 1);
306 memcpy(catsize, p->androidMessageSlot[p->androidFreeSlot], len1 + 1);
307 oldsize = p->androidMessageSlot[p->androidFreeSlot];
308 p->androidMessageSlot[p->androidFreeSlot] = catsize;
310 p->androidMessageSlot[p->androidFreeSlot] = strcat(p->androidMessageSlot[p->androidFreeSlot], buf);
313 p->androidMessageSlot[p->androidFreeSlot] = strdup(buf);
318 char *tt = strchr(p->androidMessageSlot[p->androidFreeSlot],
'\t');
321 tt = strchr(p->androidMessageSlot[p->androidFreeSlot],
'\t');
326 lstr = p->androidMessageSlot[p->androidFreeSlot];
328 if ( llen > p->maxLineLength){
329 char *remainder = &lstr[p->maxLineLength - 2];
330 buf = strdup(remainder);
334 p->androidMessageSlot[p->androidFreeSlot][p->maxLineLength - 2] =
'\n';
335 p->androidMessageSlot[p->androidFreeSlot][p->maxLineLength - 1] =
'\0';
347 char *ln = strchr(p->androidMessageSlot[p->androidFreeSlot],
'\n');
351 p->androidFreeSlot++;
352 if (p->androidFreeSlot >= p->maxLines) p->androidFreeSlot = 0;
353 if (p->androidMessageSlot[p->androidFreeSlot] != NULL) {
355 if(p->androidMessageSlot[p->androidFreeSlot]){
356 free(p->androidMessageSlot[p->androidFreeSlot]);
357 p->androidMessageSlot[p->androidFreeSlot] = NULL;
360 p->androidHaveUnreadMessages++;
363 more = (buf && *buf >
'\0');
366 p->androidHaveUnreadMessages = min(p->androidHaveUnreadMessages, p->maxLines -1);
368 int fwvsnprintf(
char *buffer,
int buffer_length,
const char *fmt, va_list ap)
380 tempbuf = malloc(buffer_length);
381 format = malloc(buffer_length);
387 for (j = 0; fmt[j] && fmt[j] !=
'%'; j++) {
393 count += sprintf(tempbuf,
"%s", format);
397 for (j = 0; !isalpha(fmt[j]); j++) {
399 if (j && fmt[j] ==
'%')
403 format[j + 1] =
'\0';
410 count += sprintf(tempbuf, format, i);
416 u = va_arg(ap,
unsigned);
417 count += sprintf(tempbuf, format, u);
420 c = (char)va_arg(ap,
int);
421 count += sprintf(tempbuf, format, c);
424 s = va_arg(ap,
char *);
427 if ((
int)(strlen(s) + count) > buffer_length) {
430 ltc = (int)strlen(s);
431 if (ltc > 80) ltc = 80;
432 strncpy(tmpstr, s, ltc);
433 tmpstr[ltc] =
'.'; ltc++;
434 tmpstr[ltc] =
'.'; ltc++;
435 tmpstr[ltc] =
'.'; ltc++;
438 count += sprintf(tempbuf, format, tmpstr);
440 else count += sprintf(tempbuf, format, s);
448 d = va_arg(ap,
double);
449 count += sprintf(tempbuf, format, d);
452 v = va_arg(ap,
void *);
453 count += sprintf(tempbuf, format, v);
456 count += sprintf(tempbuf,
"%d", count);
459 count += sprintf(tempbuf,
"%%");
462 ERROR_MSG(
"ConsoleMessage: invalid format specifier: %c\n", format[j]);
465 if ((
int)(strlen(tempbuf) + strlen(buffer)) < (buffer_length)-10)
467 strcat(buffer, tempbuf);
474 int ConsoleMessage0(
const char *fmt, va_list args){
481 retval = fwvsnprintf(p->FWbuffer, STRING_LENGTH - 1, fmt, args);
484 p->callback[0](p->FWbuffer);
486 p->callback[1](p->FWbuffer);
490 for(i=0;i<p->nbackB;i++)
491 p->callbackB[i](p->dataB[i],p->FWbuffer);
504 int ConsoleMessage(
const char *fmt, ...)
513 va_start( args, fmt );
514 return ConsoleMessage0(fmt,args);