The dprintf implementation in LK

Source: Internet
Author: User
In LK we usually print the log through dprintf, as shown below
dprintf (INFO, "0x%x", T->tls[i]);
where info represents the level of log, LK has three levels in total
/* Debug Levels */
#define CRITICAL 0
#define ALWAYS 0
#define INFO 1
#define SPEW 2


Continue to see the realization of dprintf
#define DPRINTF (level, x ...) do {if (level) <= debuglevel) {_dprintf (x);}} while (0)
found that if you want to print a level smaller than debuglevel,log it will be printed
#if defined (DEBUG)
#define DebugLevel DEBUG
#else
#define DEBUGLEVEL 2
#endif
You can see that if debug is not defined, DebugLevel defaults to 2, which means that all logs can be printed.
The implementation of _DPRINTF is as follows:
int _dprintf (const char *FMT, ...)
{
Char buf[256];
Char ts_buf[13];
int err;


snprintf (ts_buf, sizeof (TS_BUF), "[%u]", current_time ());
Dputs (always, ts_buf);


Va_list ap;
Va_start (AP, FMT);
Call vsnprintf to format the string, such as the 0x%x in this example to indicate that the log is printed in 16 binary ways
Err = vsnprintf (buf, sizeof (BUF), FMT, AP);
Va_end (AP);


Dputs (always, buf);


return err;
}
Log is specifically called Dputs to print out, we continue to see its implementation
#define DPUTS (level, str) does {if (level) <= debuglevel) {_dputs (str);}} and (0)




To the above, but also to determine the log level, continue to call _dputs to print log,_dputs is the implementation of the specific platform, we look at an example that has been implemented.
int _dputs (const char *STR)
{
while (*str! = 0) {
_DPUTC (*str++);
}


return 0;
}
The characters in a string are printed through _DPUTC by a while loop.
void _dputc (char c)
{
*reg8 (debug_stdout) = C;
}
#define REG8 (addr) ((volatile uint8_t *) (addr))
As you can see, the HW will automatically print the characters as soon as the string is written to the UART register.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.