I have a logging system for my application Now this is what i do:
static void Background() {
while(IsAlive){
while(!logs.empty()){
ShowLog(log.front());
log.pop();
}
while(logs.empty()){
Sleep(200);
}
}
}
static void Init(){
// Some Checks
Logger::backgroundThread = std::thread(Background);
backgroundThread.detach();
}
static void Log(std::string log){
logs.push(log);
}
static void ShowLog(std::string log){
// Actual implementation is bit complex but that does not involve threads so i guess it is irrelevant for this question
std::cout << log << std::endl;
}
Here is a log
is a std::queue<std::string>
.
Now i am not very sure about whether this is a good approach or not.
Is there any better way to achieve this.
Note i am using C++17