Skip to content

Commit

Permalink
Fix potential use before initialization for logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticial committed Dec 18, 2024
1 parent 8f9cfa5 commit 8d58d26
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,45 @@ FileWindowLogger::FileWindowLogger(const std::string& path)
: m_file(QString::fromStdString(path))
, m_max_queue_size(LOG_HISTORY_LINES)
, m_stopping(false)
, m_thread(&FileWindowLogger::thread_loop, this)
{
bool exists = m_file.exists();
m_file.open(QIODevice::WriteOnly | QIODevice::Append);
if (!exists){
std::string bom = "\xef\xbb\xbf";
m_file.write(bom.c_str(), bom.size());
}

m_thread = std::thread(&FileWindowLogger::thread_loop, this);
}
void FileWindowLogger::operator+=(FileWindowLoggerWindow& widget){
auto scope_check = m_sanitizer.check_scope();
std::lock_guard<std::mutex> lg(m_lock);
m_windows.insert(&widget);
}
void FileWindowLogger::operator-=(FileWindowLoggerWindow& widget){
auto scope_check = m_sanitizer.check_scope();
std::lock_guard<std::mutex> lg(m_lock);
m_windows.erase(&widget);
}

void FileWindowLogger::log(const std::string& msg, Color color){
auto scope_check = m_sanitizer.check_scope();
std::unique_lock<std::mutex> lg(m_lock);
m_last_log_tracker += msg;
m_cv.wait(lg, [this]{ return m_queue.size() < m_max_queue_size; });
m_queue.emplace_back(msg, color);
m_cv.notify_all();
}
void FileWindowLogger::log(std::string&& msg, Color color){
auto scope_check = m_sanitizer.check_scope();
std::unique_lock<std::mutex> lg(m_lock);
m_last_log_tracker += msg;
m_cv.wait(lg, [this]{ return m_queue.size() < m_max_queue_size; });
m_queue.emplace_back(std::move(msg), color);
m_cv.notify_all();
}
std::vector<std::string> FileWindowLogger::get_last() const{
auto scope_check = m_sanitizer.check_scope();
std::unique_lock<std::mutex> lg(m_lock);
return m_last_log_tracker.snapshot();
}
Expand Down Expand Up @@ -154,6 +160,7 @@ QString FileWindowLogger::to_window_str(const std::string& msg, Color color){
return QString::fromStdString(str);
}
void FileWindowLogger::internal_log(const std::string& msg, Color color){
auto scope_check = m_sanitizer.check_scope();
std::string line = normalize_newlines(msg);
{
if (!m_windows.empty()){
Expand All @@ -169,6 +176,7 @@ void FileWindowLogger::internal_log(const std::string& msg, Color color){
}
}
void FileWindowLogger::thread_loop(){
auto scope_check = m_sanitizer.check_scope();
std::unique_lock<std::mutex> lg(m_lock);
while (true){
m_cv.wait(lg, [&]{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <QFile>
#include <QTextEdit>
#include <QMainWindow>
#include "Common/Cpp/LifetimeSanitizer.h"
#include "Logger.h"

namespace PokemonAutomation{
Expand Down Expand Up @@ -66,6 +67,8 @@ class FileWindowLogger : public Logger{
std::deque<std::pair<std::string, Color>> m_queue;
std::set<FileWindowLoggerWindow*> m_windows;
std::thread m_thread;

LifetimeSanitizer m_sanitizer;
};


Expand Down

0 comments on commit 8d58d26

Please sign in to comment.