Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C language variable parameters and macro definition to implement your own log system

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use C language variable parameters and macro definitions to implement your log system. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

In the process of embedded application development, the log system is very important!

Especially when there is an occasional bug related to the current execution environment in the production process.

If there is no log system to track the problem, it is difficult to reproduce the problem.

Therefore, it is very helpful and necessary to implement a log system of your own.

In the software model, the log system is generally compiled into library files.

The application program can record log information by directly calling the API interface function provided in the library.

Then there are three issues to deal with in order to implement your own logging system:

(1) the design of log API function.

(2) the cache of log information.

(3) persistence of log information, that is, writing to the local file system.

This paper mainly explains the first problem: the design of log API function.

Code first:

test

Knowledge point

1. Concatenation of the literal quantity of a string

In C language, there are many ways to concatenate strings: memcpy,strcpy,strcat,sprintf and so on.

In line 19 of code, the literal concatenation of strings in C language is used to concatenate the three strings of "% slug% d (% s)\"format"\ "\ n" into one string.

Let me add:

In the log system code, there are places where you need to format strings.

Using sprintf is the most convenient, but also the least efficient!

You can also use some third-party libraries for string formatting, such as fmtlib,facebook 's folly format,google 's Abseil StrFormat.

Of course, the best way is to format specific types of data yourself, which can significantly improve the throughput of the logging system, which will be discussed in the next article.

two。 Variable parameter

As we all know, the printf function is implemented through the variable parameter mechanism.

Variable parameters can be defined and used as follows:

(1) without parameter name

(2) with parameter name

Line 20 uses _ _ VA_ARGS__ to represent the three points (...) in the macro definition parameters, that is, variable parameters.

Let's talk about "#".

If you call: debug2 ("code =% d", 100); there is no problem with this call.

If you call: debug2 ("hello"); when called here, no arguments are passed after format, then a compilation error occurs because it becomes printf ("hello",) after the macro replacement, and the first parameter is followed by a comma, so an error is reported.

If you call: debug3 ("hello, world!"); there is no problem, because there is a "# #" before the variable parameter _ _ VA_ARGS__ in debug3, and when the compiler finds that no argument is passed in, it automatically removes the comma after format, so OK is compiled.

3. # and # in Macro definition

The function of # is to "serialize" macro parameters during preprocessing, for example:

The function of # # is to "glue" two macro parameters during preprocessing, for example:

On how to use C language variable parameters and macro definition to achieve their own log system is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report