Skip to content

Commit

Permalink
simplify more
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed Feb 1, 2025
1 parent 792c303 commit 4f4ce28
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
26 changes: 11 additions & 15 deletions WickedEngine/wiBacklog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,28 @@ namespace wi::backlog
std::deque<LogEntry> entries;
std::mutex entriesLock;

std::string getTextWithoutLock()
std::string getText()
{
std::scoped_lock lck(entriesLock);
std::string retval;
for (auto& x : entries)
{
retval += x.text;
}
return retval;
}
void writeLogfileWithoutLock()
void writeLogfile()
{
static const std::string filename = wi::helper::GetCurrentPath() + "/log.txt";
std::string text = getTextWithoutLock();
std::string text = getText();
wi::helper::FileWrite(filename, (const uint8_t*)text.c_str(), text.length());
}
void writeLogfile()
{
std::scoped_lock lck(entriesLock);
writeLogfileWithoutLock();
}

~InternalState()
{
// The object will automatically write out the backlog to the temp folder when it's destroyed
// Should happen on application exit
std::scoped_lock lck(entriesLock);
writeLogfileWithoutLock();
writeLogfile();
}
} internal_state;

Expand Down Expand Up @@ -303,7 +298,7 @@ namespace wi::backlog
params.cursor = {};
if (refitscroll)
{
float textheight = wi::font::TextHeight(getText(), params); // getText() locks!
float textheight = wi::font::TextHeight(getText(), params);
float limit = canvas.GetLogicalHeight() - 50;
if (scroll + textheight > limit)
{
Expand All @@ -319,9 +314,11 @@ namespace wi::backlog
params.enableLinearOutputMapping(9);
}

internal_state.entriesLock.lock();
static std::deque<LogEntry> entriesCopy;
entriesCopy = internal_state.entries; // Force copy because drawing text while locking is not safe because on error it might try to lock again!

internal_state.entriesLock.lock();
// Force copy because drawing text while locking is not safe because an error inside might try to lock again!
entriesCopy = internal_state.entries;
internal_state.entriesLock.unlock();

for (auto& x : entriesCopy)
Expand All @@ -346,8 +343,7 @@ namespace wi::backlog

std::string getText()
{
std::scoped_lock lck(internal_state.entriesLock);
return internal_state.getTextWithoutLock();
return internal_state.getText();
}
void clear()
{
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace wi::version
// minor features, major updates, breaking compatibility changes
const int minor = 71;
// minor bug fixes, alterations, refactors, updates
const int revision = 669;
const int revision = 670;

const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);

Expand Down

0 comments on commit 4f4ce28

Please sign in to comment.