Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Arret propre du logger (pas encore fonctionnel)

  • Loading branch information...
commit 0184e38652d37454fdd5fe2b37b3a6830976198e 1 parent 06e1363
@pgentile authored
Showing with 90 additions and 43 deletions.
  1. +37 −15 include/logger.hpp
  2. +1 −1  src/app.cpp
  3. +52 −27 src/logger.cpp
View
52 include/logger.hpp
@@ -35,36 +35,58 @@ namespace logger
};
- class LogEvent
+ class Event
{
- public:
- LogEvent(pthread_t threadId, const string& message);
- virtual ~LogEvent();
- inline const pthread_t threadId(void) {
- return _threadId;
+ public:
+
+ enum Kind {
+ LOG_EVENT,
+ SHUTDOWN
+ };
+
+ Event(Kind kind);
+
+ inline Kind kind(void) const {
+ return _kind;
}
- inline const string& message(void) {
+ private:
+ Kind _kind;
+
+ };
+
+ class LogEvent: public Event
+ {
+ public:
+ LogEvent(const string& message);
+
+ inline const string& message(void) const {
return _message;
}
private:
- pthread_t _threadId;
string _message;
};
+
+ class ShutdownEvent: public Event
+ {
+ public:
+ ShutdownEvent(void);
+
+ };
- class LogEventConsumer: public Thread
+ class Consumer: public Thread
{
public:
- LogEventConsumer(queue<LogEvent*>& pendingEvents, Mutex& mutex, Condition& publishedCond);
+ Consumer(queue<Event*>& pendingEvents, Mutex& mutex, Condition& publishedCond);
virtual void run();
- virtual ~LogEventConsumer();
+ virtual ~Consumer();
private:
- queue<LogEvent*>& _pendingEvents;
+ queue<Event*>& _pendingEvents;
Mutex& _mutex;
Condition& _publishedCond;
};
@@ -79,14 +101,14 @@ namespace logger
void log(const char* message);
protected:
- void _publishEvent(LogEvent* event);
+ void _publishEvent(Event* event);
private:
- queue<LogEvent*> _pendingEvents;
+ queue<Event*> _pendingEvents;
Mutex _mutex;
Condition _publishedCond;
- LogEventConsumer _consumer;
+ Consumer _consumer;
};
View
2  src/app.cpp
@@ -22,7 +22,7 @@ int main(void)
loggerManager.log("Test d'un log 1");
loggerManager.log("Test d'un log 2");
- unsigned int duration = 10;
+ unsigned int duration = 5;
cout << "On dort..." << endl;
sleep(duration);
View
79 src/logger.cpp
@@ -14,32 +14,45 @@ namespace logger
using namespace std;
using namespace logger;
+ // Class Level
+
const Level Level::debug(0);
const Level Level::info(500);
const Level Level::warn(1000);
const Level Level::error(1500);
const Level Level::fatal(2000);
+
+ // Class Consumer
- void LogEventConsumer::run()
+ void Consumer::run()
{
cout << "Consuming logs..." << endl;
+ bool running = true;
+
MutexLock lock(_mutex);
- while (!_pendingEvents.empty()) {
+ while (running) {
+ // Waiting signal
+ _publishedCond.wait();
// Consume pending events
while (!_pendingEvents.empty()) {
- auto_ptr<LogEvent> event(_pendingEvents.front()); // Delete on scope exit
- cout << "LOGGER EVENT: Thread " << event->threadId() << ": " << event->message() << endl;
+ auto_ptr<Event> event(_pendingEvents.front()); // Delete event on scope exit
+ Event::Kind kind = event->kind();
+ if (kind == Event::LOG_EVENT) {
+ LogEvent* logEvent = static_cast<LogEvent*>(event.get());
+ cout << "LOGGER EVENT: " << logEvent->message() << endl;
+ } else if (kind == Event::SHUTDOWN) {
+ running = false;
+ cout << "Shutdown received" << endl;
+ }
+
_pendingEvents.pop();
}
-
- // Waiting signal
- _publishedCond.wait();
}
}
- LogEventConsumer::LogEventConsumer(queue<LogEvent*>& pendingEvents, Mutex& mutex, Condition& publishedCond):
+ Consumer::Consumer(queue<Event*>& pendingEvents, Mutex& mutex, Condition& publishedCond):
_pendingEvents(pendingEvents),
_mutex(mutex),
_publishedCond(publishedCond)
@@ -47,19 +60,32 @@ namespace logger
}
- LogEventConsumer::~LogEventConsumer(void)
+ Consumer::~Consumer(void)
{
}
+ // Class Event
+
+ Event::Event(Kind kind) {
+ _kind = kind;
+ }
- LogEvent::LogEvent(pthread_t threadId, const string& message) :
- _threadId(threadId), _message(message)
+ // Class LogEvent
+
+ LogEvent::LogEvent(const string& message) :
+ Event(LOG_EVENT),
+ _message(message)
{
}
- LogEvent::~LogEvent()
+ // Class ShutdownEvent
+
+ ShutdownEvent::ShutdownEvent(void):
+ Event(LOG_EVENT)
{
}
+
+ // Class LoggerManager
LoggerManager::LoggerManager(void):
_publishedCond(_mutex),
@@ -67,33 +93,32 @@ namespace logger
{
_consumer.start();
}
+
+ LoggerManager::~LoggerManager()
+ {
+ Event* event = new ShutdownEvent();
+ _publishEvent(event);
+
+ _consumer.join();
+ }
void LoggerManager::log(string& message)
{
- pthread_t threadId = pthread_self();
- LogEvent* event = new LogEvent(threadId, message);
+ Event* event = new LogEvent(message);
_publishEvent(event);
}
void LoggerManager::log(const char* message)
{
- pthread_t threadId = pthread_self();
string messageStr = message;
- LogEvent* event = new LogEvent(threadId, messageStr);
- _publishEvent(event);
+ log(messageStr);
}
- void LoggerManager::_publishEvent(LogEvent* event)
- {
- {
- MutexLock lock(_mutex);
- _pendingEvents.push(event);
- _publishedCond.signal();
- }
- }
-
- LoggerManager::~LoggerManager()
+ void LoggerManager::_publishEvent(Event* event)
{
+ MutexLock lock(_mutex);
+ _pendingEvents.push(event);
+ _publishedCond.signal();
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.