⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <dos.h>
#include <sys/stat.h>
#include "tinyio.h"
#include "wdebug.h"
#include "drwatcom.h"
#include "commdlg.h"
#include "jdlg.h"

static void logPrint( char *str, ... );

BOOL    notesAdded;
static loginfo  logInfo;

/*
 * getNewLogName
 */
static BOOL getNewLogName( HWND parent ) {
    OPENFILENAME        of;
    BOOL                ret;
    char                filter[100];
    char                *ptr;

    memset( &of, 0, sizeof( OPENFILENAME ) );
    of.lStructSize = sizeof( OPENFILENAME );
    of.hwndOwner = parent;
    of.hInstance = Instance;
    of.lpstrDefExt = NULL;
    of.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
    of.lpstrFile = logInfo.filename;
    of.nMaxFile = MAX_FNAME;
    CopyRCString( STR_ALL_FILE_FILTER, filter, sizeof( filter ) );
    ptr = filter + strlen( filter ) + 1;
    strcpy( ptr, "*.*" );
    ptr += strlen( ptr ) + 1;
    *ptr = '\0';
    of.lpstrFilter = filter;
    of.nFilterIndex = 1L;
    of.lpstrTitle = AllocRCString( STR_LOG_FILENAME );
    ret = GetSaveFileName( &of );
    FreeRCString( (char *)of.lpstrTitle );
    return( ret );
}

/*
 * LogDialog - show task status
 */
BOOL __export FAR PASCAL LogDialog( HWND hwnd, WORD msg, WORD wparam,
                                    DWORD lparam )
{
    int         i;

    lparam = lparam;

    switch( msg ) {
    case WM_INITDIALOG:
    {
        char tmp[128];

        logInfo = LogInfo;
        for( i=0;i<LOGFL_MAX;i++ ) {
            CheckDlgButton( hwnd, LOG_STACK_TRACE+i,
                                logInfo.flags[i] == '1' );
        }
        #if 0
            SetCourierFont( hwnd, LOG_FILE_NAME );
            SetCourierFont( hwnd, LOG_MAX_FILE_SIZE );
            SetCourierFont( hwnd, LOG_DISASM_BACKUP );
            SetCourierFont( hwnd, LOG_DISASM_LINES );
        #endif
        SetDlgItemText( hwnd, LOG_FILE_NAME, strlwr( logInfo.filename ) );
        ltoa( logInfo.maxlogsize, tmp, 10 );
        SetDlgItemText( hwnd, LOG_MAX_FILE_SIZE, tmp );
        itoa( logInfo.disasmbackup, tmp, 10 );
        SetDlgItemText( hwnd, LOG_DISASM_BACKUP, tmp );
        itoa( logInfo.disasmlines, tmp, 10 );
        SetDlgItemText( hwnd, LOG_DISASM_LINES, tmp );
        return( TRUE );
    }
    case WM_CLOSE:
        PostMessage( hwnd, WM_COMMAND, IDCANCEL, 0L );
        return( TRUE );
    case WM_COMMAND:
        if( wparam >= LOG_STACK_TRACE && wparam <= LOG_MAXFL ) {
            i = wparam - LOG_STACK_TRACE;
            if( logInfo.flags[i] == '1' ) {
                logInfo.flags[i] = '0';
            } else {
                logInfo.flags[i] = '1';
            }
            CheckDlgButton( hwnd, wparam, logInfo.flags[i] == '1' );
            if( i == LOGFL_MOD_SEGMENTS
               && logInfo.flags[ LOGFL_MOD_SEGMENTS ] == '1' ) {
                logInfo.flags[ LOGFL_MODULES ] = '1';
                CheckDlgButton( hwnd, LOG_STACK_TRACE + LOGFL_MODULES, TRUE );
            }
            if( i == LOGFL_MODULES && logInfo.flags[ LOGFL_MODULES ] == '0' )
            {
                logInfo.flags[ LOGFL_MOD_SEGMENTS ] = '0';
                CheckDlgButton( hwnd, LOG_STACK_TRACE + LOGFL_MOD_SEGMENTS,
                                FALSE );
            }
            return( TRUE );
        }
        switch( wparam ) {
        case IDOK:
        {
            char        tmp[128];

            GetDlgItemText( hwnd, LOG_FILE_NAME, (LPSTR) logInfo.filename,
                                MAX_FNAME );
            GetDlgItemText( hwnd, LOG_MAX_FILE_SIZE, (LPSTR) tmp, 128 );
            logInfo.maxlogsize = atol( tmp );
            GetDlgItemText( hwnd, LOG_DISASM_BACKUP, (LPSTR) tmp, 128 );
            logInfo.disasmbackup = atoi( tmp );
            GetDlgItemText( hwnd, LOG_DISASM_LINES, (LPSTR) tmp, 128 );
            logInfo.disasmlines = atoi( tmp );
            LogInfo = logInfo;
            EndDialog( hwnd, 0 );
            return( TRUE );
        }
        case IDCANCEL:
            EndDialog( hwnd, 0 );
            return( TRUE );
        case LOG_BROWSE:
            if( getNewLogName( hwnd ) ) {
                strlwr( logInfo.filename );
                SetDlgItemText( hwnd, LOG_FILE_NAME, logInfo.filename );
            }
            return( TRUE );
        }
    }
    return( FALSE );

} /* LogDialog */

/*
 * DoLogDialog - start log info dialog
 */
void DoLogDialog( HWND hwnd )
{
    FARPROC     fp;

    fp = MakeProcInstance( (FARPROC)LogDialog, Instance );
    JDialogBox( Instance, "LOG", hwnd, (DLGPROC)fp );
    FreeProcInstance( fp );

} /* DoLogDialog */

