Add logging for manually changing your targeted zone (#72814) (#20787)

* Add logging for manually changing your targeted zone (#72814)

See title.
Surgery hud is exempt from this.

Requested by @Mothblocks

Signed-off-by: GitHub <noreply@github.com>
Co-authored-by: Mothblocks <35135081+Mothblocks@users.noreply.github.com>
Co-authored-by: dragomagol <66640614+dragomagol@users.noreply.github.com>

* Delete 

---------

Signed-off-by: GitHub <noreply@github.com>
Co-authored-by: Zephyr <12817816+ZephyrTFA@users.noreply.github.com>
Co-authored-by: Mothblocks <35135081+Mothblocks@users.noreply.github.com>
Co-authored-by: dragomagol <66640614+dragomagol@users.noreply.github.com>
This commit is contained in:
Tom
2023-04-26 10:44:01 -07:00
committed by GitHub
co-authored by Mothblocks dragomagol Zephyr
parent 901cc89d02
commit 6435018ba7
17 changed files with 232 additions and 78 deletions
@@ -0,0 +1,3 @@
/datum/log_category/target_zone_switch
category = LOG_CATEGORY_TARGET_ZONE_SWITCH
config_flag = /datum/config_entry/flag/log_zone_switch
+32
View File
@@ -0,0 +1,32 @@
/// The main datum that contains all log entries for a category
/datum/log_category
/// The category this datum contains
var/category
/// If set this config flag is checked to enable this log category
var/config_flag
/// List of all entries, in chronological order of when they were added
var/list/entries = list()
/// Backup log category to catch attempts to log to a category that doesn't exist
/datum/log_category/backup_category_not_found
category = LOG_CATEGORY_NOT_FOUND
/// Add an entry to this category. It is very important that any data you provide doesn't hold references to anything!
/datum/log_category/proc/add_entry(message, list/data)
var/list/entry = list(
LOG_ENTRY_MESSAGE = message,
LOG_ENTRY_TIMESTAMP = big_number_to_text(rustg_unix_timestamp()),
)
if(data)
entry[LOG_ENTRY_DATA] = data
entries += list(entry)
write_entry(entry)
/// Allows for category specific file splitting. Needs to accept a null entry for the default file.
/datum/log_category/proc/get_output_file(list/entry)
return "[GLOB.log_directory]/[category].json"
/// Writes an entry to the output file for the category
/datum/log_category/proc/write_entry(list/entry)
rustg_file_append("[json_encode(entry)]\n", get_output_file(entry))
+70
View File
@@ -0,0 +1,70 @@
GLOBAL_DATUM_INIT(logger, /datum/log_holder, new)
GLOBAL_PROTECT(logger)
/**
* Main datum to manage logging actions
*/
/datum/log_holder
/// Round ID, if set, that logging is initialized for
var/round_id
/// When the log_holder first initialized
var/logging_start_timestamp
/// Associative: category -> datum
var/list/datum/log_category/log_categories
/// typecache list for categories that exist but are disabled
var/list/disabled_categories
var/initialized = FALSE
var/shutdown = FALSE
/// Assembles basic information for logging, creating the log category datums and checking for config flags as required
/datum/log_holder/proc/init_logging()
if(initialized)
CRASH("Attempted to call init_logging twice!")
initialized = TRUE
round_id = GLOB.round_id
logging_start_timestamp = rustg_unix_timestamp()
log_categories = list()
disabled_categories = list()
for(var/datum/log_category/category_type as anything in subtypesof(/datum/log_category))
var/category = initial(category_type.category)
if(!category)
continue
if(category in log_categories)
stack_trace("Found two identical log category type definitions! [category_type]")
continue
var/config_flag = initial(category_type.config_flag)
if(config_flag && !config.Get(config_flag))
disabled_categories[category] = TRUE
continue
category_type = log_categories[category] = new category_type
var/list/log_start_entry = list(
LOG_HEADER_CATEGORY = category,
LOG_HEADER_INIT_TIMESTAMP = big_number_to_text(logging_start_timestamp),
LOG_HEADER_ROUND_ID = big_number_to_text(GLOB.round_id),
)
rustg_file_write("[json_encode(log_start_entry)]\n", category_type.get_output_file(null))
/// Tells the log_holder to not allow any more logging to be done, and dumps all categories to their json file
/datum/log_holder/proc/shutdown_logging()
if(shutdown)
CRASH("Attempted to call shutdown_logging twice!")
shutdown = TRUE
/// This is Log because log is a byond internal proc
/datum/log_holder/proc/Log(category, message, list/data)
if(!initialized || shutdown)
CRASH("Attempted to perform logging before initializion or after shutdown!")
if(disabled_categories[category])
return
var/datum/log_category/log_category = log_categories[category]
if(!log_category)
Log(LOG_CATEGORY_NOT_FOUND, message, data)
CRASH("Attempted to log to a category that doesn't exist! [category]")
log_category.add_entry(message, data)
+43
View File
@@ -477,3 +477,46 @@
else if(iscarbon(mob) || isAI(mob) || isbrain(mob))
divided_health = abs(HEALTH_THRESHOLD_DEAD - mob.health) / abs(HEALTH_THRESHOLD_DEAD - mob.maxHealth)
return divided_health * 100
/**
* Generates a log message when a user manually changes their targeted zone.
* Only need to one of new_target or old_target, and the other will be auto populated with the current selected zone.
*/
/mob/proc/log_manual_zone_selected_update(source, new_target, old_target)
if(!new_target && !old_target)
CRASH("Called log_manual_zone_selected_update without specifying a new or old target")
old_target ||= zone_selected
new_target ||= zone_selected
if(old_target == new_target)
return
var/list/data = list(
"new_target" = new_target,
"old_target" = old_target,
)
if(mind?.assigned_role)
data["assigned_role"] = mind.assigned_role.title
if(job)
data["assigned_job"] = job
var/atom/handitem = get_active_held_item()
if(handitem)
data["active_item"] = list(
"type" = handitem.type,
"name" = handitem.name,
)
var/atom/offhand = get_inactive_held_item()
if(offhand)
data["offhand_item"] = list(
"type" = offhand.type,
"name" = offhand.name,
)
GLOB.logger.Log(
LOG_CATEGORY_TARGET_ZONE_SWITCH,
"[key_name(src)] manually changed selected zone",
data,
)