Перенес вывод логов в отдельный поток, функция log только формирует строку и помещает ее в очередь для логирования.
This commit is contained in:
parent
8392f06b66
commit
336e342c14
27
include/log_queue.h
Normal file
27
include/log_queue.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#ifndef LOG_QUEUE_H
|
||||||
|
#define LOG_QUEUE_H
|
||||||
|
|
||||||
|
#include <queue>
|
||||||
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
|
|
||||||
|
class log_queue
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::thread _worker;
|
||||||
|
std::mutex _mutex;
|
||||||
|
std::condition_variable _cond;
|
||||||
|
std::queue<std::string> _queue;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void worker_func();
|
||||||
|
|
||||||
|
public:
|
||||||
|
log_queue();
|
||||||
|
~log_queue();
|
||||||
|
void add_log(const std::string& logLine);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* LOG_QUEUE_H */
|
||||||
|
|
||||||
@ -1,6 +1,8 @@
|
|||||||
#ifndef _LOGGER_H_
|
#ifndef _LOGGER_H_
|
||||||
#define _LOGGER_H_
|
#define _LOGGER_H_
|
||||||
|
|
||||||
|
#include "log_queue.h"
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -10,6 +12,7 @@ class logger
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::stringstream _logLine;
|
std::stringstream _logLine;
|
||||||
|
log_queue _queue;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
logger();
|
logger();
|
||||||
|
|||||||
33
src/log_queue.cpp
Normal file
33
src/log_queue.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include "log_queue.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
log_queue::log_queue(): _worker(&log_queue::worker_func, this)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
log_queue::~log_queue()
|
||||||
|
{
|
||||||
|
_worker.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
void log_queue::worker_func()
|
||||||
|
{
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(_mutex);
|
||||||
|
_cond.wait(lock, [this] { return !_queue.empty(); });
|
||||||
|
std::string line = _queue.front();
|
||||||
|
_queue.pop();
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
|
// actually logging
|
||||||
|
std::cout << line << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void log_queue::add_log(const std::string& logLine)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
_queue.push(logLine);
|
||||||
|
_cond.notify_one();
|
||||||
|
}
|
||||||
@ -17,6 +17,6 @@ void logger::log_internal(const char *s)
|
|||||||
_logLine << *s++;
|
_logLine << *s++;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << _logLine.str() << std::endl;
|
_queue.add_log(_logLine.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -571,5 +571,8 @@ int main(int argc, char **argv)
|
|||||||
ulog.log("one: %, two: %", one, two);
|
ulog.log("one: %, two: %", one, two);
|
||||||
ulog.log("Hello world!");
|
ulog.log("Hello world!");
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
std::cin >> i;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user