Skip to content

Commit a0b481a

Browse files
author
whwu
committed
add gitignore for vscode.
add cmake build. add main function in ulog_example.c.
1 parent 22de23c commit a0b481a

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ modules.order
5050
Module.symvers
5151
Mkfile.old
5252
dkms.conf
53+
build/
54+
.vscode

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(ulog)
4+
5+
add_subdirectory(tests)
6+
add_subdirectory(src)

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Set the project name
2+
project(ulog)
3+
4+
# Add a library with the above sources
5+
add_library(${PROJECT_NAME} ulog.c)

tests/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
project(ulog_example)
2+
3+
include_directories(${PROJECT_SOURCE_DIR}/../src)
4+
5+
add_executable(ulog_example ulog_example.c)
6+
7+
target_link_libraries(ulog_example ulog)

tests/ulog_example.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <stdio.h>
2+
#include "ulog.h"
3+
4+
// To use uLog, you must define a function to process logging messages. It can
5+
// write the messages to a console, to a file, to an in-memory buffer: the
6+
// choice is yours. And you get to choose the format of the message.
7+
//
8+
// The following example prints to the console.
9+
//
10+
// One caveat: msg is a static string and will be over-written at the next call
11+
// to ULOG. This means you may print it or copy it, but saving a pointer to it
12+
// will lead to confusion and astonishment.
13+
//
14+
void my_console_logger(ulog_level_t severity, char *msg)
15+
{
16+
printf("console: %s [%s]: %s\n",
17+
"time", // user defined function
18+
ulog_level_name(severity),
19+
msg);
20+
}
21+
22+
void my_file_logger(ulog_level_t severity, char *msg)
23+
{
24+
printf("file: %s [%s]: %s\n",
25+
"time", // user defined function
26+
ulog_level_name(severity),
27+
msg);
28+
}
29+
30+
int main()
31+
{
32+
int arg = 42;
33+
34+
ULOG_INIT();
35+
36+
// log messages with a severity of WARNING or higher to the console. The
37+
// user must supply a method for my_console_logger, e.g. along the lines
38+
// of what is shown above.
39+
ULOG_SUBSCRIBE(my_console_logger, ULOG_DEBUG_LEVEL);
40+
41+
// log messages with a severity of DEBUG or higher to a file. The user must
42+
// provide a method for my_file_logger (not shown here).
43+
ULOG_SUBSCRIBE(my_file_logger, ULOG_WARNING_LEVEL);
44+
45+
ULOG_INFO("Info, arg=%d", arg); // logs to file but not console
46+
ULOG_CRITICAL("Critical, arg=%d", arg); // logs to file and console
47+
48+
// dynamically change the threshold for a specific logger
49+
ULOG_SUBSCRIBE(my_console_logger, ULOG_INFO_LEVEL);
50+
51+
ULOG_INFO("Info, arg=%d", arg); // logs to file and console
52+
53+
// remove a logger
54+
ULOG_UNSUBSCRIBE(my_file_logger);
55+
56+
ULOG_INFO("Info, arg=%d", arg); // logs to console only
57+
}

0 commit comments

Comments
 (0)