Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3539

Show
Ignore:
Timestamp:
05/31/08 10:36:28 (6 months ago)
Author:
keinfarbton
Message:

Add Trace.memory. This closes ticket #773.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/util/log/Trace.d

    r2809 r3539  
    99        author:         Kris 
    1010 
    11         Synchronized, formatted console output. This can be used in lieu  
     11        Synchronized, formatted console output. This can be used in lieu 
    1212        of true logging where appropriate. 
    1313 
     
    2121        Special character sequences, such as "\n", are written directly to 
    2222        the output without any translation (though an output-filter could 
    23         be inserted to perform translation as required). Platform-specific  
    24         newlines are generated instead via the formatln() method, which also  
     23        be inserted to perform translation as required). Platform-specific 
     24        newlines are generated instead via the formatln() method, which also 
    2525        flushes the output when configured to do so: 
    2626        --- 
     
    3232        Trace.format ("hello {}", "world").flush; 
    3333        --- 
    34          
     34 
    3535*******************************************************************************/ 
    3636 
     
    5858 
    5959/******************************************************************************* 
    60          
     60 
    6161        Intended for internal use only 
    62          
     62 
    6363*******************************************************************************/ 
    6464 
     
    143143                return output.write (s); 
    144144        } 
     145 
     146        /********************************************************************** 
     147 
     148                Print a range of raw memory as a hex dump. 
     149                Characters in range 0x20..0xFE are printed, all others are 
     150                shown as dots. 
     151 
     152                ---- 
     153000000:  47 49 46 38  39 61 10 00  10 00 80 00  00 48 5D 8C  GIF89a.......H]. 
     154000010:  FF FF FF 21  F9 04 01 00  00 01 00 2C  00 00 00 00  ...!.......,.... 
     155000020:  10 00 10 00  00 02 11 8C  8F A9 CB ED  0F A3 84 C0  ................ 
     156000030:  D4 70 A7 DE  BC FB 8F 14  00 3B                     .p.......; 
     157                ---- 
     158 
     159        **********************************************************************/ 
     160 
     161        final SyncPrint memory (void[] mem) 
     162        { 
     163            auto data = cast(ubyte[]) mem; 
     164            synchronized (mutex) 
     165            { 
     166                for( int row = 0; row < data.length; row += 16 ) 
     167                { 
     168                    // print relative offset 
     169                    convert.convert (&sink, "{:X6}:  ", row ); 
     170 
     171                    // print data bytes 
     172                    for( int idx = 0; idx < 16 ; idx++ ) 
     173                    { 
     174                        // print byte or stuffing spaces 
     175                        if ( idx + row < data.length ) 
     176                            convert (&sink, "{:X2} ", data[ row + idx ] ); 
     177                        else 
     178                            output.write ("   "); 
     179 
     180                        // after each 4 bytes group an extra space 
     181                        if (( idx & 0x03 ) == 3 ) 
     182                            output.write (" "); 
     183                    } 
     184 
     185                    // ascii view 
     186                    // all char 0x20..0x7e are OK for printing, 
     187                    // other values are printed as a dot 
     188                    ubyte[16] ascii = void; 
     189                    int asciiIdx; 
     190                    for ( asciiIdx = 0; 
     191                        (asciiIdx<16) && (asciiIdx+row < data.length); 
     192                        asciiIdx++ ) 
     193                    { 
     194                        ubyte c = data[ row + asciiIdx ]; 
     195                        if ( c < 0x20 || c > 0x7E ) 
     196                            c = '.'; 
     197                        ascii[asciiIdx] = c; 
     198                    } 
     199                    output.write (ascii[ 0 .. asciiIdx ]); 
     200                    output.write (Eol); 
     201                } 
     202                if (flushLines) 
     203                    output.flush; 
     204            } 
     205            return this; 
     206        } 
    145207}