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 implement one's own logging system in embedded Development

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

Share

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

This article introduces the relevant knowledge of "how to realize your own log system in embedded development". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Log system is very important in embedded application development.

Especially when there are sporadic bugs in the production process that are related to the current execution environment,

Without a logging system to track problems, it is difficult to reproduce them.

Therefore, it is helpful and necessary to implement your own logging system.

In software models, log systems are generally compiled into library files.

The application program directly calls the API interface function provided in the library to record the log information.

So implementing your own logging system requires three issues to be addressed:

(1) Design of log API functions.

(2) Cache of log information.

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

This article focuses on the first problem: the design of logging API functions.

First the code:

test

knowledge points

1. String literal concatenation

In C, there are many ways to concatenate strings: memcpy,strcpy,strcat,sprintf, etc.

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

To add:

There are places in the logging system code where you need to format strings.

Sprintf is the most convenient, but also the least efficient!

You can also use third-party libraries for string formatting, such as fmtlib, folly format for Facebook, and Abseil StrFormat for Google.

Of course, the best way is to format certain types of data yourself, which can significantly improve the throughput of the logging system. We will talk about this part of the code in the next article.

2. variable parameters

As we all know, printf function is implemented through 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 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"); where no arguments are passed after format, then you will compile an error because after the macro substitution it becomes printf("hello",), and there is an extra comma after the first argument, so you will report an error.

If you call debug3("hello, world! "); There is no problem in this way, because debug3 has"##"in front of the variable parameter__VA_ARGS__. When the compiler finds that there is no parameter passed, it will automatically remove the comma after format, so the compilation is OK.

3.#and ##in macro definition

#is used to "string" macro parameters during preprocessing, such as:

##is used to "glue" two macro parameters during preprocessing, for example:

"Embedded development how to achieve their own log system" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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