Skip to content

Commit 769756c

Browse files
committed
Add flag to enable custom implementation of nob_log
1 parent 7deb15d commit 769756c

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

nob.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const char *test_names[] = {
2626
"temp_aligned_alloc",
2727
"temp_path_comps",
2828
"temp_running_executable_path",
29+
"nob_log_custom_implementation",
2930
};
3031
#define test_names_count ARRAY_LEN(test_names)
3132

nob.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
it works on Windows, so it's experimental for now.
8282
- NOB_STRIP_PREFIX - string the `nob_` prefixes from non-redefinable names.
8383
- NOB_NO_ECHO - do not echo the actions various nob functions are doing (like nob_cmd_run(), nob_mkdir_if_not_exists(), etc).
84+
- NOB_LOG_CUSTOM_IMPLEMENTATION - Declares nob_log_default as a separate function with the same signature as nob_log
85+
and renames the definition of nob_log to nob_log_default. The user must define and implement nob_log themselves.
8486
8587
## Redefinable Macros
8688
@@ -207,7 +209,12 @@ typedef enum {
207209
// Any messages with the level below nob_minimal_log_level are going to be suppressed.
208210
extern Nob_Log_Level nob_minimal_log_level;
209211

210-
NOBDEF void nob_log(Nob_Log_Level level, const char *fmt, ...) NOB_PRINTF_FORMAT(2, 3);
212+
#define NOB_LOG_DECLARATION(nob_log_name) NOBDEF void nob_log_name(Nob_Log_Level level, const char *fmt, ...) NOB_PRINTF_FORMAT(2, 3)
213+
NOB_LOG_DECLARATION(nob_log);
214+
#ifdef NOB_LOG_CUSTOM_IMPLEMENTATION
215+
NOB_LOG_DECLARATION(nob_log_default);
216+
#endif
217+
#undef NOB_LOG_DECLARATION
211218

212219
// It is an equivalent of shift command from bash (do `help shift` in bash). It basically
213220
// pops an element from the beginning of a sized array.
@@ -1535,7 +1542,13 @@ NOBDEF bool nob_cmd_run_sync_redirect_and_reset(Nob_Cmd *cmd, Nob_Cmd_Redirect r
15351542
return nob_proc_wait(p);
15361543
}
15371544

1538-
NOBDEF void nob_log(Nob_Log_Level level, const char *fmt, ...)
1545+
NOBDEF void
1546+
#ifdef NOB_LOG_CUSTOM_IMPLEMENTATION
1547+
nob_log_default
1548+
#else
1549+
nob_log
1550+
#endif
1551+
(Nob_Log_Level level, const char *fmt, ...)
15391552
{
15401553
if (level < nob_minimal_log_level) return;
15411554

@@ -2344,6 +2357,9 @@ NOBDEF int closedir(DIR *dirp)
23442357
// NOTE: Name log is already defined in math.h and historically always was the natural logarithmic function.
23452358
// So there should be no reason to strip the `nob_` prefix in this specific case.
23462359
// #define log nob_log
2360+
#ifdef NOB_LOG_CUSTOM_IMPLEMENTATION
2361+
#define log_default nob_log_default
2362+
#endif // NOB_LOG_CUSTOM_IMPLEMENTATION
23472363
#define shift nob_shift
23482364
#define shift_args nob_shift_args
23492365
#define File_Paths Nob_File_Paths
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#define NOB_IMPLEMENTATION
2+
#define NOB_LOG_CUSTOM_IMPLEMENTATION
3+
#include "nob.h"
4+
5+
const char * log_level_to_string_literal(Nob_Log_Level level)
6+
{
7+
switch (level) {
8+
case NOB_INFO: return "INFO";
9+
case NOB_WARNING: return "WARNING";
10+
case NOB_ERROR: return "ERROR";
11+
case NOB_NO_LOGS: return "NO_LOGS";
12+
default: return "unknown";
13+
}
14+
}
15+
16+
// Custom implementation of nob_log
17+
void nob_log(Nob_Log_Level level, const char *fmt, ...)
18+
{
19+
fprintf(stderr, "[CUSTOM] Persistent Custom Log Test Message (Level: %s)\n",
20+
log_level_to_string_literal(level)
21+
);
22+
23+
// You can still use the default implementation of nob_log
24+
va_list args;
25+
va_start(args, fmt);
26+
nob_log_default(level, fmt, args);
27+
va_end(args);
28+
}
29+
30+
void log_test_messages(void)
31+
{
32+
nob_log(NOB_INFO, "Info Test Message");
33+
nob_log(NOB_WARNING, "Warning Test Message");
34+
nob_log(NOB_ERROR, "Error Test Message");
35+
nob_log(NOB_NO_LOGS, "YOU SHOULD NEVER SEE THIS");
36+
}
37+
38+
int main(void)
39+
{
40+
log_test_messages();
41+
return 0;
42+
}

0 commit comments

Comments
 (0)