#define BUFF_SIZE       1024
static int              logFile;
static char             *workBuff;
static GLOBALHANDLE     workHandle;
static int              buffPos;

/*
 * startLogFile - start log for this session
 */
static BOOL startLogFile( void )
{
    tiny_ret_t  rc;

    rc = TinyOpen( LogInfo.filename, TIO_WRITE );
    if( TINY_ERROR( rc ) ) {
        rc = TinyCreate( LogInfo.filename, 0 );
        logFile = TINY_INFO( rc );
        TinyClose( logFile );
        rc = TinyOpen( LogInfo.filename, TIO_WRITE );
        if( TINY_ERROR( rc ) ) {
            return( FALSE );
        }
    }
    logFile = TINY_INFO( rc );

    workHandle = GlobalAlloc( GMEM_FIXED, BUFF_SIZE );
    workBuff = GlobalLock( workHandle );
    if( logFile == -1 || workBuff == NULL ) {
        if( workBuff != NULL ) {
            GlobalUnlock( workHandle );
            GlobalFree( workHandle );
        }
        return( FALSE );
    }
    TinySeek( logFile, 0L, TIO_SEEK_END );
    return( TRUE );

} /* startLogFile */

/*
 * logFlush - flush the log file
 */
static void logFlush( void )
{
    TinyWrite( logFile, workBuff, buffPos );
    buffPos = 0;

} /* logFlush */

/*
 * finishLogFile - close up log file
 */
static void finishLogFile( void )
{

    logFlush();
    TinyClose( logFile );
    GlobalUnlock( workHandle );
    GlobalFree( workHandle );
    workBuff = NULL;

} /* finishLogFile */

/*
 * dologPrint - print to the log file
 */
static void doLogPrint( char *str, va_list al )
{
    char        tmp[256];
    int         len,i;

    vsprintf( tmp, str, al );
    len = strlen( tmp );

    for( i=0;i<len;i++ ) {
        if( tmp[i] == '\n' ) {
            workBuff[ buffPos++ ] = '\r';
        }
        workBuff[ buffPos++ ]  = tmp[i];
        if( buffPos >= BUFF_SIZE-3 ) {
            logFlush();
        }
    }

} /* logPrint */

/*
 * logPrint
 */
static void logPrint( char *str, ... )
{
    va_list     al;

    va_start( al, str );
    doLogPrint( str, al );
    va_end( al );
}

/*
 * rcLogPrint
 */
static void rcLogPrint( DWORD msgid, ... )
{
    va_list     al;
    char        *str;

    str = AllocRCString( msgid );
    va_start( al, msgid );
    doLogPrint( str, al );
    va_end( al );
    FreeRCString( str );
}

void logComment( char *str ) {

    if( !notesAdded ) {
        rcLogPrint( STR_USER_NOTES_ULINE );
        rcLogPrint( STR_USER_NOTES );
        rcLogPrint( STR_USER_NOTES_ULINE );
        notesAdded = TRUE;
    }
    logPrint( "%s\n", str );
}

/*
 * formatSel - format selector info
 */
static void formatSel( char *which, WORD sel )
{
    descriptor  desc;
    DWORD       base;
    DWORD       limit;

    GetADescriptor( sel, &desc );
    if( which != NULL ) {
        logPrint( "      %s = %04x  ", which, sel );
        if( sel == 0L ) {
            logPrint( "********  ********  ****  ****  *   ***    *\n" );
            return;
        }
    } else {
        logPrint( " %04x  ", sel );
    }

    base = GET_DESC_BASE( desc );
    limit = GET_DESC_LIMIT( desc );
    logPrint( "%08lx  %08lx  ", base, limit );
    if( desc.granularity ) {
        logPrint( "page  " );
    } else {
        logPrint( "byte  " );
    }
    if( desc.type == 2 ) {
        logPrint( "data  " );
    } else {
        logPrint( "code  " );
    }
    logPrint( "%1d   ", (WORD) desc.dpl );
    if( desc.type == 2 )  {
        logPrint( "R" );
        if( desc.writeable_or_readable ) {
            logPrint( "/W" );
        } else {
            logPrint( "  " );
        }
        logPrint( "    " );
    } else {
        logPrint( "Ex" );
        if( desc.writeable_or_readable ) {
            logPrint( "/R" );
        } else {
            logPrint( "  " );
        }
        logPrint( "   " );
    }
    if( desc.big_or_default ) {
        logPrint( "Y\n" );
    } else {
        logPrint( " \n" );
    }

} /* formatSel */

/*
 * logDisasm - log some disassembly
 */
static void logDisasm( WORD CS, DWORD EIP )
{
    int         i;
    ADDRESS     addr;
    char        str[256];

    addr.seg = CS;
    addr.offset = EIP;
    InstructionBackup( LogInfo.disasmbackup, &addr );
    for( i=0;i<LogInfo.disasmlines; i++ ) {
        if( addr.offset == EIP ) {
            logPrint( "--->" );
        } else {
            logPrint( "    " );
        }
        addr.offset += Disassemble( &addr, str, TRUE );
        logPrint( "%s\n", str );
    }

} /* logDisasm */

/*
 * logSysInfo - record basic system info
 */
static void logSysInfo( BOOL wasfault )
{
    char        *s;
    time_t      tod;
    WORD        ver;

    tod = time( NULL );
    s = ctime( &tod );
    if( wasfault ) {
        rcLogPrint( STR_DETECTED_FAULT_ON, AppName, s );
    } else {
        rcLogPrint( STR_LOG_TAKEN_ON, AppName, s );
    }

    ver = GetVersion();
    rcLogPrint( STR_WIN_VER_IS, (WORD) ver&0xff, (WORD)ver>>8 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -