Skip to content

Commit

Permalink
user logging done
Browse files Browse the repository at this point in the history
  • Loading branch information
dwasint committed Jun 10, 2024
1 parent 7595134 commit e57bcf3
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions code/__DEFINES/logging.dm
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
#define LOG_CATEGORY_ARTIFACT "artifact"
#define LOG_CATEGORY_BLACKMARKET "blackmarket"
#define LOG_CATEGORY_ANTAG_REP "antag-rep"
#define LOG_CATEGORY_BOMBING "bombing"

// Admin categories
#define LOG_CATEGORY_ADMIN "admin"
Expand Down
37 changes: 36 additions & 1 deletion code/__HELPERS/logging/_logging.dm
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ GLOBAL_LIST_INIT(testing_global_profiler, list("_PROFILE_NAME" = "Global"))
#define log_reftracker(msg)
#endif

/atom/proc/return_category(type)
switch(type)
if(LOG_ATTACK, LOG_VICTIM)
return LOG_CATEGORY_ATTACK
if(LOG_SAY, LOG_WHISPER, LOG_DSAY, LOG_CHAT, LOG_PDA)
return LOG_CATEGORY_GAME_SAY
if(LOG_EMOTE)
return LOG_CATEGORY_GAME_EMOTE
if(LOG_OOC)
return LOG_CATEGORY_GAME_OOC
if(LOG_ADMIN, LOG_ASAY, LOG_ADMIN_PRIVATE)
return LOG_CATEGORY_ADMIN
if(LOG_VIRUS)
return LOG_CATEGORY_VIRUS
if(LOG_GAME)
return LOG_CATEGORY_GAME
if(LOG_MECHA)
return LOG_CATEGORY_MECHA
if(LOG_MECHCOMP)
return LOG_CATEGORY_MECHCOMP
if(LOG_CLONING)
return LOG_CATEGORY_CLONING
if(LOG_ECON)
return LOG_CATEGORY_ECONOMY
else
return LOG_CATEGORY_GAME
/**
* Generic logging helper
*
Expand All @@ -95,11 +121,20 @@ GLOBAL_LIST_INIT(testing_global_profiler, list("_PROFILE_NAME" = "Global"))
* * color - color of the log text
* * log_globally - boolean checking whether or not we write this log to the log file
*/
/atom/proc/log_message(message, message_type, color = null, log_globally = TRUE)
/atom/proc/log_message(message, message_type, color = null, log_globally = TRUE, loki = TRUE, severity = "info", category)
if(!log_globally)
return

if(!category)
category = return_category(message_type)

var/log_text = "[key_name(src)] [message] [loc_name(src)]"

if(isliving(src) && loki)
var/mob/living/source = src
if(source.client)
SSloki.send_user_log(category, log_text, severity, source.key, null)

switch(message_type)
/// ship both attack logs and victim logs to the end of round attack.log just to ensure we don't lose information
if(LOG_ATTACK, LOG_VICTIM)
Expand Down
7 changes: 3 additions & 4 deletions code/__HELPERS/logging/attack.dm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* * atom/object - is a tool with which the action was made (usually an item)
* * addition - is any additional text, which will be appended to the rest of the log line
*/
/proc/log_combat(atom/user, atom/target, what_done, atom/object=null, addition=null)
/proc/log_combat(atom/user, atom/target, what_done, atom/object=null, addition=null, severity = "error")
var/ssource = key_name(user)
var/starget = key_name(target)

Expand All @@ -30,15 +30,14 @@
var/postfix = "[sobject][saddition][hp]"

var/message = "[what_done] [starget][postfix]"
user.log_message(message, LOG_ATTACK, color="red")
user.log_message(message, LOG_ATTACK, color="red", loki = FALSE)

if(isliving(user))
var/source_key = living_user.key
var/target_key = null
if(isliving(target))
target_key = living_target.key
if(source_key && living_user?.client.test_marked)
SSloki.send_user_log(LOG_CATEGORY_ATTACK, message, "warning", source_key, target_key)
SSloki.send_user_log(LOG_CATEGORY_ATTACK, message, severity, source_key, target_key)

if(user != target)
var/reverse_message = "was [what_done] by [ssource][postfix]"
Expand Down
2 changes: 1 addition & 1 deletion code/__HELPERS/logging/mob.dm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


/// Logs a message in a mob's individual log, and in the global logs as well if log_globally is true
/mob/log_message(message, message_type, color = null, log_globally = TRUE)
/mob/log_message(message, message_type, color = null, log_globally = TRUE, loki = TRUE, severity = "info", category)
if(!LAZYLEN(message))
stack_trace("Empty message")
return
Expand Down
14 changes: 13 additions & 1 deletion code/__HELPERS/~monkestation-helpers/logging/attack.dm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@
if(user)
if(HAS_TRAIT(user, TRAIT_PACIFISM))
bomb_message = "(while pacifist) [bomb_message]"
user.log_message(bomb_message, LOG_ATTACK) //let it go to individual logs as well as the game log
user.log_message(bomb_message, LOG_ATTACK, loki = FALSE) //let it go to individual logs as well as the game log
var/user_key
var/target_key

