working Communication, Database, Logger (w/out CDC)
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
set(KPI_Rover_Logger_Inc
|
||||
#${PROJECT_KPIROVER_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
set(KPI_Rover_Logger_Src
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ulUlog.c
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ulog.c
|
||||
)
|
||||
|
||||
add_library(KPI_Rover_Logger STATIC)
|
||||
|
||||
target_include_directories(KPI_Rover_Logger PRIVATE
|
||||
${KPI_Rover_Logger_Inc}
|
||||
)
|
||||
|
||||
target_sources(KPI_Rover_Logger PRIVATE
|
||||
${KPI_Rover_Logger_Src}
|
||||
)
|
||||
|
||||
target_link_libraries(KPI_Rover_Logger PRIVATE
|
||||
FREERTOS_H
|
||||
HAL_H
|
||||
)
|
||||
|
||||
add_library(KPI_Rover_Logger_H INTERFACE)
|
||||
target_include_directories(KPI_Rover_Logger_H INTERFACE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
@@ -0,0 +1,50 @@
|
||||
# uLog Logger
|
||||
|
||||
A lightweight logging system for the KPI Rover ECU software that provides structured logging with multiple log levels and configurable output destinations. Integrated with FreeRTOS for thread-safe operation across multiple tasks.
|
||||
|
||||
## Usage Example
|
||||
|
||||
Once initialized, you can use logging macros throughout your application:
|
||||
|
||||
```c
|
||||
#include "ulog.h"
|
||||
|
||||
void StartDefaultTask(void *argument)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
|
||||
ULOG_INFO("Default task started");
|
||||
|
||||
for(;;)
|
||||
{
|
||||
ULOG_DEBUG("Hello from default task %d", count++);
|
||||
vTaskDelay(pdMS_TO_TICKS(101)); // Wait for ~100ms
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Available Log Levels
|
||||
|
||||
The logger supports multiple log levels in order of increasing severity:
|
||||
|
||||
| Level | Macro | Numeric Value | Description |
|
||||
|-------|-------|---------------|-------------|
|
||||
| **TRACE** | `ULOG_TRACE()` | 100 | Very detailed information for tracing program flow |
|
||||
| **DEBUG** | `ULOG_DEBUG()` | 101 | Detailed information for diagnosing problems |
|
||||
| **INFO** | `ULOG_INFO()` | 102 | General information about program execution |
|
||||
| **WARNING** | `ULOG_WARNING()` | 103 | Something unexpected happened, but software still works |
|
||||
| **ERROR** | `ULOG_ERROR()` | 104 | Serious problem occurred, some function failed |
|
||||
| **CRITICAL** | `ULOG_CRITICAL()` | 105 | Very serious error, program may be unable to continue |
|
||||
| **ALWAYS** | `ULOG_ALWAYS()` | 106 | Messages that should always be logged |
|
||||
|
||||
### Example Output
|
||||
|
||||
When properly configured, the logger produces formatted output like this:
|
||||
|
||||
```
|
||||
[20301][DEBUG][main.c] Hello from default task 201
|
||||
[21302][INFO][sensor_task.c] Temperature: 23.5°C
|
||||
[22303][WARNING][sensor_task.c] High temperature detected: 81.2°C
|
||||
[23304][ERROR][can_driver.c] CAN transmission failed, error code: 0x42
|
||||
```
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2024 KPI Rover Project
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "ulUlog.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "queue.h"
|
||||
#include "cmsis_os2.h"
|
||||
//#include "usbd_cdc_if.h"
|
||||
//#include "usbd_def.h"
|
||||
#include "main.h"
|
||||
#include "portable.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LOG_QUEUE_LENGTH 20
|
||||
#define MAX_LOG_MESSAGE_SIZE 256
|
||||
|
||||
static void ulogTask(void *argument);
|
||||
|
||||
static osThreadId_t ulogTaskHandle;
|
||||
static StaticQueue_t xUlogQueueBuffer;
|
||||
static uint8_t ucUlogQueueStorage[LOG_QUEUE_LENGTH * MAX_LOG_MESSAGE_SIZE];
|
||||
static QueueHandle_t xULogQueue = NULL;
|
||||
|
||||
volatile uint32_t ulogUsbIsEstablished;
|
||||
osEventFlagsId_t ulogFlags;
|
||||
#define ULOG_CDC_TRANSMIT_FINISH_FLAG 0x1
|
||||
#define ULOG_DEV_CONNECTED_WAKEUP_FLAG 0x2
|
||||
|
||||
static StaticSemaphore_t ulUlog_mutex_storage;
|
||||
#define ULOG_LOCK() // osMutexAcquire(&ulUlog_mutex_storage, osWaitForever)
|
||||
#define ULOG_UNLOCK() // osMutexRelease(&ulUlog_mutex_storage)
|
||||
|
||||
static const osThreadAttr_t ulogTask_attributes = {
|
||||
.name = "ulogTask",
|
||||
.stack_size = 128 * 4,
|
||||
.priority = (osPriority_t) osPriorityNormal,
|
||||
};
|
||||
|
||||
|
||||
void ul_ulog_init()
|
||||
{
|
||||
xULogQueue = xQueueCreateStatic(LOG_QUEUE_LENGTH, MAX_LOG_MESSAGE_SIZE,
|
||||
ucUlogQueueStorage, &xUlogQueueBuffer);
|
||||
if (xULogQueue == NULL)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
ulogTaskHandle = osThreadNew(ulogTask, NULL, &ulogTask_attributes);
|
||||
if (ulogTaskHandle == NULL)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
ulogFlags = osEventFlagsNew(NULL);
|
||||
ulogUsbIsEstablished = 0;
|
||||
|
||||
{
|
||||
const static osMutexAttr_t mutex_attrs = {NULL, 0, &ulUlog_mutex_storage, sizeof(ulUlog_mutex_storage)};
|
||||
(void) osMutexNew(&mutex_attrs);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void ul_ulog_send_critical(ulog_level_t level, const char *filename, char *msg)
|
||||
{
|
||||
if (xULogQueue == NULL || msg == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
static char logBuffer[MAX_LOG_MESSAGE_SIZE];
|
||||
int len;
|
||||
size_t msg_len = strlen(msg);
|
||||
|
||||
// Extract just the filename from the full path
|
||||
const char *fbasename = strrchr(filename, '/');
|
||||
if (fbasename) {
|
||||
fbasename++; // Skip the '/'
|
||||
} else {
|
||||
fbasename = filename; // No path separator found
|
||||
}
|
||||
|
||||
// Truncate message if too long, leaving space for timestamp, level, and filename
|
||||
if (msg_len > (MAX_LOG_MESSAGE_SIZE - 60))
|
||||
{
|
||||
msg_len = MAX_LOG_MESSAGE_SIZE - 60;
|
||||
}
|
||||
|
||||
TickType_t currentTime = xTaskGetTickCount();
|
||||
len = snprintf(logBuffer, sizeof(logBuffer), "[%lu][%s][%s] %.*s\r\n",
|
||||
(unsigned long)currentTime,
|
||||
ulog_level_name(level),
|
||||
fbasename,
|
||||
(int)msg_len, msg);
|
||||
|
||||
// Ensure buffer is always null-terminated
|
||||
if (len < 0)
|
||||
{
|
||||
// snprintf failed
|
||||
return;
|
||||
}
|
||||
if (len >= sizeof(logBuffer))
|
||||
{
|
||||
logBuffer[sizeof(logBuffer) - 1] = '\0';
|
||||
}
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
if (uxQueueSpacesAvailable(xULogQueue))
|
||||
{
|
||||
if (xPortIsInsideInterrupt())
|
||||
{
|
||||
xQueueSendFromISR(xULogQueue, logBuffer, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
else
|
||||
{
|
||||
xQueueSend(xULogQueue, logBuffer, pdMS_TO_TICKS(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ul_ulog_send(ulog_level_t level, const char *filename, char *msg)
|
||||
{
|
||||
ULOG_LOCK();
|
||||
ul_ulog_send_critical(level, filename, msg);
|
||||
ULOG_UNLOCK();
|
||||
}
|
||||
|
||||
static void ulogTask(void *argument)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
static char logMessage[MAX_LOG_MESSAGE_SIZE];
|
||||
BaseType_t result = xQueueReceive(xULogQueue, logMessage, portMAX_DELAY);
|
||||
|
||||
if (result == pdTRUE)
|
||||
{
|
||||
logMessage[MAX_LOG_MESSAGE_SIZE - 1] = '\0';
|
||||
|
||||
#if 0
|
||||
while (ulogUsbIsEstablished != 1)
|
||||
{
|
||||
osEventFlagsWait(ulogFlags, ULOG_DEV_CONNECTED_WAKEUP_FLAG, 0, osWaitForever);
|
||||
osDelay(2000);
|
||||
}
|
||||
|
||||
CDC_Transmit_FS((uint8_t *)logMessage, strlen(logMessage));
|
||||
osEventFlagsWait(ulogFlags, ULOG_CDC_TRANSMIT_FINISH_FLAG, 0, 100);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2024 KPI Rover Project
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef ULOG_TASK_H_
|
||||
#define UL_ULOG_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ulog.h"
|
||||
|
||||
void ul_ulog_init();
|
||||
void ul_ulog_send(ulog_level_t level, const char *filename, char *msg);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* UL_ULOG_H_ */
|
||||
@@ -0,0 +1,159 @@
|
||||
/**
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 KPI Rover Project
|
||||
|
||||
This file contains modifications and extensions to the original uLog library.
|
||||
Original uLog library:
|
||||
Copyright (c) 2019 R. Dunbar Poor <rdpoor@gmail.com>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file ulog.c
|
||||
*
|
||||
* \brief uLog: lightweight logging for embedded systems
|
||||
*
|
||||
* See ulog.h for sparse documentation.
|
||||
*/
|
||||
|
||||
#include "ulog.h"
|
||||
|
||||
#ifdef ULOG_ENABLED // whole file...
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
// =============================================================================
|
||||
// types and definitions
|
||||
|
||||
typedef struct {
|
||||
ulog_function_t fn;
|
||||
ulog_level_t threshold;
|
||||
} subscriber_t;
|
||||
|
||||
static ulog_level_t ulog_lowest_log_level();
|
||||
|
||||
// =============================================================================
|
||||
// local storage
|
||||
|
||||
static subscriber_t s_subscribers[ULOG_MAX_SUBSCRIBERS];
|
||||
static char s_message[ULOG_MAX_MESSAGE_LENGTH];
|
||||
static ulog_level_t s_lowest_log_level;
|
||||
|
||||
// =============================================================================
|
||||
// user-visible code
|
||||
|
||||
void ulog_init() {
|
||||
memset(s_subscribers, 0, sizeof(s_subscribers));
|
||||
s_lowest_log_level = ulog_lowest_log_level();
|
||||
}
|
||||
|
||||
// search the s_subscribers table to install or update fn
|
||||
ulog_err_t ulog_subscribe(ulog_function_t fn, ulog_level_t threshold) {
|
||||
int available_slot = -1;
|
||||
int i;
|
||||
for (i=0; i<ULOG_MAX_SUBSCRIBERS; i++) {
|
||||
if (s_subscribers[i].fn == fn) {
|
||||
// already subscribed: update threshold and return immediately.
|
||||
s_subscribers[i].threshold = threshold;
|
||||
return ULOG_ERR_NONE;
|
||||
|
||||
} else if (s_subscribers[i].fn == NULL) {
|
||||
// found a free slot
|
||||
available_slot = i;
|
||||
}
|
||||
}
|
||||
// fn is not yet a subscriber. assign if possible.
|
||||
if (available_slot == -1) {
|
||||
return ULOG_ERR_SUBSCRIBERS_EXCEEDED;
|
||||
}
|
||||
s_subscribers[available_slot].fn = fn;
|
||||
s_subscribers[available_slot].threshold = threshold;
|
||||
s_lowest_log_level = ulog_lowest_log_level(); // Update lowest log level
|
||||
|
||||
return ULOG_ERR_NONE;
|
||||
}
|
||||
|
||||
// search the s_subscribers table to remove
|
||||
ulog_err_t ulog_unsubscribe(ulog_function_t fn) {
|
||||
int i;
|
||||
for (i=0; i<ULOG_MAX_SUBSCRIBERS; i++) {
|
||||
if (s_subscribers[i].fn == fn) {
|
||||
s_subscribers[i].fn = NULL; // mark as empty
|
||||
s_lowest_log_level = ulog_lowest_log_level(); // Update lowest log level
|
||||
return ULOG_ERR_NONE;
|
||||
}
|
||||
}
|
||||
return ULOG_ERR_NOT_SUBSCRIBED;
|
||||
}
|
||||
|
||||
const char *ulog_level_name(ulog_level_t severity) {
|
||||
switch(severity) {
|
||||
case ULOG_TRACE_LEVEL: return "TRACE";
|
||||
case ULOG_DEBUG_LEVEL: return "DEBUG";
|
||||
case ULOG_INFO_LEVEL: return "INFO";
|
||||
case ULOG_WARNING_LEVEL: return "WARNING";
|
||||
case ULOG_ERROR_LEVEL: return "ERROR";
|
||||
case ULOG_CRITICAL_LEVEL: return "CRITICAL";
|
||||
case ULOG_ALWAYS_LEVEL: return "ALWAYS";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
void ulog_message(const char *filename, ulog_level_t severity, const char *fmt, ...) {
|
||||
// Do not evaluate the log message if it will never be logged
|
||||
if (severity < s_lowest_log_level){
|
||||
return;
|
||||
}
|
||||
|
||||
va_list ap;
|
||||
int i;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(s_message, ULOG_MAX_MESSAGE_LENGTH, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
for (i=0; i<ULOG_MAX_SUBSCRIBERS; i++) {
|
||||
if (s_subscribers[i].fn != NULL) {
|
||||
if (severity >= s_subscribers[i].threshold) {
|
||||
s_subscribers[i].fn(severity, filename, s_message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// private code
|
||||
|
||||
static ulog_level_t ulog_lowest_log_level(){
|
||||
ulog_level_t lowest_log_level = ULOG_ALWAYS_LEVEL;
|
||||
int i;
|
||||
for (i=0; i<ULOG_MAX_SUBSCRIBERS; i++) {
|
||||
if (s_subscribers[i].fn != NULL){
|
||||
if (s_subscribers[i].threshold < lowest_log_level) {
|
||||
lowest_log_level = s_subscribers[i].threshold;
|
||||
}
|
||||
}
|
||||
}
|
||||
return lowest_log_level;
|
||||
}
|
||||
|
||||
#endif // #ifdef ULOG_ENABLED
|
||||
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 KPI Rover Project
|
||||
|
||||
This file contains modifications and extensions to the original uLog library.
|
||||
Original uLog library:
|
||||
Copyright (c) 2019 R. Dunbar Poor <rdpoor@gmail.com>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
*
|
||||
* \brief uLog: lightweight logging for embedded systems
|
||||
*
|
||||
* A quick intro by example:
|
||||
*
|
||||
* #include "ulog.h"
|
||||
*
|
||||
* // To use uLog, you must define a function to process logging messages.
|
||||
* // It can write the messages to a console, to a file, to an in-memory
|
||||
* // buffer: the choice is yours. And you get to choose the format of
|
||||
* // the message. This example prints to the console. One caveat: msg
|
||||
* // is a static string and will be over-written at the next call to ULOG.
|
||||
* // You may print it or copy it, but saving a pointer to it will lead to
|
||||
* // confusion and astonishment.
|
||||
* //
|
||||
* void my_console_logger(ulog_level_t level, const char *msg) {
|
||||
* printf("%s [%s]: %s\n",
|
||||
* get_timestamp(),
|
||||
* ulog_level_name(level),
|
||||
* msg);
|
||||
* }
|
||||
*
|
||||
* int main() {
|
||||
* ULOG_INIT();
|
||||
*
|
||||
* // log to the console messages that are WARNING or more severe. You
|
||||
* // can re-subscribe at any point to change the severity level.
|
||||
* ULOG_SUBSCRIBE(my_console_logger, ULOG_WARNING);
|
||||
*
|
||||
* // log to a file messages that are DEBUG or more severe
|
||||
* ULOG_SUBSCRIBE(my_file_logger, ULOG_DEBUG);
|
||||
*
|
||||
* int arg = 42;
|
||||
* ULOG_INFO("Arg is %d", arg); // logs to file but not console
|
||||
* }
|
||||
*/
|
||||
|
||||
#ifndef ULOG_H_
|
||||
#define ULOG_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
ULOG_TRACE_LEVEL=100,
|
||||
ULOG_DEBUG_LEVEL,
|
||||
ULOG_INFO_LEVEL,
|
||||
ULOG_WARNING_LEVEL,
|
||||
ULOG_ERROR_LEVEL,
|
||||
ULOG_CRITICAL_LEVEL,
|
||||
ULOG_ALWAYS_LEVEL
|
||||
} ulog_level_t;
|
||||
|
||||
// The following macros enable or disable uLog. If `ULOG_ENABLED` is
|
||||
// defined at compile time, a macro such as `ULOG_INFO(...)` expands
|
||||
// into `ulog_message(ULOG_INFO_LEVEL, ...)`. If `ULOG_ENABLED` is not
|
||||
// defined, then the same macro expands into `do {} while(0)` and will
|
||||
// not generate any code at all.
|
||||
//
|
||||
// There are two ways to enable uLog: you can uncomment the following
|
||||
// line, or -- if it is commented out -- you can add -DULOG_ENABLED to
|
||||
// your compiler switches.
|
||||
#ifndef ULOG_ENABLED
|
||||
#define ULOG_ENABLED
|
||||
#endif
|
||||
|
||||
#ifdef ULOG_ENABLED
|
||||
#define ULOG_INIT() ulog_init()
|
||||
#define ULOG_SUBSCRIBE(a, b) ulog_subscribe(a, b)
|
||||
#define ULOG_UNSUBSCRIBE(a) ulog_unsubscribe(a)
|
||||
#define ULOG_LEVEL_NAME(a) ulog_level_name(a)
|
||||
#define ULOG(...) ulog_message(__FILE__, __VA_ARGS__)
|
||||
#define ULOG_TRACE(...) ulog_message(__FILE__, ULOG_TRACE_LEVEL, __VA_ARGS__)
|
||||
#define ULOG_DEBUG(...) ulog_message(__FILE__, ULOG_DEBUG_LEVEL, __VA_ARGS__)
|
||||
#define ULOG_INFO(...) ulog_message(__FILE__, ULOG_INFO_LEVEL, __VA_ARGS__)
|
||||
#define ULOG_WARNING(...) ulog_message(__FILE__, ULOG_WARNING_LEVEL, __VA_ARGS__)
|
||||
#define ULOG_ERROR(...) ulog_message(__FILE__, ULOG_ERROR_LEVEL, __VA_ARGS__)
|
||||
#define ULOG_CRITICAL(...) ulog_message(__FILE__, ULOG_CRITICAL_LEVEL, __VA_ARGS__)
|
||||
#define ULOG_ALWAYS(...) ulog_message(__FILE__, ULOG_ALWAYS_LEVEL, __VA_ARGS__)
|
||||
#else
|
||||
// uLog vanishes when disabled at compile time...
|
||||
#define ULOG_INIT() do {} while(0)
|
||||
#define ULOG_SUBSCRIBE(a, b) do {} while(0)
|
||||
#define ULOG_UNSUBSCRIBE(a) do {} while(0)
|
||||
#define ULOG_LEVEL_NAME(a) do {} while(0)
|
||||
#define ULOG(s, f, ...) do {} while(0)
|
||||
#define ULOG_TRACE(f, ...) do {} while(0)
|
||||
#define ULOG_DEBUG(f, ...) do {} while(0)
|
||||
#define ULOG_INFO(f, ...) do {} while(0)
|
||||
#define ULOG_WARNING(f, ...) do {} while(0)
|
||||
#define ULOG_ERROR(f, ...) do {} while(0)
|
||||
#define ULOG_CRITICAL(f, ...) do {} while(0)
|
||||
#define ULOG_ALWAYS(f, ...) do {} while(0)
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
ULOG_ERR_NONE = 0,
|
||||
ULOG_ERR_SUBSCRIBERS_EXCEEDED,
|
||||
ULOG_ERR_NOT_SUBSCRIBED,
|
||||
} ulog_err_t;
|
||||
|
||||
// define the maximum number of concurrent subscribers
|
||||
#ifndef ULOG_MAX_SUBSCRIBERS
|
||||
#define ULOG_MAX_SUBSCRIBERS 6
|
||||
#endif
|
||||
// maximum length of formatted log message
|
||||
#ifndef ULOG_MAX_MESSAGE_LENGTH
|
||||
#define ULOG_MAX_MESSAGE_LENGTH 120
|
||||
#endif
|
||||
/**
|
||||
* @brief: prototype for uLog subscribers.
|
||||
*/
|
||||
typedef void (*ulog_function_t)(ulog_level_t severity, const char *filename, char *msg);
|
||||
|
||||
void ulog_init(void);
|
||||
ulog_err_t ulog_subscribe(ulog_function_t fn, ulog_level_t threshold);
|
||||
ulog_err_t ulog_unsubscribe(ulog_function_t fn);
|
||||
const char *ulog_level_name(ulog_level_t level);
|
||||
void ulog_message(const char *filename, ulog_level_t severity, const char *fmt, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ULOG_H_ */
|
||||
Reference in New Issue
Block a user