Subversion Repositories Aucun

Rev

Rev 220 | 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
 
220 ixe013 9
#define MAX_TRACE_BUFFER 2048
60 ixe013 10
 
68 ixe013 11
typedef int (*WriteBufferProc)(LPCWSTR buffer);
59 ixe013 12
 
215 ixe013 13
int WriteBufferToStream(const wchar_t* buffer)
59 ixe013 14
{
215 ixe013 15
    //Targetted for release 1.5
16
    return 0;
68 ixe013 17
}
59 ixe013 18
 
215 ixe013 19
int WriteBufferToOutputDebugString(const wchar_t* buffer)
68 ixe013 20
{
215 ixe013 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
/* ---------------------------------------------------------------------------- */
122 ixe013 32
WriteBufferProc GetOutputWriter(int level)
59 ixe013 33
{
215 ixe013 34
    WriteBufferProc result = 0;
35
    wchar_t buffer[512];
219 ixe013 36
    int reglevel = eNONE;
60 ixe013 37
 
215 ixe013 38
    if (GetDebugSetting(L"Level", buffer, sizeof buffer / sizeof * buffer) == S_OK)
39
    {
219 ixe013 40
        if (_wcsicmp(L"Debug", buffer) == 0)
215 ixe013 41
        {
219 ixe013 42
            reglevel = eDEBUG;
215 ixe013 43
        }
44
        else if (_wcsicmp(L"Error", buffer) == 0)
45
        {
46
            reglevel = eERROR;
47
        }
48
        else if (_wcsicmp(L"Warning", buffer) == 0)
49
        {
50
            reglevel = eWARN;
51
        }
52
        else if (_wcsicmp(L"Information", buffer) == 0)
53
        {
54
            reglevel = eINFO;
55
        }
56
        else
57
        {
58
            reglevel = eDEBUG;
59
        }
219 ixe013 60
    }
122 ixe013 61
 
219 ixe013 62
    if (level >= reglevel)
63
    {
64
        //TODO put code back when multiple ouput proc will be available
65
        /*
66
        if (GetDebugSetting(L"Output", buffer, sizeof buffer / sizeof * buffer) == S_OK)
215 ixe013 67
        {
219 ixe013 68
            //Is it output debug string ?
69
            if (_wcsicmp(L"OutputDebugString", buffer) == 0)
122 ixe013 70
            {
219 ixe013 71
                result = &WriteBufferToOutputDebugString;
122 ixe013 72
            }
215 ixe013 73
        }
219 ixe013 74
        /*/
75
        result = &WriteBufferToOutputDebugString;
76
        //*/
215 ixe013 77
    }
122 ixe013 78
 
215 ixe013 79
    return result;
59 ixe013 80
}
81
 
68 ixe013 82
 
215 ixe013 83
void Trace(int level, const wchar_t* file, const char* function, int line, const wchar_t* format, ...)
59 ixe013 84
{
215 ixe013 85
    WriteBufferProc output_proc = GetOutputWriter(level);
60 ixe013 86
 
215 ixe013 87
    if (output_proc)
88
    {
89
        wchar_t buffer[MAX_TRACE_BUFFER];
59 ixe013 90
 
215 ixe013 91
        if (file && line)
92
        {
93
            swprintf_s(buffer, sizeof buffer / sizeof * buffer, L"[%s:%S:%ld] (%d) ", file, function, line, level);
94
            output_proc(buffer);
95
        }
59 ixe013 96
 
220 ixe013 97
        if(format && *format)
98
        {
99
            va_list args;
100
            va_start(args, format);
59 ixe013 101
 
220 ixe013 102
            if (vswprintf_s(buffer, sizeof buffer / sizeof * buffer, format, args) >= 0)
103
            {
104
                output_proc(buffer);
105
            }
106
 
107
            va_end(args);
215 ixe013 108
        }
109
    }
65 ixe013 110
}
111
 
160 ixe013 112
void TraceMessage(const wchar_t* file, const char* function, int line, DWORD dw)
122 ixe013 113
{
215 ixe013 114
    LPVOID lpMsgBuf;
115
    FormatMessage(
116
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
117
        FORMAT_MESSAGE_FROM_SYSTEM,
118
        NULL,
119
        dw,
120
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
121
        (LPTSTR) &lpMsgBuf,
122
        0, NULL);
217 ixe013 123
 
124
    //TODO : Do not hardcode eERROR here
215 ixe013 125
    Trace(eERROR, file, function, line, L"0x%08X: %s", dw, lpMsgBuf);
126
    LocalFree(lpMsgBuf);
71 ixe013 127
}
224 ixe013 128