if(isliving(target))
var/mob/living/target_living = target
target_key = target_living.key

if(isliving(user))
var/mob/living/living_user = user
user_key = living_user.key

SSloki.send_user_log(LOG_CATEGORY_BOMBING, bomb_message, "critical", user_key, target_key)
bomb_message = "[key_name(user)] at [AREACOORD(user)] [bomb_message]."
else
log_attack(bomb_message)
Expand Down
2 changes: 1 addition & 1 deletion code/modules/mob/living/carbon/human/_species.dm
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ GLOBAL_LIST_EMPTY(features_by_species)
target.visible_message(span_danger("[user]'s [atk_verb] misses [target]!"), \
span_danger("You avoid [user]'s [atk_verb]!"), span_hear("You hear a swoosh!"), COMBAT_MESSAGE_RANGE, user)
to_chat(user, span_warning("Your [atk_verb] misses [target]!"))
log_combat(user, target, "attempted to punch")
log_combat(user, target, "attempted to punch", severity = "warning")
return FALSE

var/armor_block = target.run_armor_check(affecting, MELEE)
Expand Down
2 changes: 1 addition & 1 deletion code/modules/vehicles/mecha/equipment/mecha_equipment.dm
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
log_message("[src] removed from equipment.", LOG_MECHA)
chassis = null

/obj/item/mecha_parts/mecha_equipment/log_message(message, message_type=LOG_GAME, color=null, log_globally)
/obj/item/mecha_parts/mecha_equipment/log_message(message, message_type=LOG_GAME, color=null, log_globally, loki = TRUE, severity = "info", category)
if(chassis)
return chassis.log_message("ATTACHMENT: [src] [message]", message_type, color)
return ..()
Expand Down
15 changes: 8 additions & 7 deletions monkestation/code/modules/metrics/loki_integration/subsystem.dm
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ https://grafana.com/docs/loki/latest/reference/loki-http-api/#post-lokiapiv1push
*/

/client
var/test_marked = TRUE
var/test_marked = FALSE

/datum/config_entry/string/loki_url
default = ""

/datum/config_entry/flag/loki_enabled
default = TRUE
default = FALSE

SUBSYSTEM_DEF(loki)
name = "Loki Sender"
Expand All @@ -25,25 +25,26 @@ SUBSYSTEM_DEF(loki)
return SS_INIT_SUCCESS

/datum/controller/subsystem/loki/proc/send_server_log(category, message, severity)

var/time = rustg_unix_timestamp()
time = replacetext(time, ".", "")
time += "000"
var/built_json = "{\"streams\":\[{\"stream\":{\"level\":\"[severity]\",\"category\":\"[category]\", \"round\":\"[GLOB.round_id]\"},\"values\":\[\[\"[time]\", \"[message]\"\]\]}\]}"
push_data(built_json)

/datum/controller/subsystem/loki/proc/send_user_log(category, message, severity, source, target)
var/time = rustg_unix_timestamp()
time = replacetext(time, ".", "")
time += "000"
var/built_json = "{\"streams\":\[{\"stream\":{\"level\":\"[severity]\",\"target\":\"[target]\",\"source\":\"[source]\",\"category\":\"[category]\"},\"values\":\[\[\"[time]\", \"[message]\"\]\]}\]}"
var/built_json = "{\"streams\":\[{\"stream\":{\"level\":\"[severity]\",\"target\":\"[target]\",\"source\":\"[source]\",\"category\":\"[category]\", \"round\":\"[GLOB.round_id]\"},\"values\":\[\[\"[time]\", \"[message]\"\]\]}\]}"
push_data(built_json)

/datum/controller/subsystem/loki/proc/push_data(json)
if(!json || !CONFIG_GET(flag/loki_enabled))
message_admins("NOT ABLE TO SEND")
return
var/payload = json
message_admins(payload)
var/list/headers = list()
headers["Content-Type"] = "application/json"
var/datum/http_request/request = new()
//note about this, the loki_url also contains the api and userkey needed to actually send data if you are sending data outside of host box.
request.prepare(RUSTG_HTTP_METHOD_POST, "[CONFIG_GET(string/loki_url)]/loki/api/v1/push", payload, headers, "tmp/response.json")
request.begin_async()
message_admins("SENT LOG")

0 comments on commit e57bcf3

Please sign in to comment.