Vector35 / binaryninja-api

Public API, examples, documentation and issues for Binary Ninja
https://binary.ninja/
MIT License
917 stars 207 forks source link

Add Logging callback apis to Python API #2236

Open gkprow-runsafe opened 3 years ago

gkprow-runsafe commented 3 years ago

Current Binja release: Version 2.2.2603-dev (Build ID 8f9fea38)

Current functionality: The logging API allows users to limit which LogLevels should be written to stdout/err, to enable writing logs to a file, etc. But, when a log message is written to stdout/err or a file, only the message itself is written. No metadata is written along with the message (e.g., no timestamps, no PID, etc...).

Desired functionality: There are cases where it would be helpful to supply a custom prefix to the log messages written either by the user (e.g., bn.log.log()) or by Binja's core lib.

Here are a couple of approaches for your consideration:

  1. Enhance the log API to allow the user to supply a callback for writing the logs. The user API would receive a string parameter that is the log message Binja wants to write. The user callback would simply pass that into print along with any other info the user wants to print.
  2. Enhance the log API to receive a function that returns a string, or allows a user to specify a literal string, to prefix to the log written by the core lib.
nshp commented 3 years ago

This is already how the core API works:

    // Callbacks
    struct BNLogListener
    {
        void* context;
        void (*log)(void* ctxt, BNLogLevel level, const char* msg);
        void (*close)(void* ctxt);
        BNLogLevel (*getLogLevel)(void* ctxt);
    };

    BINARYNINJACOREAPI void BNRegisterLogListener(BNLogListener* listener);
    BINARYNINJACOREAPI void BNUnregisterLogListener(BNLogListener* listener);
    BINARYNINJACOREAPI void BNUpdateLogListeners(void);

There's just no Python wrapper for that part currently. It'd be easy to wrap that native API as an addition to log.py, if you wanted to submit a PR?

gkprow-runsafe commented 3 years ago

Sure thing. I'll work to create a PR.

plafosse commented 3 years ago

Changing title to reflect actual issue.