varch/test/test_vlog.c
2024-11-16 13:36:54 +08:00

148 lines
3.4 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if defined(TEST_TARGET_vlog)
#include <varch/command.h>
#include <varch/vlog.h>
#else
#include "init.h"
#include "command.h"
#include "vlog.h"
#endif
#define DEFAULT_OFFLINEFILE "./test/file/log.txt"
static void vlog_callback(char *buf, int len)
{
printf("vlog_callback[%d]: %s", len, buf);
}
static void test_base(void)
{
vlog(VLOG_CHANNEL_0, "[VLOG_CHANNEL_0] vlog!\r\n");
vlog(VLOG_CHANNEL_1, "[VLOG_CHANNEL_1] vlog!\r\n");
vlog(VLOG_CHANNEL_2, "[VLOG_CHANNEL_2] vlog!\r\n");
}
static void test_channel(void)
{
VLOG_DISABALE(VLOG_CHANNEL_0);
VLOG_ENABALE(VLOG_CHANNEL_1);
vlog(VLOG_CHANNEL_0, "[VLOG_CHANNEL_0] vlog!\r\n");
vlog(VLOG_CHANNEL_1, "[VLOG_CHANNEL_1] vlog!\r\n");
vlog(VLOG_CHANNEL_2, "[VLOG_CHANNEL_2] vlog!\r\n");
}
static void test_offline(void)
{
vlog_set_offline(VLOG_CHANNEL_0, DEFAULT_OFFLINEFILE);
for (int i = 0; i < 10; i++)
{
vlog(VLOG_CHANNEL_0, "vlog %d!\r\n", i);
}
vlog_set_offline(VLOG_CHANNEL_0, NULL);
}
static void usage(void)
{
printf(
"Usage: vlog [opt] [arg]\n"
"\n"
"options:\n"
" -c [<channel>] Select the output channel and select by bit, channel 0 by default\n"
" 1: channel 0\n"
" 2: channel 1\n"
" 4: channel 2\n"
" 8: channel 3\n"
" ...\n"
" -f [<offline file>] Select the offline out file name, \"./test/file/log.txt\" by default\n"
" -h Print help\n"
" -v Print version\n"
"\n"
"argument:\n"
" <base> Test base function\n"
" <channel> Test channel function\n"
" <offline> Test offline function\n"
);
}
static int test(int argc, char *argv[])
{
char *logfile = DEFAULT_OFFLINEFILE;
vlogChnType chn = VLOG_CHANNEL_0;
/* reset getopt */
command_opt_init();
while (1)
{
int opt = command_getopt(argc, argv, "c::f::hv");
if (opt == -1) break;
switch (opt)
{
case 'c' :
chn = atoi(command_optarg);
break;
case 'f' :
if (command_optarg)
{
logfile = command_optarg;
}
else
{
logfile = DEFAULT_OFFLINEFILE;
}
break;
case 'v' :
printf("vlog version %d.%d.%d\r\n", VLOG_V_MAJOR, VLOG_V_MINOR, VLOG_V_PATCH);
return 0;
case '?':
printf("Unknown option `%c`\r\n", command_optopt);
return -1;
case 'h' :
default:
usage();
return 0;
}
}
if (logfile) vlog_set_offline(chn, logfile);
vlog(chn, "Hello vlog!\r\n");
if (logfile) vlog_set_offline(chn, NULL);
// vlog_set_func(vlog_callback);
for (int index = command_optind; index < argc; index++)
{
if (!strcmp(argv[index], "base"))
{
test_base();
}
else if (!strcmp(argv[index], "channel"))
{
test_channel();
}
else if (!strcmp(argv[index], "offline"))
{
test_offline();
}
}
return 0;
}
#if defined(TEST_TARGET_vlog)
int main(int argc, char *argv[])
{
return test(argc, argv);
}
#else
void test_vlog(void)
{
command_export("vlog", test);
}
init_export_app(test_vlog);
#endif