Skip to content

Conversation

@isiloron
Copy link

@isiloron isiloron commented Sep 28, 2025

Implementation of issue #137

Adds flag NOB_LOG_CUSTOM_IMPLEMENTATION.

When defined it will declare the function nob_log_default which has the same signature as nob_log.
If NOB_IMPLEMENTATION is defined, the nob_log definition will be renamed to nob_log_default.
If NOB_STRIP_PREFIX is defined, log_default will become an alias for nob_log_default.

The user has to define and implement nob_log on their own after including nob.h with the NOB_IMPLEMENTATION and NOB_LOG_CUSTOM_IMPLEMENTATION flags set.
The original nob_log implementation is still accessible by the user via nob_log_default.

Example:

// File: nob.c
#define NOB_IMPLEMENTATION
#define NOB_LOG_CUSTOM_IMPLEMENTATION
#include "nob.h"
#include "nob_extra.h"
#include "nob_bonus.h"

// Custom nob_log
void nob_log(Nob_Log_Level level, const char *fmt, ...)
{
    fprintf(stderr, "[CRITICAL ERROR] Urmom!\n");

    // Original nob_log accessible via nob_log_default
    va_list args;
    va_start(args, fmt);
    nob_log_default(level, fmt, args);
    va_end(args);
}

int main(int argc, char **argv)
{
    nob_log(NOB_INFO, "Start of build!");
    /*
    * The line above will print the following to stderr:
    * [CRITICAL ERROR] Urmom!
    * [INFO] Start of build!
    */

    nob_extra_foo();
    nob_bonus_bar();

    // <Rest of build goes here>
}
// File: nob_extra.c
#include "nob_extra.h"
#define NOB_LOG_CUSTOM_IMPLEMENTATION
#include "nob.h"

void nob_extra_foo()
{
    // Both lines below will print to stderr, the second line will circumvent the custom implementation.
    nob_log(LOG_INFO, "Hello custom logger from nob_extra_foo!");
    nob_log_default(LOG_INFO, "Hello default logger from nob_extra_foo!");
}
// File: nob_bonus.c
#include "nob_bonus.h"
#include "nob.h"

void nob_bonus_bar()
{
    /*
    * Of the lines below, only the first will compile without error.
    * In >=C99 (at least for gcc), the second line will most likely fail to compile
    * due to implicit function declaration being an error by default.
    * This is because NOB_LOG_CUSTOM_IMPLEMENTATION was not defined
    * before inclusion of nob.h
    */
    nob_log(LOG_INFO, "Hello custom logger from nob_bonus_bar!");
    nob_log_default(LOG_INFO, "Hello default logger from nob_bonus_bar!");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant