04. Transplantation of W601 EasyLogger
1. Introduction
EasyLogger It is an ultra lightweight (ROM < 1.6K, RAM < 0.3k) and high-performance C/C + + log library, which is very suitable for resource sensitive software projects, such as IoT products, wearable devices, smart homes, etc. Compared with the well-known C/C + + log libraries such as log4c and zlog, EasyLogger has simpler functions and fewer interfaces for users, but it can be used quickly. More practical functions support dynamic expansion in the form of plug-ins.
2. Why transplant
- Support user-defined output mode (e.g. terminal, file, database, serial port, 485, Flash...);
- The log content can include level, timestamp, thread information, process information, etc;
- Log output is designed to be thread safe, and supports asynchronous output and buffered output modes;
- Support multiple operating systems( RT-Thread , UCOS, Linux, Windows...), also supports bare metal platforms;
- The log supports RAW format and hexdump;
- Support dynamic filtering by tag, level and keyword;
- Logs at all levels can be displayed in different colors;
- It has strong expansibility and supports the expansion of new functions in the form of plug-ins.
Explanation of terms:
- 1. RAW format: unformatted original log.
- 2. Label: in the software, labels can be set for logs to be printed according to files, modules, functions, etc., so as to realize log classification.
In a word, it is easy to use, because in future work, we will inevitably encounter various problems. At this time, having an easy-to-use and eye-catching Log will greatly improve our development efficiency.
What are the specific features of easylogger? You can go to github to find the source code.
3. Transplanting EasyLogger based on MDK
First download the source code from github, and then take a look at its README file. This is helpful for our migration, and then take a look at docs \ zh \ port \ kernel MD file. Here's how to transplant it. I'll transplant it directly below.
- Directly copy the inc, port and src folders under easylogger to your project folder.
- No matter if you compile it first, you will encounter the following errors without accident
This is due to eLog_ There are many configurations in the cfg file, and we are in the Windows environment, so we directly modify this configuration file first. After opening the file, you can clearly see the dividing line, indicating that there are module layers. Let's take a look at the first one. It's easy to see that this is the basic configuration of log log. Then we will directly annotate all the following two boundaries and compile them again.
The compilation result passed successfully, except for several warning s:
Three of them say that the function has no return value, and the other is that the last line needs an empty line. After solving it, there will be no warning.
Write a log initialization function and test it:
void log_init(void) { elog_init();//initialization /* Set the format of each level of log */ elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL); elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~(ELOG_FMT_FUNC | ELOG_FMT_T_INFO | ELOG_FMT_P_INFO)); elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~(ELOG_FMT_FUNC | ELOG_FMT_T_INFO | ELOG_FMT_P_INFO)); elog_start();//start-up } void test_elog(void) {//Test function /* test log output for all level */ log_a("Hello EasyLogger!"); log_e("Hello EasyLogger!"); log_w("Hello EasyLogger!"); log_i("Hello EasyLogger!"); log_d("Hello EasyLogger!"); log_v("Hello EasyLogger!"); }
Download the program to W601, no output, ahaha
I've been looking for it for a long time. The reason is that the following function is empty. It's just to add a sentence to the above file reporting the reason for warning and print it
void elog_port_output(const char *log, size_t size) { /* output to terminal */ printf("%.*s", size, log); //Add this sentence to print!!! /* add your code here */ }
So far, EasyLogger has been ported. For other functions, such as setting the color of log level, you can use eLog_ cfg. The configuration in H is the previously commented code, which can be tested bit by bit.
So far, EasyLogger has been ported. For other functions, such as setting the color of log level, you can use eLog_ cfg. The configuration in H is the previously commented code, which can be tested bit by bit.