Exemplo n.º 1
0
VOID SCALL LogFileTraceV(const CHAR *file, const CHAR *func, INT line, const CHAR *format, va_list argList)
{
#if 0
    CHAR        location[MAX_PATH];
#endif
    CHAR        *messageEnd;
    DWORD       errorCode;
    DWORD       processId;
    DWORD       threadId;
    LOG_ENTRY   *entry;
    size_t      remaining;

    // Preserve system error code
    errorCode = GetLastError();

    ASSERT(file != NULL);
    ASSERT(func != NULL);
    ASSERT(format != NULL);

    processId = GetCurrentProcessId();
    threadId  = GetCurrentThreadId();

    // Retrieve a spare entry structure (also checks log status)
    entry = GetSpareEntry();
    if (entry != NULL) {
        //
        // Available trace information:
        //
        // file      - Source file
        // func      - Function name in the source file
        // line      - Line number in the source file
        // processId - Current process ID
        // threadId  - Current thread ID
        //
        GetLocalTime(&entry->time);

#if 0
        StringCchPrintfA(location, ELEMENT_COUNT(location), "%s:%d", LogFileName(file), line);

        StringCchPrintfExA(entry->message, ELEMENT_COUNT(entry->message),
            &messageEnd, &remaining, 0, "%-20s - %-20s - ", location, func);
#else
        StringCchPrintfExA(entry->message, ELEMENT_COUNT(entry->message),
            &messageEnd, &remaining, 0, "%s - ", func);
#endif
        StringCchVPrintfExA(messageEnd, remaining, NULL, &remaining, 0, format, argList);

        entry->length = ELEMENT_COUNT(entry->message) - remaining;

        // Insert log entry into the write queue
        QueueInsert(entry);
    }

    // Restore system error code
    SetLastError(errorCode);
}
Exemplo n.º 2
0
/*++

TraceFormat

    Sends the message to a debugger.

Arguments:
    funct   - Pointer to a null-terminated string that specifies the function.

    format  - Pointer to a null-terminated printf-style format string.

    ...     - Arguments to insert into "format".

Return Values:
    None.

--*/
VOID CCALL TraceFormat(const char *funct, const char *format, ...)
{
    char *end;
    char output[1024];
    DWORD error;
    size_t remaining;
    va_list argList;

    ASSERT(funct != NULL);
    ASSERT(format != NULL);

    // Preserve system error code
    error = GetLastError();

    StringCchPrintfExA(output, ELEMENT_COUNT(output), &end, &remaining, 0,
        "| %4d | %17s | ", GetCurrentThreadId(), funct);
    va_start(argList, format);
    StringCchVPrintfA(end, remaining, format, argList);
    va_end(argList);

    OutputDebugStringA(output);

    // Restore system error code
    SetLastError(error);
}
Exemplo n.º 3
0
BOOL CLIENT::LogMessageV(PCHAR pszMsg, ...)
{
    DWORD cbWritten = 0;
    CHAR szBuf[1024];
    PCHAR pcchEnd = szBuf + ARRAYSIZE(szBuf) - 2;
    PCHAR pcchCur = szBuf;
    HRESULT hr;

    va_list args;
    va_start(args, pszMsg);
    hr = StringCchVPrintfExA(pcchCur, pcchEnd - pcchCur,
                             &pcchCur, NULL, STRSAFE_NULL_ON_FAILURE,
                             pszMsg, args);
    va_end(args);
    if (FAILED(hr)) {
        goto cleanup;
    }

    hr = StringCchPrintfExA(pcchCur, szBuf + (ARRAYSIZE(szBuf)) - pcchCur,
                            &pcchCur, NULL, STRSAFE_NULL_ON_FAILURE,
                            "\n");

  cleanup:
    WriteFile(hFile, szBuf, (DWORD)(pcchCur - szBuf), &cbWritten, NULL);
    return TRUE;
}
Exemplo n.º 4
0
static VOID CCALL QueueWrite(VOID *context)
{
    CHAR        buffer[128];
    DWORD       written;
    HANDLE      file;
    LOG_ENTRY   *entry;
    size_t      remaining;

    UNREFERENCED_PARAMETER(context);

    // Log system must be inactive or shutting down (flush)
    if (logStatus != LOG_STATUS_ACTIVE && logStatus != LOG_STATUS_SHUTDOWN) {
        return;
    }

    file = FileOpen(logPath, GENERIC_WRITE, FILE_SHARE_READ, 10);
    if (file == INVALID_HANDLE_VALUE) {
        return;
    }

    // Process all queued log entries
    for (;;) {
        entry = GetQueueEntry();
        if (entry == NULL) {
            break;
        }

        // Format the time stamp
        StringCchPrintfExA(buffer, ELEMENT_COUNT(buffer), NULL, &remaining, 0,
            "%02d-%02d-%04d %02d:%02d:%02d ",
            entry->time.wMonth, entry->time.wDay, entry->time.wYear,
            entry->time.wHour, entry->time.wMinute, entry->time.wSecond);

        // Seek to the end of the log file
        SetFilePointer(file, 0, 0, FILE_END);

        if (!WriteFile(file, buffer, ELEMENT_COUNT(buffer) - remaining, &written, NULL)) {
            TRACE("Unable to write log file (error %lu).", GetLastError());
        }
        if (!WriteFile(file, entry->message, entry->length, &written, NULL)) {
            TRACE("Unable to write log file (error %lu).", GetLastError());
        }

        MemFree(entry);
    }

    CloseHandle(file);
}
Exemplo n.º 5
0
 void operator()(const char* pszFormat, ...)
 {
     ASSERT_ISNOTNULL(pszFormat);
     char szFormatBuffer[512] = { 0 };
     
     StringCchPrintfExA(szFormatBuffer, 512, NULL, NULL,
         STRSAFE_NULL_ON_FAILURE, "%s (%u) : %s", m_pszFile, m_uLine,
         pszFormat);
     
     va_list args;
     va_start(args, pszFormat);
     char szBuffer[4096] = { 0 };
     
     StringCchVPrintfExA(szBuffer, 4096, NULL, NULL,
         STRSAFE_NULL_ON_FAILURE, szFormatBuffer, args);
     
     Tracer::Output(szBuffer);
     va_end(args);              
 }
