LingYun Studio embeded system framwork software, such as thirdparty build shell and lingyun library
guowenxue
2024-08-21 10bed2f901130a08badc17f2de356b41afb931ab
Add buffer dump print code
1 files modified
1 files added
79 ■■■■■ changed files
booster/testcase/dump.c 75 ●●●●● patch | view | raw | blame | history
booster/testcase/logger.c 4 ●●●● patch | view | raw | blame | history
booster/testcase/dump.c
New file
@@ -0,0 +1,75 @@
/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  dump.c
 *    Description:  This is the buffer dump print code.
 *
 *        Version:  1.0.0(08/08/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "08/08/2012 06:51:40 PM"
 *
 ********************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void dump_buf(const char *prompt, const unsigned char *buf, size_t len)
{
    char        line[256];
    size_t      i, j;
    int         offset;
    if( prompt )
    {
        printf("%s", prompt);
    }
    for(i = 0; i < len; i += 16)
    {
        offset = snprintf(line, sizeof(line), "%08zx: ", i);
        /* Print hex representation */
        for (j = 0; j < 16; j++)
        {
            if (i + j < len)
                offset += snprintf(line + offset, sizeof(line) - offset, "%02x ", buf[i + j]);
            else
                offset += snprintf(line + offset, sizeof(line) - offset, "   ");
        }
        offset += snprintf(line + offset, sizeof(line) - offset, " ");
        /* Print ASCII representation */
        for (j = 0; j < 16; j++)
        {
            if (i + j < len)
            {
                unsigned char c = buf[i + j];
                offset += snprintf(line + offset, sizeof(line) - offset, "%c", (c >= 32 && c <= 126) ? c : '.');
            }
            else
            {
                offset += snprintf(line + offset, sizeof(line) - offset, " ");
            }
        }
        /* Print the line */
        printf("%s\n", line);
    }
}
int main (int argc, char **argv)
{
    char    buf[256];
    int     i;
    for(i=0; i<sizeof(buf); i++)
        buf[i] = i;
    dump_buf("Hex dump buffer content:\n", buf, sizeof(buf));
    return 0;
}
booster/testcase/logger.c
@@ -2,8 +2,8 @@
 *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  test_logger.c
 *    Description:  This is the linux logger system test code.
 *       Filename:  logger.c
 *    Description:  This is the logger system test code.
 *
 *        Version:  1.0.0(08/08/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>