Skip to content

Commit 67b2621

Browse files
committed
Remove debugs, add enable / disable, refactor
1 parent c050add commit 67b2621

2 files changed

Lines changed: 52 additions & 83 deletions

File tree

plugins/logcleaner/logcleaner.cpp

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "Debug.h"
21
#include "LuaTools.h"
32
#include "PluginManager.h"
43
#include "PluginLua.h"
@@ -32,33 +31,26 @@ static bool clear_combat = false;
3231
static bool clear_sparring = true;
3332
static bool clear_hunting = false;
3433

35-
namespace DFHack {
36-
DBG_DECLARE(logcleaner, control, DebugCategory::LINFO);
37-
DBG_DECLARE(logcleaner, cleanup, DebugCategory::LINFO);
38-
}
39-
4034
static void cleanupLogs(color_ostream& out);
4135
static command_result do_command(color_ostream& out, std::vector<std::string>& params);
42-
static void do_enable();
43-
static void do_disable();
4436

4537
// Getter functions for Lua
4638
static bool logcleaner_getCombat() { return clear_combat; }
4739
static bool logcleaner_getSparring() { return clear_sparring; }
4840
static bool logcleaner_getHunting() { return clear_hunting; }
4941

5042
// Setter functions for Lua (also persist to config)
51-
static void logcleaner_setCombat(color_ostream& out, bool val) {
43+
static void logcleaner_setCombat(bool val) {
5244
clear_combat = val;
5345
config.set_bool(CONFIG_CLEAR_COMBAT, clear_combat);
5446
}
5547

56-
static void logcleaner_setSparring(color_ostream& out, bool val) {
48+
static void logcleaner_setSparring(bool val) {
5749
clear_sparring = val;
5850
config.set_bool(CONFIG_CLEAR_SPARING, clear_sparring);
5951
}
6052

61-
static void logcleaner_setHunting(color_ostream& out, bool val) {
53+
static void logcleaner_setHunting(bool val) {
6254
clear_hunting = val;
6355
config.set_bool(CONFIG_CLEAR_HUNTING, clear_hunting);
6456
}
@@ -89,12 +81,6 @@ static command_result do_command(color_ostream& out, std::vector<std::string>& p
8981
return show_help ? CR_WRONG_USAGE : CR_OK;
9082
}
9183

92-
static void do_enable() {
93-
}
94-
95-
static void do_disable() {
96-
}
97-
9884
DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) {
9985
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode()) {
10086
out.printerr("Cannot enable {} without a loaded fort.\n", plugin_name);
@@ -103,31 +89,19 @@ DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) {
10389

10490
if (enable != is_enabled) {
10591
is_enabled = enable;
106-
DEBUG(control, out).print("{} from the API; persisting\n",
107-
is_enabled ? "enabled" : "disabled");
10892
config.set_bool(CONFIG_IS_ENABLED, is_enabled);
109-
if (enable)
110-
do_enable();
111-
else
112-
do_disable();
113-
} else {
114-
DEBUG(control, out).print("{} from the API, but already {}; no action\n",
115-
is_enabled ? "enabled" : "disabled",
116-
is_enabled ? "enabled" : "disabled");
11793
}
11894
return CR_OK;
11995
}
12096

12197
DFhackCExport command_result plugin_shutdown(color_ostream& out) {
122-
DEBUG(control, out).print("shutting down {}\n", plugin_name);
12398
return CR_OK;
12499
}
125100

126101
DFhackCExport command_result plugin_load_site_data(color_ostream& out) {
127102
config = World::GetPersistentSiteData(CONFIG_KEY);
128103

129104
if (!config.isValid()) {
130-
DEBUG(control, out).print("no config found in this save; initializing\n");
131105
config = World::AddPersistentSiteData(CONFIG_KEY);
132106
config.set_bool(CONFIG_IS_ENABLED, is_enabled);
133107
config.set_bool(CONFIG_CLEAR_COMBAT, clear_combat);
@@ -140,19 +114,12 @@ DFhackCExport command_result plugin_load_site_data(color_ostream& out) {
140114
clear_sparring = config.get_bool(CONFIG_CLEAR_SPARING);
141115
clear_hunting = config.get_bool(CONFIG_CLEAR_HUNTING);
142116

143-
DEBUG(control, out).print("loading persisted enabled state: {}\n",
144-
is_enabled ? "true" : "false");
145-
if (is_enabled)
146-
do_enable();
147-
148117
return CR_OK;
149118
}
150119

151120
DFhackCExport command_result plugin_onstatechange(color_ostream& out, state_change_event event) {
152121
if (event == DFHack::SC_WORLD_UNLOADED && is_enabled) {
153-
DEBUG(control, out).print("world unloaded; disabling {}\n", plugin_name);
154122
is_enabled = false;
155-
do_disable();
156123
}
157124
return CR_OK;
158125
}
@@ -163,31 +130,17 @@ static void cleanupLogs(color_ostream& out) {
163130

164131
// Collect all report IDs from unit combat/sparring/hunting logs
165132
std::unordered_set<int32_t> report_ids_to_remove;
133+
bool log_types[] = {clear_combat, clear_sparring, clear_hunting};
166134

167135
for (auto unit : world->units.all) {
168-
// Combat logs (index 0)
169-
if (clear_combat) {
170-
auto& log = unit->reports.log[0];
171-
for (auto report_id : log) {
172-
report_ids_to_remove.insert(report_id);
173-
}
174-
log.clear();
175-
}
176-
// Sparring logs (index 1)
177-
if (clear_sparring) {
178-
auto& log = unit->reports.log[1];
179-
for (auto report_id : log) {
180-
report_ids_to_remove.insert(report_id);
136+
for (int log_idx = 0; log_idx < 3; log_idx++) {
137+
if (log_types[log_idx]) {
138+
auto& log = unit->reports.log[log_idx];
139+
for (auto report_id : log) {
140+
report_ids_to_remove.insert(report_id);
141+
}
142+
log.clear();
181143
}
182-
log.clear();
183-
}
184-
// Hunting logs (index 2)
185-
if (clear_hunting) {
186-
auto& log = unit->reports.log[2];
187-
for (auto report_id : log) {
188-
report_ids_to_remove.insert(report_id);
189-
}
190-
log.clear();
191144
}
192145
}
193146

@@ -197,8 +150,6 @@ static void cleanupLogs(color_ostream& out) {
197150
// Remove collected reports from global buffers
198151
auto& reports = world->status.reports;
199152

200-
int reports_erased = 0;
201-
202153
for (auto report_id : report_ids_to_remove) {
203154
df::report* report = df::report::find(report_id);
204155
if (!report)
@@ -208,7 +159,6 @@ static void cleanupLogs(color_ostream& out) {
208159
if (it != reports.end()) {
209160
delete report;
210161
reports.erase(it);
211-
reports_erased++;
212162
}
213163
}
214164
}

plugins/lua/logcleaner.lua

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,51 @@ function parse_commandline(...)
1717
return true
1818
end
1919

20+
-- Handle enable/disable commands
21+
if command == 'enable' then
22+
if isEnabled() then
23+
print('logcleaner is already enabled')
24+
else
25+
dfhack.run_command('enable', 'logcleaner')
26+
print('logcleaner enabled')
27+
end
28+
return true
29+
end
30+
31+
if command == 'disable' then
32+
if not isEnabled() then
33+
print('logcleaner is already disabled')
34+
else
35+
dfhack.run_command('disable', 'logcleaner')
36+
print('logcleaner disabled')
37+
end
38+
return true
39+
end
40+
2041
-- Start with all disabled, enable only what's specified
2142
local new_combat, new_sparring, new_hunting = false, false, false
2243
local has_filter = false
2344

24-
for _, param in ipairs(args) do
25-
if param == 'all' then
26-
new_combat, new_sparring, new_hunting = true, true, true
27-
has_filter = true
28-
elseif param == 'none' then
29-
new_combat, new_sparring, new_hunting = false, false, false
30-
else
31-
-- Split by comma for multiple options in one parameter
32-
for token in param:gmatch('([^,]+)') do
33-
if token == 'combat' then
34-
new_combat = true
35-
has_filter = true
36-
elseif token == 'sparring' then
37-
new_sparring = true
38-
has_filter = true
39-
elseif token == 'hunting' then
40-
new_hunting = true
41-
has_filter = true
42-
else
43-
dfhack.printerr('Unknown option: ' .. token)
44-
return false
45-
end
45+
if command == 'all' then
46+
new_combat, new_sparring, new_hunting = true, true, true
47+
has_filter = true
48+
elseif command == 'none' then
49+
new_combat, new_sparring, new_hunting = false, false, false
50+
else
51+
-- Split by comma for multiple options in one parameter
52+
for token in command:gmatch('([^,]+)') do
53+
if token == 'combat' then
54+
new_combat = true
55+
has_filter = true
56+
elseif token == 'sparring' then
57+
new_sparring = true
58+
has_filter = true
59+
elseif token == 'hunting' then
60+
new_hunting = true
61+
has_filter = true
62+
else
63+
dfhack.printerr('Unknown option: ' .. token)
64+
return false
4665
end
4766
end
4867
end

0 commit comments

Comments
 (0)