How to do logs

How to do logs

One of the main ways of diagnostics of faults in work of the software which is already working on the user's computer is maintaining event logs - logs. Usually in them data on starts and also important information on a status of process and a system environment in case of critical failure are entered. It is possible to do logs as own means, and through special services of operating systems.

It is required to you

  • - the translator from the used programming language;
  • - perhaps, Windows Platform SDK package;
  • - perhaps, a package of the developer for glibc.

Instruction

1. Analyze terms of use and make requirements to the developed subsystem, a component or library which will do logs. Answer questions of under the administration of what platform or platforms it should function what its application programming interface will be.

2. According to the revealed features of functioning and the provided API create preparation of a subsystem of maintaining dens. Start implementation of its functionality.

3. The simplest option of maintaining dens is independent creation of files in the place determined by an application configuration with the subsequent record of data of any format in them. Apply for this purpose functions of standard library C (fopen, fclose, fwrite), objects of flows of standard library C ++ (ofstream), classes of the used framework (such as CFile, QFile) or the API function of the operating system (CreateFile, WriteFile in Windows).

4. Implement record of dens by means of the program interface of syslog service in UNIX - compatible operating systems. The API syslog functions are declared in the heading syslog.h file. Connect it in the right place of the source code of the project.

5. Be connected to syslog service, using function call of openlog. As parameters transfer it the pointer to the line containing the identifier of the application or a component which will carry out record, flags of options and a mask of the events which are missed in the log. Use calls of the syslog and vsyslog functions for adding of logging. For disconnection from service cause the closelog function. The simple code sample for work with syslog can be such: openlog (""prefix"", LOG_NDELAY | LOG_CONS | LOG_PID, LOG_LOCAL1); syslog (LOG_INFO, "" %s"", ""Info""); syslog (LOG_NOTICE, "" %s"", ""Notice""); closelog (); It makes to Be connected sense to syslog at initialization of the application, and to be disconnected - at completion of work.

6. In operating systems of Windows use API for work with EventLog service to add entries in system logs. Make RegisterEventSource call for receiving a descriptor of the log on the specified machine. Use this descriptor at function calls of ReportEvent which is carrying out logging. After completion of work cause DeregisterEventSource to close connection and to release the resources selected with RegisterEventSource. The simplest example of work with EventLog can be such: HANDLE h =:: RegisterEventSource (NULL, ""AnySource""); ASSERT (h! = NULL);:: ReportEvent (h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 3, 0, ""Text1Text2Text3"", NULL);:: DeregisterEventSource(h); As well as in a case with syslog, it makes sense to cause RegisterEventSource at the beginning, and DeregisterEventSource - at completion of operation of application.

Author: «MirrorInfo» Dream Team


Print