2022-10-29 03:12 PM
Hi All,
I've had some logging code written some time back and I'd like to be able to selectively prepend the date/time onto what gets passed in however I'm at a loss as to know how to do this or where to start. The code that was supplied is below.
#ifndef LOGGING_H_
#define LOGGING_H_
#include <stdarg.h>
#include <stdio.h>
#define LOG_LEVEL_SILENT 0
#define LOG_LEVEL_ERROR 1
#define LOG_LEVEL_INFO 2
#define LOG_LEVEL_DEBUG 3
#define DEFAULT_LOG_LEVEL LOG_LEVEL_DEBUG
/* global log level which can be accessed anywhere this file is included */
extern uint8_t log_level_global_;
#define LOG(level, fmt, ...) \
do { if (level <= log_level_global_) printf(fmt, ##__VA_ARGS__); } while (0)
#define LOGLN(level, fmt, ...) \
do { if (level <= log_level_global_) printf(fmt"\r\n", ##__VA_ARGS__); } while (0)
#define LOG_ERROR(fmt, ...) LOG(LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
#define LOG_INFO(fmt, ...) LOG(LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
#define LOG_DEBUG(fmt, ...) LOG(LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#define LOGLN_ERROR(fmt, ...) LOGLN(LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
#define LOGLN_INFO(fmt, ...) LOGLN(LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
#define LOGLN_DEBUG(fmt, ...) LOGLN(LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
void logging_set_log_level(uint8_t level);
uint8_t logging_get_log_level(void);
#endif
And I'd like to add something like the following to the beginning of the line without having to pass it in every time. Calling a function like LOGLN_INFO_DT would be ideal or similar.
char timebuf[20];
time_keeper_get_time_string(timebuf);
LOGLN_INFO("\r\n\n%s \tSYSTEM Initializing", timebuf);
Thanks,
Paul