开发者

application logging in a pure win32 project

开发者 https://www.devze.com 2023-01-30 16:56 出处:网络
In web development I\'ve my own framework which uses a log file to help me in development and to monitor errors or production. This web framework, almost always running on *nix, uses a simple plain te

In web development I've my own framework which uses a log file to help me in development and to monitor errors or production. This web framework, almost always running on *nix, uses a simple plain text as logfile, logrotate and tail.

Today I'm creating a win32 app, which will act as TCP/IP server (this is a desktop app but embed some services) and I need to log events in order to know when things goes bad (in production AND development, to debug while I'm creating it).

I remember MFC, which I had TRACE macro. That was fine to development debug. Are there something like TRACE in pure win32 development?

To production log, what's the best approach to event logging? Windows Event Log or simple plain text files? how can I rotate it in wi开发者_JAVA百科ndows?

Thank you, Daniel Koch


For development usually it's enough to use some OutputDebugString wrap like this:

inline void logA(const char* format, ...)
{
    char buf[1024];
    wvsprintfA(buf, format, ((char*)&format) + sizeof(void*));
    OutputDebugStringA(buf);
}

#define DEBUG_LOG_A(format, ...) logA( \
        "(#" BOOST_PP_STRINGIZE( __LINE__ ) ") "__FUNCTION__ " : " \
        format, __VA_ARGS__)

And DebugView to watch it.


log4c is the C version of the widely-used log4j framework (see log4cxx above, which is for C++). You could certainly tailor this to output data wherever you wish.

It has to be compiled using GCC but once built you should be able to use the libs from a program compiled with the Microsoft toolchain.

This previous question covers some other C options.


Back then, I successfully used log4cxx.


If you can use ATL in your project, I suggest you do so, if you're not frightened by a bit of C++ templates :-)

It's a quite lightweight library, but very usefull for basic operations, you can also decide to link statically to it, wich is cool when you don't want deployment issues.

And, back to the tracing question, it has an ATLTRACE macro that's quite helpful.


When it comes to the development part, (equivalent to “TRACE”), not only that it possible but you can set colors and font styles. First, you call:

#include <conio.h>
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

To set the text color, I use:

inline void setcolor(int textcol, int backcol)
{
    if ((textcol % 16) == (backcol % 16))textcol++;
    textcol %= 16; backcol %= 16;
    unsigned short wAttributes = ((unsigned)backcol << 4) | (unsigned)textcol;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    SetConsoleTextAttribute(hConsole, wAttributes);
}

Then, just use wprintf() to display the text. If you wish to clear the display area, just call:

system("cls");

Also refresh the display by calling:

void refresh()
{
    HWND hwnd = FindWindowEx(NULL, NULL, L"CabinetWClass", NULL);
    while (hwnd != NULL)
    {
        PostMessage(hwnd, WM_COMMAND, 41504, 0);
        hwnd = FindWindowEx(NULL, hwnd, L"CabinetWClass", NULL);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消