Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow registering existing callbacks #452

Closed
abumq opened this issue Jan 8, 2017 · 2 comments
Closed

Allow registering existing callbacks #452

abumq opened this issue Jan 8, 2017 · 2 comments

Comments

@abumq
Copy link
Owner

abumq commented Jan 8, 2017

All the callbacks need to be modified in order to use existing callback (and if not provided create new as we're doing now to prevent breaking of existing code)

Essentially

template<typename T>
static inline bool installLoggerRegistrationCallback(const std::string& id) {

to

template<typename T>
static inline bool installLoggerRegistrationCallback(const std::string& id, T* cb = nullptr) {

This will allow us to function better in callbacks and allow us to import more stuffs.

Wait until #445 is merged since we are holding off any merges and waiting for @aparajita

@abumq abumq added this to the v9.90 milestone Jan 8, 2017
@aparajita
Copy link
Contributor

See #453. Go ahead and make the changes to that branch.

@easylogging
Copy link
Contributor

easylogging commented Jan 11, 2017

We don't need this, we can set data for the callback using el::Loggers::loggerRegistrationCallback<T>()

Sample code:

#include "easylogging++.h"

INITIALIZE_EASYLOGGINGPP

class Data {
public:
    explicit Data(const std::string& name) : m_name(name) {}
    std::string name() const { return m_name; }
private:
    std::string m_name;
};

class Handler : public el::LoggerRegistrationCallback {
public:
    Handler() : m_data(nullptr) {}
    void setData(const Data* d) { m_data = d; }
protected:
    virtual void handle(const el::Logger* logger) {
       ELPP_COUT << "(Handler) Registered new logger " << logger->id() << " m_data = " << m_data->name() << std::endl;
    }
private:
    const Data* m_data;
};

void setup(Data* d) {
    const std::string configuratorName = "handler";
    el::Loggers::installLoggerRegistrationCallback<Handler>(configuratorName);
    Handler* h = el::Loggers::loggerRegistrationCallback<Handler>(configuratorName); // Get registered / installed callback with specified name
    h->setData(d);
}
int main(void) {

    Data d("mydd");
    setup(&d);

    LOG(INFO) << "Now we will register three loggers";

    el::Loggers::getLogger("logger1");
    el::Loggers::getLogger("logger2");
    el::Loggers::getLogger("logger3");
    return 0;
}

If we compile and run this
g++ test.cpp easylogging++.cc -o bin/test.cpp.bin -std=c++11

We get

$ ./bin/test.cpp.bin
2017-01-11 14:56:02,474 INFO  [default] Now we will register three loggers
(Handler) Registered new logger logger1 m_data = mydd
(Handler) Registered new logger logger2 m_data = mydd
(Handler) Registered new logger logger3 m_data = mydd

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

No branches or pull requests

3 participants