-
-
Notifications
You must be signed in to change notification settings - Fork 38
Improve script logging #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mcmonkey4eva
merged 15 commits into
DenizenScript:master
from
tal5:format_scripts_to_core
Aug 24, 2025
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ff4fa49
Initial copy over
tal5 13db577
Make it work in core & add impl method
tal5 d95f511
Cache format tag
tal5 2418b42
`debug` command: update to modern switch
tal5 a8dd853
logging contexts POC
tal5 d33e3a9
Fix max arg count
tal5 35dd4e6
Initial review fixes
tal5 1ab1c3c
fix `name` arg nullability
tal5 e5e203c
Move deprecation to core
tal5 7e53457
The vision™
tal5 3da6404
Separate formatted debug
tal5 29a69bc
Remove `Key`, consistentify naming
tal5 f10e8f9
Move `ElementTag.format` to core
tal5 b7aebcc
It's not necessarily logging specific
tal5 18bbe45
Get context directly from entry
tal5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/com/denizenscript/denizencore/scripts/ScriptLoggingContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package com.denizenscript.denizencore.scripts; | ||
|
|
||
| import com.denizenscript.denizencore.DenizenCore; | ||
| import com.denizenscript.denizencore.scripts.containers.ScriptContainer; | ||
| import com.denizenscript.denizencore.scripts.containers.core.FormatScriptContainer; | ||
| import com.denizenscript.denizencore.tags.ParseableTag; | ||
| import com.denizenscript.denizencore.tags.TagContext; | ||
| import com.denizenscript.denizencore.tags.TagManager; | ||
| import com.denizenscript.denizencore.utilities.CoreUtilities; | ||
| import com.denizenscript.denizencore.utilities.YamlConfiguration; | ||
| import com.denizenscript.denizencore.utilities.debugging.Debug; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public record ScriptLoggingContext(Map<Key, ParseableTag> formats, ParseableTag singleFormat) { | ||
|
|
||
| // <--[language] | ||
| // @name Script Logging Format | ||
| // @group Script Container System | ||
| // @description | ||
| // Script logging contexts provide the format certain commands will use for their texts. Most notably, this includes <@link command debug>. | ||
| // See specific command's documentation for information on which formats they use (for example, the 'debug' command supports a 'debug' format and an 'error' format). | ||
| // The formats are specified under a 'formats' key, and can be either a <@link language Format Script Containers> or a direct format with the same syntax as format scripts. | ||
| // When specifying a direct format, use the format name as the key; When specifying a format script, use '<format>_script' as the key (see example below). | ||
| // <code> | ||
| // my_project_task: | ||
| // type: task | ||
| // formats: | ||
| // # A direct format | ||
| // debug: [MyProject] <[text]> | ||
| // # A separate format script | ||
| // error_script: my_project_error | ||
| // script: | ||
| // - if <util.real_time_since_start.in_hours> > 20: | ||
| // # Will be formatted by the 'my_project_error' format script. | ||
| // - debug error "The system has been running for over 20 hours! Please restart!" | ||
| // - else: | ||
| // # Will print "[MyProject]: The system does not need a restart yet." | ||
| // - debug "The system does not need a restart yet." | ||
| // </code> | ||
| // --> | ||
|
|
||
| public static final Set<Key> DEBUG_TYPES = new HashSet<>(); | ||
|
|
||
| public record Key(String key) { | ||
|
mcmonkey4eva marked this conversation as resolved.
Outdated
|
||
| public Key(String key) { | ||
| this.key = CoreUtilities.toLowerCase(key); | ||
| } | ||
| } | ||
|
|
||
| public static Key registerFormatType(String name) { | ||
| Key key = new Key(name); | ||
| if (!DEBUG_TYPES.add(key)) { | ||
| throw new IllegalArgumentException("Tried registering duplicate format type! format '" + name + "' already exists."); | ||
| } | ||
| return key; | ||
| } | ||
|
|
||
| public static ScriptLoggingContext parseFromConfiguration(ScriptContainer script) { | ||
| YamlConfiguration formatsConfig = script.getConfigurationSection("formats"); | ||
| if (formatsConfig == null) { | ||
| return null; | ||
| } | ||
| Map<Key, ParseableTag> formats = new HashMap<>(); | ||
| TagContext context = null; | ||
| for (Key formatType : DEBUG_TYPES) { | ||
| String rawFormat = formatsConfig.getString(formatType.key); | ||
| if (rawFormat != null) { | ||
| if (context == null) { | ||
| context = DenizenCore.implementation.getTagContext(script); | ||
| } | ||
| formats.put(formatType, TagManager.parseTextToTag(rawFormat, context)); | ||
| continue; | ||
| } | ||
| String formatScriptInput = formatsConfig.getString(formatType.key + "_script"); | ||
| if (formatScriptInput == null) { | ||
| continue; | ||
| } | ||
| FormatScriptContainer formatScript = ScriptRegistry.getScriptContainerAs(formatScriptInput, FormatScriptContainer.class); | ||
| if (formatScript == null || formatScript.getFormatTag() == null) { | ||
| Debug.echoError(script, "Invalid format script '" + formatScriptInput + "' specified for debug format '" + formatType.key + "'."); | ||
| continue; | ||
| } | ||
| formats.put(formatType, formatScript.getFormatTag()); | ||
| } | ||
| if (formats.isEmpty()) { | ||
| Debug.echoError(script, "Invalid logging config, must specify at least one valid debug format."); | ||
| return null; | ||
| } | ||
| return new ScriptLoggingContext(formats, null); | ||
| } | ||
|
|
||
| public boolean hasFormat(Key debugType) { | ||
| return singleFormat != null || formats.containsKey(debugType); | ||
| } | ||
|
|
||
| public String formatOrNull(Key debugType, String rawText, ScriptEntry entry) { | ||
| ParseableTag formatTag = singleFormat == null ? formats.get(debugType) : singleFormat; | ||
| return formatTag != null ? FormatScriptContainer.formatText(formatTag, rawText, entry.getContext(), entry.getScript()) : null; | ||
| } | ||
|
|
||
| public String format(Key debugType, String rawText, ScriptEntry entry) { | ||
| String formatted = formatOrNull(debugType, rawText, entry); | ||
| return formatted != null ? formatted : rawText; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this a record
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No specific reason, records are just an easy "Hey java, give me an object with these fields and a ready constructor and
equalsand everything" - afaics making this a class would just be more lines of code, no? as it never needs to be modified anyway