A lightweight library written for embedded systems.
Original author is Shawn Feng. I removed a bunch of stuff I didn't need and keep the C code.
- Different log levels
- Log color identification
- Print token (any basic type is automatically recognized and printed)
- Hex dump (print the hexadecimal content in the specified address)
- Statistics code running time
#include "ulog/ulog.h"
#include <stdio.h>
#include <time.h>
static int put_str(void *user_data, const char *str) {
return printf("%s", str);
}
int main() {
// Initial logger
logger_set_output_callback(ULOG_GLOBAL, put_str);
double pi = 3.14159265;
// Different log levels
LOGGER_TRACE("PI = %.3f", pi);
LOGGER_DEBUG("PI = %.3f", pi);
LOGGER_INFO("PI = %.3f", pi);
LOGGER_WARN("PI = %.3f", pi);
LOGGER_ERROR("PI = %.3f", pi);
LOGGER_FATAL("PI = %.3f", pi);
LOGGER_RAW("PI = %.3f\r\n", pi);
// Output debugging expression
LOGGER_TOKEN(pi);
LOGGER_TOKEN(50 * pi / 180);
LOGGER_TOKEN(&pi); // print address of pi
char *text = (char *)"Ulog is a micro log library.";
LOGGER_TOKEN((char *)text);
// Hex dump
LOGGER_HEX_DUMP(text, 45, 16);
// Output multiple tokens to one line
time_t now = 1577259816;
struct tm lt = *localtime(&now);
LOGGER_MULTI_TOKEN(lt.tm_year + 1900, lt.tm_mon + 1, lt.tm_mday);
LOGGER_MULTI_TOKEN(lt.tm_wday, lt.tm_hour, lt.tm_min, lt.tm_sec);
// Output execution time of some statements
LOGGER_TIME_CODE(
uint32_t n = 1000 * 1000; while (n--);
);
return 0;
}
$ ./examples/readme
2025-04-27 14:31:05.019 99463-259 T (readme.c:16 main) PI = 3.142
2025-04-27 14:31:05.020 99463-259 D (readme.c:17 main) PI = 3.142
2025-04-27 14:31:05.020 99463-259 I (readme.c:18 main) PI = 3.142
2025-04-27 14:31:05.020 99463-259 W (readme.c:19 main) PI = 3.142
2025-04-27 14:31:05.020 99463-259 E (readme.c:20 main) PI = 3.142
2025-04-27 14:31:05.020 99463-259 F (readme.c:21 main) PI = 3.142
PI = 3.142
2025-04-27 14:31:05.020 99463-259 D (readme.c:25 main) pi => 3.141593
2025-04-27 14:31:05.020 99463-259 D (readme.c:26 main) 50 * pi / 180 => 0.872665
2025-04-27 14:31:05.020 99463-259 D (readme.c:27 main) &pi => 0x16f4a1b28
2025-04-27 14:31:05.020 99463-259 D (readme.c:30 main) (char *)text => "Ulog is a micro log library."
2025-04-27 14:31:05.020 99463-259 D (readme.c:33 main) hex_dump(data:text, length:45, width:16) =>
10095fcaa 55 6c 6f 67 20 69 73 20 61 20 6d 69 63 72 6f 20 |Ulog is a micro |
10095fcba 6c 6f 67 20 6c 69 62 72 61 72 79 2e 00 1b 5b 33 |log library...[3|
10095fcca 34 6d 25 73 20 1b 5b 33 31 6d 3d 3e 20 |4m%s .[31m=> |
10095fcd7
2025-04-27 14:31:05.020 99463-259 D (readme.c:39 main) lt.tm_year + 1900 => 2019, lt.tm_mon + 1 => 12, lt.tm_mday => 25
2025-04-27 14:31:05.020 99463-259 D (readme.c:40 main) lt.tm_wday => 3, lt.tm_hour => 2, lt.tm_min => 43, lt.tm_sec => 36
2025-04-27 14:31:05.022 99463-259 D (readme.c:47 main) time { uint32_t n = 1000 * 1000; while (n--); } => 2458us
Copyright (c) 2021 Shawn Feng
Copyright (c) 2025 Kevin Walchko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.