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

Certain scenerio and custom log dispatch causes deadlock at dispatch because of global lock #571

Closed
abumq opened this issue Oct 12, 2017 · 0 comments
Labels

Comments

@abumq
Copy link
Owner

abumq commented Oct 12, 2017

Consider following program (also added to samples)

#include <chrono>
#include <thread>
#include <future>
#include "easylogging++.h"

INITIALIZE_EASYLOGGINGPP

void f() {
    std::async(std::launch::async, [&]() {
        std::cout << "[internal] inside async()" << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
        LOG(INFO) << "This is from async";
    });
}

class MyHandler : public el::LogDispatchCallback {
public:
    void handle(const el::LogDispatchData* d) {
        std::cout << "Message: " << d->logMessage()->message() << " [logger: " << d->logMessage()->logger()->id() << "]" << std::endl;
        if (d->logMessage()->logger()->id() != "default") {
            std::cout << "[internal] calling f()" << std::endl;
            f();
        }
    }
};



int main(int,char**){
    el::Loggers::addFlag(el::LoggingFlag::CreateLoggerAutomatically);
    // el::Helpers::uninstallLogDispatchCallback<el::base::DefaultLogDispatchCallback>("DefaultLogDispatchCallback");
    el::Helpers::installLogDispatchCallback<MyHandler>("MyHandler");

    LOG(INFO)<<"The program has started!";
    CLOG(INFO, "frommain") << "THis is another";

    std::thread t1([](){
        LOG(INFO) << "This is from thread";
    });

    t1.join();

    LOG(INFO) << "Shutting down.";
    return 0;
}

we should be able to use LOG(...) (i.e, default logger) in above case as we have custom handler. But because of global lock ELPP->lock() in LogDispatch this scenerio is not possible. We need to allow this scenerio

The expected output is

2017-10-12 13:55:48,712 INFO [default] The program has started!
Message: The program has started! [logger: default]
2017-10-12 13:55:48,713 INFO [frommain] THis is another
Message: THis is another [logger: frommain]
[internal] calling f()
[internal] inside async()
2017-10-12 13:55:49,716 INFO [default] This is from async
Message: This is from async [logger: default]
2017-10-12 13:55:49,717 INFO [default] This is from thread
Message: This is from thread [logger: default]
2017-10-12 13:55:49,717 INFO [default] Shutting down.
Message: Shutting down. [logger: default]

but we are getting a deadlock

2017-10-12 14:00:00,587 INFO [default] The program has started!
Message: The program has started! [logger: default]
2017-10-12 14:00:00,588 INFO [frommain] THis is another
Message: THis is another [logger: frommain]
[internal] calling f()
[internal] inside async()
... {no output after this}

Documentation

In documentation it says

DO NOT LOG ANYTHING IN THIS HANDLER OR YOU WILL END UP IN INFINITE-LOOP

that is still true but above if (d->logMessage()->logger()->id() != "default") { should allow logging with default logger inside the dispatch handler

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

No branches or pull requests

1 participant