How To Make Logs

Table of contents:

How To Make Logs
How To Make Logs

Video: How To Make Logs

Video: How To Make Logs
Video: How to setup and use an Audit Logging Channel in Discord! 2024, November
Anonim

One of the main ways to diagnose malfunctions in the operation of software already running on the user's computer is to keep event logs - logs. Typically, they record information about the launches, as well as important information about the state of the process and the system environment in the event of a critical failure. You can create logs both by your own means and using special services of operating systems.

How to make logs
How to make logs

It is necessary

  • - translator from the used programming language;
  • - possibly a Windows Platform SDK;
  • - possibly a development package for glibc.

Instructions

Step 1

Analyze the terms of use and make up the requirements for the developed subsystem, component or library that will make logs. Answer the questions about what platform or platforms it should operate on, what its API will be.

Step 2

In accordance with the identified functioning features and the provided API, create a template for the logging subsystem. Start implementing its functionality.

Step 3

The simplest option for logging is to independently create files in a location determined by the application configuration, and then write data in an arbitrary format to them. Use C Standard Library functions (fopen, fclose, fwrite), C ++ Standard Library stream objects (ofstream), framework classes (such as CFile, QFile), or operating system API functions (CreateFile, WriteFile on Windows) for this.

Step 4

Implement logging using the syslog API on UNIX-compatible operating systems. The syslog API functions are declared in the syslog.h header file. Connect it in the right place in the source code of your project.

Step 5

Connect to the syslog service using the openlog function call. As parameters, pass it a pointer to a string containing the identifier of the application or component that will write, option flags and a mask of events to be passed to the log. Use calls to the syslog and vsyslog functions to add entries to the log. Call the closelog function to disconnect from the service. A simple example of syslog code might be: openlog ("prefix", LOG_NDELAY | LOG_CONS | LOG_PID, LOG_LOCAL1); syslog (LOG_INFO, "% s", "Info"); syslog (LOG_NOTICE, "% s", " Notice "); closelog (); It makes sense to connect to syslog when initializing the application, and disconnect when shutting down.

Step 6

On Windows operating systems, use the EventLog API to add entries to the system logs. Call RegisterEventSource to get the log descriptor on the specified machine. Use this handle when calling the ReportEvent function that writes to the log. When finished, call DeregisterEventSource to close the connection and release the resources allocated by RegisterEventSource. The simplest example of working with EventLog might be: HANDLE h =:: RegisterEventSource (NULL, "AnySource"); ASSERT (h! = NULL);:: ReportEvent (h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 3, 0, "Text1Text2Text3", NULL);:: DeregisterEventSource (h); As with syslog, it makes sense to call RegisterEventSource at start and DeregisterEventSource at application shutdown.

Recommended: