Skip to content

Commit 4a6477a

Browse files
authored
Core, logs: removed tag concatenation (#1524)
* Core, logs: removed tag concatenation * Logs: remove unused fn * Logs: remove allocation
1 parent 93a4b9c commit 4a6477a

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

furi/core/log.c

+40-4
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,60 @@ void furi_log_init() {
2222
furi_log.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
2323
}
2424

25-
void furi_log_print(FuriLogLevel level, const char* format, ...) {
25+
void furi_log_print_format(FuriLogLevel level, const char* tag, const char* format, ...) {
2626
if(level <= furi_log.log_level &&
2727
furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk) {
2828
string_t string;
29+
string_init(string);
30+
31+
const char* color = FURI_LOG_CLR_RESET;
32+
const char* log_letter = " ";
33+
switch(level) {
34+
case FuriLogLevelError:
35+
color = FURI_LOG_CLR_E;
36+
log_letter = "E";
37+
break;
38+
case FuriLogLevelWarn:
39+
color = FURI_LOG_CLR_W;
40+
log_letter = "W";
41+
break;
42+
case FuriLogLevelInfo:
43+
color = FURI_LOG_CLR_I;
44+
log_letter = "I";
45+
break;
46+
case FuriLogLevelDebug:
47+
color = FURI_LOG_CLR_D;
48+
log_letter = "D";
49+
break;
50+
case FuriLogLevelTrace:
51+
color = FURI_LOG_CLR_T;
52+
log_letter = "T";
53+
break;
54+
default:
55+
break;
56+
}
2957

3058
// Timestamp
31-
string_init_printf(string, "%lu ", furi_log.timetamp());
59+
string_printf(
60+
string,
61+
"%lu %s[%s][%s] " FURI_LOG_CLR_RESET,
62+
furi_log.timetamp(),
63+
color,
64+
log_letter,
65+
tag);
3266
furi_log.puts(string_get_cstr(string));
33-
string_clear(string);
67+
string_reset(string);
3468

3569
va_list args;
3670
va_start(args, format);
37-
string_init_vprintf(string, format, args);
71+
string_vprintf(string, format, args);
3872
va_end(args);
3973

4074
furi_log.puts(string_get_cstr(string));
4175
string_clear(string);
4276

77+
furi_log.puts("\r\n");
78+
4379
furi_mutex_release(furi_log.mutex);
4480
}
4581
}

furi/core/log.h

+14-16
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ typedef uint32_t (*FuriLogTimestamp)(void);
4444
/** Initialize logging */
4545
void furi_log_init();
4646

47-
/** Log record
48-
*
49-
* @param[in] level The level
50-
* @param[in] format The format
51-
* @param[in] <unnamed> VA args
47+
/** Print log record
48+
*
49+
* @param level
50+
* @param tag
51+
* @param format
52+
* @param ...
5253
*/
53-
void furi_log_print(FuriLogLevel level, const char* format, ...);
54+
void furi_log_print_format(FuriLogLevel level, const char* tag, const char* format, ...);
5455

5556
/** Set log level
5657
*
@@ -76,25 +77,22 @@ void furi_log_set_puts(FuriLogPuts puts);
7677
*/
7778
void furi_log_set_timestamp(FuriLogTimestamp timestamp);
7879

79-
#define FURI_LOG_FORMAT(log_letter, tag, format) \
80-
FURI_LOG_CLR_##log_letter "[" #log_letter "][" tag "]: " FURI_LOG_CLR_RESET format "\r\n"
81-
#define FURI_LOG_SHOW(tag, format, log_level, log_letter, ...) \
82-
furi_log_print(log_level, FURI_LOG_FORMAT(log_letter, tag, format), ##__VA_ARGS__)
83-
8480
/** Log methods
8581
*
8682
* @param tag The application tag
8783
* @param format The format
8884
* @param ... VA Args
8985
*/
9086
#define FURI_LOG_E(tag, format, ...) \
91-
FURI_LOG_SHOW(tag, format, FuriLogLevelError, E, ##__VA_ARGS__)
92-
#define FURI_LOG_W(tag, format, ...) FURI_LOG_SHOW(tag, format, FuriLogLevelWarn, W, ##__VA_ARGS__)
93-
#define FURI_LOG_I(tag, format, ...) FURI_LOG_SHOW(tag, format, FuriLogLevelInfo, I, ##__VA_ARGS__)
87+
furi_log_print_format(FuriLogLevelError, tag, format, ##__VA_ARGS__)
88+
#define FURI_LOG_W(tag, format, ...) \
89+
furi_log_print_format(FuriLogLevelWarn, tag, format, ##__VA_ARGS__)
90+
#define FURI_LOG_I(tag, format, ...) \
91+
furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
9492
#define FURI_LOG_D(tag, format, ...) \
95-
FURI_LOG_SHOW(tag, format, FuriLogLevelDebug, D, ##__VA_ARGS__)
93+
furi_log_print_format(FuriLogLevelDebug, tag, format, ##__VA_ARGS__)
9694
#define FURI_LOG_T(tag, format, ...) \
97-
FURI_LOG_SHOW(tag, format, FuriLogLevelTrace, T, ##__VA_ARGS__)
95+
furi_log_print_format(FuriLogLevelTrace, tag, format, ##__VA_ARGS__)
9896

9997
#ifdef __cplusplus
10098
}

0 commit comments

Comments
 (0)