Output debug information to the console or file

Source: Internet
Author: User

Output debug information to the console or file

We often encounter this situation. The planners ran to say that the program had gone wrong, and then gave a great description of the program without making the problem clear. If the program running information can be automatically recorded, it is much easier to find bugs, that is, the log function.

In addition, for debugging programs, you often need to input some debugging information in the program, so that you can more easily find bugs. The debugging information can only be seen in the debugging mode of the editor, or by using third-party software such as debugview, but it is restricted by the compiling environment, and it is impossible to run the program with debugview every time... It is much easier to automatically record debugging information to log files.

In debugging mode, the debugger obtains information because it captures the output information of outputdebugstring () in the program. Therefore, if we can write a program to capture the output information of the outputdebugstring () function, we can solve the above requirements. For the principle of the outputdebugstring () function, refer to http://www.unixwiz.net/techtips/outputdebugstring.html.

After understanding the principles, it is easy to implement them. The code below is as follows:

1 const max_debugbuffer = 4096; // the data transmitted between the application and the debugger is completed through a 4 kb shared memory block.
2
3 typedef struct dbwin_buffer {
4 DWORD dwprocessid;
5 char data [4096-sizeof (DWORD)];
6} debugbuffer, * pdebugbuffer;
7
8 // thread Functions
9 void winapi debugtrackproc (pvoid pvparam)
10 {
11 handle hmapping = NULL;
12 handle hackevent = NULL;
13 handle hreadyevent = NULL;
14 pdebugbuffer pdbbuffer = NULL;
15 tchar tzbuffer [max_debugbuffer];
16
17 // open the event handle
18 hackevent = createevent (null, false, false, text ("dbwin_buffer_ready "));
19 if (hackevent = NULL)
20 {
21 closehandle (hackevent );
22 return;
23}
24
25 hreadyevent = createevent (null, false, false, text ("dbwin_data_ready "));
26 if (hreadyevent = NULL)
27 {
28 closehandle (hreadyevent );
29 return;
30}
31
32 // Create File ing
33 hmapping = createfilemapping (invalid_handle_value, null, page_readwrite, 0, max_debugbuffer, text ("dbwin_buffer "));
34 if (hmapping = NULL)
35 {
36 closehandle (hmapping );
37 return;
38}
39
40 // map the debugging Buffer
41 pdbbuffer = (pdebugbuffer) mapviewoffile (hmapping, file_map_read, 0, 0, 0 );
42
43 // Loop
44 While (true)
45 {
46 // activate the event
47 setevent (hackevent );
48 // wait for the buffer data
49 If (waitforsingleobject (m_hreadyevent, infinite) = wait_object_0)
50 {
51 // Save the information. This is what we want. With this information, you can log it or output it to the console.
52 tzbuffer = pdbbuffer-> szstring;
53}
54}
55
56 // release
57 if (pdbbuffer)
58 {
59 unmapviewoffile (pdbbuffer );
60}
61 closehandle (hmapping );
62 closehandle (hreadyevent );
63 closehandle (hackevent );
64}

Copyright Disclaimer: This article is an original article and can be reproduced. However, you must use a hyperlink to indicate the original source and author information of the article. Please respect your labor achievements. Thank you!
Xiao Xiang's blog http://xfxsworld.cnblogs.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.