Skip to content

Commit

Permalink
workaround clean old logs
Browse files Browse the repository at this point in the history
  • Loading branch information
fxliang committed Jun 14, 2024
1 parent 2ca5a52 commit 0d20317
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <StringAlgorithm.hpp>
#include <WeaselConstants.h>
#include <WeaselUtility.h>
#include <boost/algorithm/string.hpp>
#include <vector>

#include <filesystem>
#include <map>
Expand Down Expand Up @@ -38,6 +40,40 @@ int expand_ibus_modifier(int m) {
return (m & 0xff) | ((m & 0xff00) << 16);
}

static void CleanOldLogs() {
char ymd[12] = {0};
time_t now = time(NULL);
strftime(ymd, sizeof(ymd), ".%Y%m%d", localtime(&now));
std::string today(ymd);
const std::string app_name = "rime.weasel";
std::string dir = WeaselLogPath().string();
if (!fs::exists(fs::path(dir)))
return;
std::vector<fs::path> files;
try {
// preparing files
for (const auto& entry : fs::directory_iterator(dir)) {
const std::string& file_name(entry.path().filename().string());
if (entry.is_regular_file() && !entry.is_symlink() &&
boost::starts_with(file_name, app_name) &&
boost::ends_with(file_name, ".log") &&
!boost::contains(file_name, today)) {
files.push_back(entry.path());
}
}
} catch (const fs::filesystem_error& ex) {
}
// remove files
for (const auto& file : files) {
try {
// ensure write permission
fs::permissions(file, fs::perms::owner_write);
fs::remove(file);
} catch (const fs::filesystem_error& ex) {
}
}
}

RimeWithWeaselHandler::RimeWithWeaselHandler(UI* ui)
: m_ui(ui),
m_active_session(0),
Expand Down Expand Up @@ -120,6 +156,7 @@ void RimeWithWeaselHandler::Initialize() {
m_disabled = true;
}
#endif
CleanOldLogs();

RimeConfig config = {NULL};
if (RimeConfigOpen("weasel", &config)) {
Expand Down

0 comments on commit 0d20317

Please sign in to comment.