Skip to content

Commit

Permalink
Core, logs: removed tag concatenation (#1524)
Browse files Browse the repository at this point in the history
* Core, logs: removed tag concatenation
* Logs: remove unused fn
* Logs: remove allocation
  • Loading branch information
DrZlo13 authored Aug 3, 2022
1 parent 93a4b9c commit 4a6477a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
44 changes: 40 additions & 4 deletions furi/core/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,60 @@ void furi_log_init() {
furi_log.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
}

void furi_log_print(FuriLogLevel level, const char* format, ...) {
void furi_log_print_format(FuriLogLevel level, const char* tag, const char* format, ...) {
if(level <= furi_log.log_level &&
furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk) {
string_t string;
string_init(string);

const char* color = FURI_LOG_CLR_RESET;
const char* log_letter = " ";
switch(level) {
case FuriLogLevelError:
color = FURI_LOG_CLR_E;
log_letter = "E";
break;
case FuriLogLevelWarn:
color = FURI_LOG_CLR_W;
log_letter = "W";
break;
case FuriLogLevelInfo:
color = FURI_LOG_CLR_I;
log_letter = "I";
break;
case FuriLogLevelDebug:
color = FURI_LOG_CLR_D;
log_letter = "D";
break;
case FuriLogLevelTrace:
color = FURI_LOG_CLR_T;
log_letter = "T";
break;
default:
break;
}

// Timestamp
string_init_printf(string, "%lu ", furi_log.timetamp());
string_printf(
string,
"%lu %s[%s][%s] " FURI_LOG_CLR_RESET,
furi_log.timetamp(),
color,
log_letter,
tag);
furi_log.puts(string_get_cstr(string));
string_clear(string);
string_reset(string);

va_list args;
va_start(args, format);
string_init_vprintf(string, format, args);
string_vprintf(string, format, args);
va_end(args);

furi_log.puts(string_get_cstr(string));
string_clear(string);

furi_log.puts("\r\n");

furi_mutex_release(furi_log.mutex);
}
}
Expand Down
30 changes: 14 additions & 16 deletions furi/core/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ typedef uint32_t (*FuriLogTimestamp)(void);
/** Initialize logging */
void furi_log_init();

/** Log record
*
* @param[in] level The level
* @param[in] format The format
* @param[in] <unnamed> VA args
/** Print log record
*
* @param level
* @param tag
* @param format
* @param ...
*/
void furi_log_print(FuriLogLevel level, const char* format, ...);
void furi_log_print_format(FuriLogLevel level, const char* tag, const char* format, ...);

/** Set log level
*
Expand All @@ -76,25 +77,22 @@ void furi_log_set_puts(FuriLogPuts puts);
*/
void furi_log_set_timestamp(FuriLogTimestamp timestamp);

#define FURI_LOG_FORMAT(log_letter, tag, format) \
FURI_LOG_CLR_##log_letter "[" #log_letter "][" tag "]: " FURI_LOG_CLR_RESET format "\r\n"
#define FURI_LOG_SHOW(tag, format, log_level, log_letter, ...) \
furi_log_print(log_level, FURI_LOG_FORMAT(log_letter, tag, format), ##__VA_ARGS__)

/** Log methods
*
* @param tag The application tag
* @param format The format
* @param ... VA Args
*/
#define FURI_LOG_E(tag, format, ...) \
FURI_LOG_SHOW(tag, format, FuriLogLevelError, E, ##__VA_ARGS__)
#define FURI_LOG_W(tag, format, ...) FURI_LOG_SHOW(tag, format, FuriLogLevelWarn, W, ##__VA_ARGS__)
#define FURI_LOG_I(tag, format, ...) FURI_LOG_SHOW(tag, format, FuriLogLevelInfo, I, ##__VA_ARGS__)
furi_log_print_format(FuriLogLevelError, tag, format, ##__VA_ARGS__)
#define FURI_LOG_W(tag, format, ...) \
furi_log_print_format(FuriLogLevelWarn, tag, format, ##__VA_ARGS__)
#define FURI_LOG_I(tag, format, ...) \
furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
#define FURI_LOG_D(tag, format, ...) \
FURI_LOG_SHOW(tag, format, FuriLogLevelDebug, D, ##__VA_ARGS__)
furi_log_print_format(FuriLogLevelDebug, tag, format, ##__VA_ARGS__)
#define FURI_LOG_T(tag, format, ...) \
FURI_LOG_SHOW(tag, format, FuriLogLevelTrace, T, ##__VA_ARGS__)
furi_log_print_format(FuriLogLevelTrace, tag, format, ##__VA_ARGS__)

#ifdef __cplusplus
}
Expand Down

0 comments on commit 4a6477a

Please sign in to comment.