Skip to content

Commit

Permalink
closes #571 Removed global lock at dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
abumq committed Oct 12, 2017
1 parent 1a3d09b commit 699c12e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
File renamed without changes.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

## [Unreleased]
### Fixes
- Build fix for kFreeBSD as suggested in issue 563
- Build fix for kFreeBSD as suggested in issue #563
- Fixed issue with deadlock on dispatch (see #571)

### Updates
- Added support for AIX (thanks to @apollo13)
- Added support for AIX (thanks to @apollo13)

## [9.95.0] - 02-08-2017
### Added
Expand Down
51 changes: 51 additions & 0 deletions samples/STL/multi-loggers-with-async.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// This file is part of Easylogging++ samples
//
// Revision 1.0
//

#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;
}
12 changes: 6 additions & 6 deletions src/easylogging++.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2368,7 +2368,6 @@ void LogDispatcher::dispatch(void) {
if (!m_proceed) {
return;
}
base::threading::ScopedLock scopedLock(ELPP->lock());
base::TypedConfigurations* tc = m_logMessage.logger()->m_typedConfigurations;
if (ELPP->hasFlag(LoggingFlag::StrictLogFileSizeCheck)) {
tc->validateFileRolling(m_logMessage.level(), ELPP->preRollOutCallback());
Expand Down Expand Up @@ -2444,12 +2443,13 @@ void Writer::initializeLogger(const std::string& loggerId, bool lookup, bool nee
m_logger = ELPP->registeredLoggers()->get(loggerId, ELPP->hasFlag(LoggingFlag::CreateLoggerAutomatically));
}
if (m_logger == nullptr) {
ELPP->acquireLock();
if (!ELPP->registeredLoggers()->has(std::string(base::consts::kDefaultLoggerId))) {
// Somehow default logger has been unregistered. Not good! Register again
ELPP->registeredLoggers()->get(std::string(base::consts::kDefaultLoggerId));
{
base::threading::ScopedLock scopedLock(ELPP->lock());
if (!ELPP->registeredLoggers()->has(std::string(base::consts::kDefaultLoggerId))) {
// Somehow default logger has been unregistered. Not good! Register again
ELPP->registeredLoggers()->get(std::string(base::consts::kDefaultLoggerId));
}
}
ELPP->releaseLock(); // Need to unlock it for next writer
Writer(Level::Debug, m_file, m_line, m_func).construct(1, base::consts::kDefaultLoggerId)
<< "Logger [" << loggerId << "] is not registered yet!";
m_proceed = false;
Expand Down

0 comments on commit 699c12e

Please sign in to comment.