RaspberrPi project source code
69b42a43ca4b2d93be203c34f6b45f5de1e32a15..cfdcbd734b4ede4933c87cbe4c44f8aa855b910d
2024-05-27 guowenxue
add test example code in booster
cfdcbd diff | tree
2024-05-27 guowenxue
update dump_buf in logger.c
2c971f diff | tree
2 files modified
2 files added
194 ■■■■ changed files
project/booster/logger.c 83 ●●●●● patch | view | raw | blame | history
project/booster/makefile 2 ●●●●● patch | view | raw | blame | history
project/booster/test/makefile 64 ●●●●● patch | view | raw | blame | history
project/booster/test/test_logger.c 45 ●●●●● patch | view | raw | blame | history
project/booster/logger.c
@@ -201,79 +201,50 @@
        L.lockfn(L.udata, 0);
}
#define LINELEN 81
#define CHARS_PER_LINE 16
static char *print_char =
"                "
"                "
" !\"#$%&'()*+,-./"
"0123456789:;<=>?"
"@ABCDEFGHIJKLMNO"
"PQRSTUVWXYZ[\\]^_"
"`abcdefghijklmno"
"pqrstuvwxyz{|}~ "
"                "
"                "
" ???????????????"
"????????????????"
"????????????????"
"????????????????"
"????????????????"
"????????????????";
void log_dump(int level, const char *prompt, char *buf, size_t len)
{
    int rc;
    int idx;
    char prn[LINELEN];
    char lit[CHARS_PER_LINE + 2];
    char hc[4];
    short line_done = 1;
    int                 i, j, ofset;
    char                line[256];
    unsigned char       c;
    unsigned char      *buffer = (unsigned char *)buf;
    if (!L.fp || level>L.level)
        return;
    if( prompt )
        _log_write(level, __FILE__, __LINE__, "%s", prompt);
        _log_write(level, __FILE__, __LINE__, "%s\r\n", prompt);
    rc = len;
    idx = 0;
    lit[CHARS_PER_LINE] = '\0';
    while (rc > 0)
    for(i=0; i<len; i+=16)
    {
        if (line_done)
            snprintf(prn, LINELEN, "%08X: ", idx);
        ofset = snprintf(line, sizeof(line), "%04x: ", i);
        do
        /* print hex representation, and print spaces if end of buffer */
        for(j=0; j<16; j++)
        {
            unsigned char c = buf[idx];
            snprintf(hc, 4, "%02X ", c);
            strncat(prn, hc, LINELEN);
            lit[idx % CHARS_PER_LINE] = print_char[c];
            if(i+j < len)
                ofset += snprintf(line+ofset, sizeof(line)-ofset, "%02x ", buffer[i+j]);
            else
                ofset += snprintf(line+ofset, sizeof(line)-ofset, "   ");
        }
        while (--rc > 0 && (++idx % CHARS_PER_LINE != 0));
        ofset += snprintf(line+ofset, sizeof(line)-ofset, " ");
        line_done = (idx % CHARS_PER_LINE) == 0;
        if (line_done)
        /* print ASCII representation */
        for(j=0; j<16; j++)
        {
            if (L.fp)
                fprintf(L.fp, "%s  %s\n", prn, lit);
            if (i+j < len)
            {
                c = buffer[i+j];
                ofset += snprintf(line+ofset, sizeof(line)-ofset, "%c", (c>=32 && c<=126) ? c : '.');
            }
            else
            {
                ofset += snprintf(line+ofset, sizeof(line)-ofset, " ");
            }
        }
    }
    if (!line_done)
    {
        int ldx = idx % CHARS_PER_LINE;
        lit[ldx++] = print_char[(int)buf[idx]];
        lit[ldx] = '\0';
        while ((++idx % CHARS_PER_LINE) != 0)
            strncat(prn, "   ", sizeof(prn)-strlen(prn));
        if (L.fp)
            fprintf(L.fp, "%s  %s\n", prn, lit);
            fprintf(L.fp, "%s\r\n", line);
    }
}
project/booster/makefile
@@ -18,6 +18,8 @@
    CROSS_COMPILE?=arm-linux-gnueabihf-
endif
#CROSS_COMPILE=
LIBNAME=$(shell basename ${PWD} )
TOPDIR=$(shell dirname ${PWD} )
CFLAGS+=-D_GNU_SOURCE
project/booster/test/makefile
New file
@@ -0,0 +1,64 @@
#*********************************************************************************
#      Copyright:  (C) 2022 Guo Wenxue
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This Makefile used to compile all the C source code file in current
#                  folder to respective excutable binary files.
#
#        Version:  1.0.0(03/15/2022~)
#                  Author:  Guo Wenxue <guowenxue@gmail.com>
#      ChangeLog:  1, Release initial version on "03/15/2022 01:29:33 PM"
#
#********************************************************************************/
PWD=$(shell pwd)
LIB_PATH=$(shell dirname ${PWD})
LIB_NAME=$(shell basename ${LIB_PATH})
INSTPATH=/tftp
#ARCH ?= i386
#ARCH?=arm926t
ARCH?=arm920t
#LINK_MODE=STATIC
MODE=PRODUCTION
DEBUG=1
INSTPATH=/tftp
#CROSS_COMPILE=aarch64-linux-gnu-
export CC=${CROSS_COMPILE}gcc
export CXX=${CROSS_COMPILE}g++
export AR=${CROSS_COMPILE}ar
export AS=${CROSS_COMPILE}as
export RANLIB=${CROSS_COMPILE}ranlib
export STRIP=${CROSS_COMPILE}strip
SRCS = $(wildcard ${VPATH}/*.c)
OBJS = $(patsubst %.c,%.o,$(SRCS))
SRCFILES = $(wildcard *.c)
BINARIES=$(SRCFILES:%.c=%)
CFLAGS+=-I${LIB_PATH}
LDFLAGS+=-L${LIB_PATH} -l${LIB_NAME}
all: binaries install
binaries:  ${BINARIES}
%:  %.c
    $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
install:
    cp $(BINARIES) ${INSTPATH}
clean:
    @rm -f *.o *.lo $(BINARIES)
distclean: clean
    @rm -f  tags cscope*
.PHONY: clean entry
project/booster/test/test_logger.c
New file
@@ -0,0 +1,45 @@
/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  test_logger.c
 *    Description:  This is the linux logger system test 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>
#include <libgen.h>
#include "logger.h"
int main (int argc, char **argv)
{
    char    buf[256];
    int     i;
    for(i=0; i<sizeof(buf); i++)
        buf[i] = i;
#if 0
    log_open("console", LOG_LEVEL_DEBUG, 0, LOG_LOCK_DISABLE);
#else
    log_open("test.log", LOG_LEVEL_DEBUG, 10, LOG_LOCK_DISABLE);
#endif
    log_error("This is a errorr message\n");
    log_warn("This is a warnning message\n");
    log_info("This is a informat message\n");
    log_debug("This is a debug message\n");
    log_trace("This is a trace message\n");
    log_dump(LOG_LEVEL_DEBUG, "Hex dump buffer content:", buf, sizeof(buf));
    log_close();
    return 0;
}