2 * $Id: util.c,v 1.10 2007-10-16 12:36:29 nkeynes Exp $
4 * Miscellaneous utility functions.
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
25 #include "sh4/sh4core.h"
27 char *msg_levels[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
28 int global_msg_level = EMIT_WARN;
30 void fwrite_string( const char *s, FILE *f )
34 fwrite( &len, sizeof(len), 1, f );
37 fwrite( &len, sizeof(len), 1, f );
38 fwrite( s, len, 1, f );
42 int fread_string( char *s, int maxlen, FILE *f )
45 fread( &len, sizeof(len), 1, f );
47 fread( s, len > maxlen ? maxlen : len, 1, f );
52 void fwrite_dump( unsigned char *data, unsigned int length, FILE *f )
55 for( i =0; i<length; i+=16 ) {
56 fprintf( f, "%08X:", i);
57 for( j=i; j<i+16; j++ ) {
61 fprintf( f, " %02X", (unsigned int)(data[j]) );
66 for( j=i; j<i+16 && j<length; j++ ) {
67 fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
73 void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f )
75 fwrite_dump32v( data, length, 8, f );
78 void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f )
81 for( i =0; i<length>>2; i+=wordsPerLine ) {
82 fprintf( f, "%08X:", i);
83 for( j=i; j<i+wordsPerLine; j++ ) {
85 fprintf( f, " %08X", (unsigned int)(data[j]) );
94 void log_message( void *ptr, int level, const gchar *source, const char *msg, ... )
96 char buf[20], addr[10] = "", *p;
97 const gchar *arr[4] = {buf, source, addr};
99 time_t tm = time(NULL);
102 if( level > global_msg_level ) {
108 if( level <= EMIT_ERR ) {
109 gchar *text = g_strdup_vprintf( msg, ap );
110 if( gui_error_dialog( text ) ) {
119 strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
120 fprintf( stderr, "%s %08X %-5s ", buf, sh4r.pc, msg_levels[level] );
121 vfprintf( stderr, msg, ap );
123 fprintf( stderr, "\n" );
.