Exemplo n.º 6
0
char *
mit_krb5_pkinit_cert_hash_str(const mit_krb5_data *cert)
{
#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H

    CC_SHA1_CTX ctx;
    char *outstr, *cpOut;
    unsigned char digest[CC_SHA1_DIGEST_LENGTH];
    unsigned i;
    
    LOG_ENTRY();

    CC_SHA1_Init(&ctx);
    CC_SHA1_Update(&ctx, cert->data, cert->length);
    CC_SHA1_Final(digest, &ctx);
    
    cpOut = outstr = (char *)malloc((2 * CC_SHA1_DIGEST_LENGTH) + 1);
    if(outstr == NULL)
	return NULL;

    for(i = 0; i < CC_SHA1_DIGEST_LENGTH; i++, cpOut += 2)
	sprintf(cpOut, "%02X", (unsigned)digest[i]);
    *cpOut = '\0';
    return outstr;

#elif defined(_WIN32)

    HCRYPTPROV  hProv = 0;
    HCRYPTHASH  hHash = 0;
    char        *outstr = NULL;
    char        *outpos;
    size_t      cch_left;
    BYTE        *hash = NULL;
    DWORD       hashSize = 0;
    DWORD       len, i;

    LOG_ENTRY();

    if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_SIG, CRYPT_VERIFYCONTEXT)) {
        LOG_LASTERROR("CryptAcquireContext failed");
        goto done;
    }

    if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash)) {
        LOG_LASTERROR("CryptCreateHash failed");
        goto done;
    }

    if (!CryptHashData(hHash, (BYTE *) cert->data, cert->length, 0)) {
        LOG_LASTERROR("CryptHashData failed");
        goto done;
    }

    len = sizeof(hashSize);
    if (!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *) &hashSize, &len, 0)) {
        LOG_LASTERROR("CryptGetHashParam failed while getting hash size");
        goto done;
    }

    hash = malloc(hashSize);
    if (hash == NULL) {
        goto done;
    }

    len = hashSize;
    if (!CryptGetHashParam(hHash, HP_HASHVAL, hash, &len, 0)) {
        LOG_LASTERROR("CryptGetHashParam failed while getting hash");
        goto done;
    }

    outstr = malloc(hashSize * 2 + 1);
    if (outstr == NULL) {
        goto done;
    }

    outpos = outstr;
    cch_left = hashSize * 2 + 1;
    for (i = 0; i < hashSize; i++) {
        StringCchPrintfExA(outpos, cch_left, &outpos, &cch_left, STRSAFE_FILL_ON_FAILURE,
                           "%02X", (unsigned) hash[i]);
    }

    *outpos = '\0';

done:

    if (hHash != 0)
        CryptDestroyHash(hHash);

    if (hProv != 0)
        CryptReleaseContext(hProv, 0);

    if (hash != NULL)
        free(hash);

    return outstr;

#endif
}