How To Write Logs

Table of contents:

How To Write Logs
How To Write Logs

Video: How To Write Logs

Video: How To Write Logs
Video: Logarithms - The Easy Way! 2024, April
Anonim

Today, most application and system applications periodically save information about the process of their work, errors and failures in special logs called logs. Most general-purpose operating systems provide services that allow you to write logs using a standard programming interface.

How to write logs
How to write logs

Necessary

  • - C compiler;
  • - Windows Platform SDK;
  • - Develop package for glibc.

Instructions

Step 1

Add support for writing logs to the system log from your application designed to run on operating systems of the Windows family.

Use the RegisterEventSource API function to register the application as an event source, the ReportEvent function to add an entry to the log, and the DeregisterEventSource function to close the handle returned by RegisterEventSource.

It makes sense to call RegisterEventSource during the initialization of the application and save the returned descriptor all the time, so that entries in the log can be placed from different places in the program. The simplest example of writing to the Windows log might look like this:

HANDLE hLog = RegisterEventSource (NULL, "MyApplicationName");

if (hLog! = NULL)

{

if (ReportEvent (hLog, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, "Message text / 0", NULL))

{

// event was successfully logged

}

DeregisterEventSource (hLog);

}

More details about the semantics of the ReportEvent function can be found in MSDN at https://msdn.microsoft.com/en-us/library/windows/desktop/aa363679%28v=vs.85%29.aspx. In addition, you need to put some data about the application's executable module in the system registry, and add resources in a specific format to the module itself or a third-party dynamic library. For more information on the registry keys for the event log service, see

Step 2

Logging on Linux-compatible operating systems can usually be done using the syslog daemon. This service has an application-level interface in the form of a set of functions, the declarations of which are placed in the syslog.h header file.

Use the openlog function to create a connection to the syslog service from an application or library. Call the syslog or vsyslog functions to put messages in the log. After the end of recording events or when the application ends, close the connection to the service by calling the closelog function. In addition, you can configure the settings to ignore calls that add event records with a specific priority using the setlogmask function. An example of writing messages to the log might look like this:

openlog ("MyApplication", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);

syslog (LOG_NOTICE, "MyApplication is launched with PID% d", getuid ());

syslog (LOG_INFO, "Information message!");

closelog ();

For more information on the parameters of the syslog API functions, see the libc info documentation.

Step 3

Write logs to arbitrary files using your own implementation of the event persistence subsystem. One of the simplest solutions to this problem is to create several functions in the global scope, one of which opens a file with a specific name in the add information mode, the second closes it, and the third adds a message string passed to it as a parameter to this file. Conceptually, this solution resembles the syslog programming interface in Linux.

Use the fopen and fclose functions of the C standard library to open and close a file, respectively. Call fwrite to add information to the file. You can also use platform-specific functions (for example, CreateFile under Windows) and methods of objects of the frameworks used that encapsulate the functionality of working with files.

Recommended: