Ignore:
Timestamp:
Jul 11, 2012, 3:01:20 PM (8 years ago)
Author:
wisk
Message:

on win32, use a named pipe and IOCP to read stdout, stderr and debugfd correctly.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/libzzuf/debug.c

    r4673 r4827  
    6666size_t debugcount = 1;
    6767
     68#ifdef _WIN32
     69
     70CRITICAL_SECTION _zz_pipe_cs; /* Initialized in DllMain */
     71
     72void _zz_debug(char const *format, ...)
     73{
     74    va_list args;
     75    char buf[0x100];
     76    DWORD written;
     77    va_start(args, format);
     78    //if (_zz_debuglevel >= 1) // LATER:
     79    {
     80        HANDLE dbg_hdl = (HANDLE)_get_osfhandle(_zz_debugfd);
     81        int ret = _vsnprintf(buf, sizeof(buf), format, args);
     82
     83        if (ret <= 0)       return;  /* if _snprintf failed, we send nothing                    */
     84        if (buf[0] == '\0') return; /* if the buf is empty, we don't bother to send it to zzuf */
     85
     86        /* FIXME: if len >= count, no null-terminator is appended, so we may erased the last character */
     87        if (ret >= sizeof(buf)) buf[ret - 1] = '\n';
     88        else                    buf[ret++]   = '\n';
     89
     90        EnterCriticalSection(&_zz_pipe_cs);
     91        WriteFile(dbg_hdl, buf, ret, &written, NULL);
     92        LeaveCriticalSection(&_zz_pipe_cs);
     93    }
     94    va_end(args);
     95    fflush(NULL); /* flush all streams to make sure zzuf gotta catch 'em all */
     96}
     97
     98void _zz_debug2(char const *format, ...)
     99{
     100    va_list args;
     101    char buf[0x100];
     102    DWORD written;
     103    va_start(args, format);
     104    //if (_zz_debuglevel >= 1) // LATER:
     105    {
     106        HANDLE dbg_hdl = (HANDLE)_get_osfhandle(_zz_debugfd);
     107        int ret = _vsnprintf(buf, sizeof(buf), format, args);
     108
     109        if (ret <= 0)       return;  /* if _snprintf failed, we send nothing                    */
     110        if (buf[0] == '\0') return; /* if the buf is empty, we don't bother to send it to zzuf */
     111
     112        /* FIXME: if len >= count, no null-terminator is appended, so we may erased the last character */
     113        if (ret >= sizeof(buf)) buf[ret - 1] = '\n';
     114        else                    buf[ret++]   = '\n';
     115
     116        EnterCriticalSection(&_zz_pipe_cs);
     117        WriteFile(dbg_hdl, buf, ret, &written, NULL);
     118        LeaveCriticalSection(&_zz_pipe_cs);
     119    }
     120    va_end(args);
     121    fflush(NULL); /* flush all streams to make sure zzuf gotta catch 'em all */
     122}
     123#else
    68124void _zz_debug(char const *format, ...)
    69125{
     
    83139    va_end(args);
    84140}
     141#endif
    85142
    86143/**
Note: See TracChangeset for help on using the changeset viewer.