Subversion Repositories Aucun

Rev

Rev 71 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
60 ixe013 1
#include <windows.h>
59 ixe013 2
#include <stdio.h>
3
#include <stdarg.h>
4
#include <string.h>
5
#include <tchar.h>
68 ixe013 6
#include "trace.h"
65 ixe013 7
#include "settings.h"
59 ixe013 8
 
60 ixe013 9
#define MAX_TRACE_BUFFER 1024
10
 
68 ixe013 11
typedef int (*WriteBufferProc)(LPCWSTR buffer);
59 ixe013 12
 
68 ixe013 13
int WriteBufferToStream(const wchar_t *buffer)
59 ixe013 14
{
68 ixe013 15
    //Targetted for release 1.5
16
    return 0;
17
}
59 ixe013 18
 
68 ixe013 19
int WriteBufferToOutputDebugString(const wchar_t *buffer)
20
{
21
    OutputDebugString(buffer);
22
    return 1;
59 ixe013 23
}
24
 
68 ixe013 25
 
26
/* -------------------------------------------------------------------------- */
27
/** @brief Reads the registry and returns a function that will do the logging
28
*
29
* @return
30
*/
31
/* ---------------------------------------------------------------------------- */
32
WriteBufferProc GetOutputWriter()
59 ixe013 33
{
68 ixe013 34
    WriteBufferProc result = 0;
60 ixe013 35
 
68 ixe013 36
    wchar_t buffer[512];
71 ixe013 37
    if (GetDebugSetting(L"Output", buffer, sizeof buffer / sizeof *buffer) == S_OK)
68 ixe013 38
    {
39
        //Is it output debug string ?
40
        if (_wcsicmp(L"OutputDebugString", buffer) == 0)
41
        {
42
            result = &WriteBufferToOutputDebugString;
43
        }
44
    }
60 ixe013 45
 
68 ixe013 46
    return result;
59 ixe013 47
}
48
 
68 ixe013 49
 
50
void Trace(const wchar_t* file, int line, const wchar_t *format, ...)
59 ixe013 51
{
68 ixe013 52
    WriteBufferProc output_proc = GetOutputWriter();
60 ixe013 53
 
68 ixe013 54
    if (output_proc)
55
    {
56
        wchar_t buffer[MAX_TRACE_BUFFER];
57
        va_list args;
59 ixe013 58
 
68 ixe013 59
        if (file && line)
60
        {
61
            swprintf_s(buffer, sizeof buffer / sizeof *buffer, L"[%s:%ld] ", file, line);
62
            output_proc(buffer);
63
        }
59 ixe013 64
 
68 ixe013 65
        va_start(args, format);
59 ixe013 66
 
68 ixe013 67
        if (format && vswprintf_s(buffer, sizeof buffer / sizeof *buffer, format, args) >= 0)
68
        {
69
            output_proc(buffer);
70
        }
71
    }
65 ixe013 72
}
73
 
71 ixe013 74
void TraceMessage(const wchar_t* file, int line, DWORD dw)
75
{
76
    LPVOID lpMsgBuf;
77
 
78
    FormatMessage(
79
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
80
        FORMAT_MESSAGE_FROM_SYSTEM,
81
        NULL,
82
        dw,
83
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
84
        (LPTSTR) &lpMsgBuf,
85
        0, NULL );
86
 
87
    Trace(file, line, L"0x%08X: %s", dw, lpMsgBuf);
88
 
89
    LocalFree(lpMsgBuf);
90
}