home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ddesampl.zip / MSGLOG.C < prev    next >
C/C++ Source or Header  |  1993-11-09  |  6KB  |  281 lines

  1. /*
  2.  *    Copyright (C) 1993 IBM Corporation
  3.  *
  4.  *    DISCLAIMER OF WARRANTIES.  The following [enclosed] code is sample
  5.  *    code created by IBM Corporation.  This sample code is not part of
  6.  *    any standard or IBM product and is provided to you solely for the
  7.  *    purpose of assisting you in the development of your applications.
  8.  *    The code is provided "AS IS", without warranty of any kind.
  9.  *    IBM shall not be liable for any damages arising out of your
  10.  *    use of the sample code, even if they have been advised of the
  11.  *    possibility of such damages.
  12.  */
  13.  
  14. #define INCL_WIN
  15. #include <os2.h>
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <stdarg.h>
  19. #include <string.h>
  20. #include <io.h>
  21.  
  22.  
  23. #define    FMTTBLSIZE    16
  24. //#define    MAXLOGFILESIZE    65536L
  25.  
  26. #define    LOGFILE        "CLIENT.LOG"
  27.  
  28. #define    DATETIMEFMT    "%02d-%02d-%02d %02d:%02d:%02d.%02d"
  29.  
  30.  
  31. static FILE    *logfp = NULL;
  32.  
  33. #ifdef    MAXLOGFILESIZE
  34. static long    extraBytes = 0L;
  35. #endif
  36.  
  37. static short    been_here = 0;
  38.  
  39. static char *DdeMsgText[] = {
  40.     "WM_DDE_INITIATE",
  41.     "WM_DDE_REQUEST",
  42.     "WM_DDE_ACK",
  43.     "WM_DDE_DATA",
  44.     "WM_DDE_ADVISE",
  45.     "WM_DDE_UNADVISE",
  46.     "WM_DDE_POKE",
  47.     "WM_DDE_EXECUTE",
  48.     "WM_DDE_TERMINATE",
  49.     "WM_DDE_INITIATEACK"
  50. };
  51.  
  52. static    struct {
  53.     USHORT    status;
  54.     char    *text;
  55. }    DdeStatusText[] = {
  56.     { DDE_FACK,        "DDE_FACK"    },
  57.     { DDE_FBUSY,        "DDE_FBUSY"    },
  58.     { DDE_FNODATA,        "DDE_FNODATA"    },
  59.     { DDE_FACKREQ,        "DDE_FACKREQ"    },
  60.     { DDE_FRESPONSE,    "DDE_FRESPONSE"    },
  61.     { DDE_NOTPROCESSED,    "DDE_NOTPROCESSED" },
  62.     { DDE_FRESERVED,    "DDE_FRESERVED"    }
  63. };
  64.  
  65. static    struct {
  66.     USHORT    usFormat;
  67.     char    *text;
  68. }    DdeFormatText[ FMTTBLSIZE ];
  69.  
  70. static    USHORT    ddeFormatCnt = 0;
  71.  
  72.  
  73. #ifdef    MAXLOGFILESIZE
  74. static void
  75. wrapFile( size_t bytes )
  76. {
  77. #ifdef    OS2_1X
  78.     int    logfd = fileno( logfp );
  79. #else
  80.     int    logfd = _fileno( logfp );
  81. #endif
  82.     char tmp[ 1024 ];
  83.     
  84.     if ( bytes < sizeof( tmp ) )
  85.     {
  86.         fseek( logfp, MAXLOGFILESIZE, SEEK_SET );
  87.         bytes = fread( tmp, sizeof( char ), bytes, logfp );
  88.         rewind( logfp );
  89.         fwrite( tmp, sizeof( char ), bytes, logfp );
  90.     }
  91.     else
  92.         rewind( logfp );
  93. #ifdef    OS2_1X
  94.     chsize( logfd, MAXLOGFILESIZE );
  95. #else
  96.     _chsize( logfd, MAXLOGFILESIZE );
  97. #endif
  98. }
  99. #endif
  100.  
  101.  
  102. static char *
  103. formatStatus( USHORT fsStatus )
  104. {
  105.     static char    tmp[ 256 ];
  106.     register char    *bp = tmp;
  107.     register int    i;
  108.     USHORT        cnt = 0;
  109.  
  110.     sprintf( bp, "%d", fsStatus );
  111.  
  112.     for ( i = 0; i < sizeof(DdeStatusText)/sizeof(DdeStatusText[0]); i++ )
  113.         if ( fsStatus & DdeStatusText[i].status )
  114.         {
  115.             fsStatus &= ~DdeStatusText[i].status;
  116.             if ( cnt++ )
  117.             bp += sprintf( bp, " | " );
  118.  
  119.             bp += sprintf( bp, DdeStatusText[i].text );
  120.         }   /* if */
  121.  
  122.     if ( fsStatus )
  123.         sprintf( bp, "%s0x%04X", cnt ? " | " : "", fsStatus );
  124.  
  125.     return tmp;
  126. }
  127.  
  128.  
  129. static char *
  130. formatFormat( USHORT usFormat )
  131. {
  132.     static char szItemName[ 32 ];
  133.     register int i;
  134.  
  135.     for ( i = 0; i < ddeFormatCnt; i++ )
  136.         if ( DdeFormatText[i].usFormat == usFormat )
  137.             break;
  138.  
  139.     if ( i < ddeFormatCnt )
  140.         return ( DdeFormatText[i].text );
  141.     else
  142.     {
  143.         USHORT rc = WinQueryAtomName( WinQuerySystemAtomTable(),
  144.                       (ATOM) usFormat,
  145.                       szItemName, sizeof( szItemName ) );
  146.  
  147.         if ( !rc )
  148.         sprintf( szItemName, "%u", usFormat );
  149.         else
  150.         {
  151.         szItemName[ rc ] = 0;
  152.  
  153.         if ( ddeFormatCnt < FMTTBLSIZE )
  154.         {
  155.             DdeFormatText[ ddeFormatCnt ].text = strdup( szItemName );
  156.             if ( DdeFormatText[ ddeFormatCnt ].text != NULL )
  157.             {
  158.             DdeFormatText[ ddeFormatCnt ].usFormat = usFormat;
  159.             ddeFormatCnt++;
  160.             }
  161.         }
  162.         }
  163.     }
  164.  
  165.     return ( szItemName );
  166. }
  167.  
  168.  
  169. static void
  170. dumpDdeData( PSZ data, ULONG datalen )
  171. {
  172.     register int    i;
  173.     char        tmp[ 128 ];
  174.     register char    *cp = &tmp[16], *cp1 = tmp;
  175.     register PSZ    dp;
  176.  
  177.     for ( i = 0, dp = data; i < datalen; i++, dp++ )
  178.     {
  179.         if ( i % 16 == 0 && i )
  180.         {
  181.             cp = &tmp[16], cp1 = tmp;
  182.  
  183.             fprintf( logfp, "\t\t%*s|%.*s|  %s\n",
  184.                  6, "", 16, tmp, &tmp[16] );
  185.         }
  186.         else if ( ( i % 4 ) == 0 && i )
  187.             *cp++ = ' ';
  188.  
  189.         if ( isprint( *dp ) )
  190.             *cp1++ = *dp;
  191.         else
  192.             *cp1++ = '.';
  193.  
  194.         cp += sprintf( cp, "%02X", data[i] );
  195.     }
  196.  
  197.     if ( cp1 != tmp )
  198.     {
  199.         if ( i % 16 )
  200.         memset( cp1, '_', 16 - ( i % 16 ) );
  201.         *cp = 0;
  202.         fprintf( logfp, "\t\t%*s|%.*s|  %s\n",
  203.              6, "", 16, tmp, &tmp[16] );
  204.     }
  205. }
  206.  
  207.  
  208. void
  209. DdeMsgLog( USHORT msg, HWND hwndDdeClient, void *param )
  210. {
  211.     PDDEINIT    pddei = (PDDEINIT) param;
  212.     PDDESTRUCT    pddes = (PDDESTRUCT) param;
  213.     DATETIME    tbuf;
  214.  
  215.     if ( msg < WM_DDE_FIRST || msg > WM_DDE_LAST )
  216.         return;
  217.  
  218.     if ( !been_here )
  219.     {
  220.         been_here++;
  221.         logfp = fopen( LOGFILE, "w+b" );
  222.     }
  223.  
  224.     if ( logfp == NULL )
  225.         return;
  226.  
  227.     DosGetDateTime( &tbuf );
  228.  
  229.     fprintf( logfp, DATETIMEFMT "  " "0x%04lX %s:\t",
  230.          tbuf.year % 100, tbuf.month, tbuf.day,
  231.          tbuf.hours, tbuf.minutes, tbuf.seconds, tbuf.hundredths,
  232.          (ULONG) hwndDdeClient, DdeMsgText[ msg - WM_DDE_FIRST ] );
  233.  
  234.     switch ( msg )
  235.     {
  236.     case WM_DDE_POKE:
  237.     case WM_DDE_EXECUTE:
  238.     case WM_DDE_DATA:
  239.         fprintf( logfp, "%lu, %s, %s, %s\n",
  240.              pddes->cbData,
  241.              formatStatus( pddes->fsStatus ),
  242.              formatFormat( pddes->usFormat ),
  243.              DDES_PSZITEMNAME( pddes ) );
  244.         if ( pddes->cbData )
  245.         dumpDdeData( DDES_PABDATA( pddes ), pddes->cbData );
  246.         break;
  247.  
  248.     case WM_DDE_REQUEST:
  249.     case WM_DDE_ADVISE:
  250.     case WM_DDE_UNADVISE:
  251.     case WM_DDE_ACK:
  252.         fprintf( logfp, "%lu, %s, %s, %.*s\n",
  253.              pddes->cbData,
  254.              formatStatus( pddes->fsStatus ),
  255.              formatFormat( pddes->usFormat ),
  256.              pddes->offabData - pddes->offszItemName,
  257.              DDES_PSZITEMNAME( pddes ) );
  258.         break;
  259.  
  260.     case WM_DDE_INITIATE:
  261.     case WM_DDE_INITIATEACK:
  262.         fprintf( logfp, "%d, %s, %s\n",
  263.              pddei->cb, pddei->pszAppName, pddei->pszTopic );
  264.         break;
  265.  
  266.     case WM_DDE_TERMINATE:
  267.         fprintf( logfp, "\n" );
  268.         break;
  269.  
  270.     default:
  271.         break;
  272.     }
  273.  
  274.     fflush( logfp );
  275.  
  276. #ifdef    MAXLOGFILESIZE
  277.     if ( ( extraBytes = ftell( logfp ) - MAXLOGFILESIZE ) > 0 )
  278.         wrapFile( (size_t) extraBytes );
  279. #endif
  280. }
  281.