A multithreaded log-logging DLL

Source: Internet
Author: User
Tags export class mutex knowledge base

A multithreaded log-logging DLL

Logging is important for applications. In this paper, a simple implementation of the implementation of such a module. The module realizes the function of recording the information expected by the program. The module opens a thread for each module it registers with and creates or opens a text file with the same name with the name extension. log, located under the/log subdirectory of the program executable directory. This also has some flexibility, such as the debug version can be used to output debugging information to the file, and in release version is really used to record log information.

The following is an introduction to the interfaces of the two classes:

Cinforeport:
The class was implemented with the AFX_EXT_CLASS keyword and is a DLL export class inherited from CObject.

Data member:
 //a list of thread-holding pointers, all objects common to one, so declared as static
 static cptrlist m_lstpthreads;
 
member function:
 //registration Module
 dword Register (LPCTSTR modulenametoreg);
 //write information in which the first parameter Moudleid must be the return value of the Register function and, if NULL, the information is logged to the FatalErr.log file.
 void writeinfo (DWORD Moudleid, CString infodescribe); The
 //writes information in which the first parameter Moudleid must be the return value of the Register function and, if NULL, the information is logged to the FatalErr.log file.
 void writeinfo (DWORD Moudleid, CString infofrom, CString infodescribe);


Cwriteinfo:
This class inherits from CWinThread, which opens a thread for each registered module and maintains a log file with the extension log. Because there may be multiple threads doing the same file operation at the same time, the class also provides a mutex mechanism to ensure that file I/O is not conflicting.
Data members:
The registered module name with the same name but with the same extension
CString M_strmodulename;
CFile M_file; Log file objects
Event handles for file mutex operations
HANDLE M_heventbusy;
member functions:
GetName () and Setmodulefilename () are accessed by private member M_strmodulename.
CString GetName ();
void Setmodulefilename (CString strfilename);
Message handler function:
Process messages sent by Cinforeport::writeinfo () Tm_write_info
afx_msg void Onwriteinfo (WPARAM WPARAM, LPARAM LPARAM);

Note: Please refer to the source code, including the library itself and a test program inforeporttest. For simplicity, the OnDraw () function of the view class of the test program (a document/visual SDI program) calls the library's logging function.

Where the program needs to be perfected

The program completes in a hurry and the problem is not resolved:

1. Security checks, such as the executable file in the directory without/log subdirectories, the program will produce an exception.
2. The export class of the program is not a problem in the EXE file as a global variable, and in the DLL as a global variable application will cause no response, trace discovery is due to the creation of a new thread at:: WaitForSingleObject () at the zombie, As a local variable (the class's member variable is used with no problems).

This article is reproduced from the VC Knowledge Base, hereby declare.

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.