-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Open
tal5
wants to merge
15
commits into
DenizenScript:master
Choose a base branch
from
tal5:format_scripts_to_core
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all 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
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
103 changes: 103 additions & 0 deletions
103
src/main/java/com/denizenscript/denizencore/scripts/ScriptFormattingContext.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,103 @@ | ||
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 ScriptFormattingContext(Map<String, 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<String> FORMAT_TYPES = new HashSet<>(); | ||
|
||
public static String registerFormatType(String name) { | ||
String nameLower = CoreUtilities.toLowerCase(name); | ||
if (!FORMAT_TYPES.add(nameLower)) { | ||
throw new IllegalArgumentException("Tried registering duplicate format type! format '" + name + "' already exists."); | ||
} | ||
return nameLower; | ||
} | ||
|
||
public static ScriptFormattingContext parseFromConfiguration(ScriptContainer script) { | ||
YamlConfiguration formatsConfig = script.getConfigurationSection("formats"); | ||
if (formatsConfig == null) { | ||
return null; | ||
} | ||
Map<String, ParseableTag> formats = new HashMap<>(); | ||
TagContext context = null; | ||
for (String formatType : FORMAT_TYPES) { | ||
String rawFormat = formatsConfig.getString(formatType); | ||
if (rawFormat != null) { | ||
if (context == null) { | ||
context = DenizenCore.implementation.getTagContext(script); | ||
} | ||
formats.put(formatType, TagManager.parseTextToTag(rawFormat, context)); | ||
continue; | ||
} | ||
String formatScriptInput = formatsConfig.getString(formatType + "_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 format '" + formatType + "'."); | ||
continue; | ||
} | ||
formats.put(formatType, formatScript.getFormatTag()); | ||
} | ||
if (formats.isEmpty()) { | ||
Debug.echoError(script, "Invalid formats config, must specify at least one valid format."); | ||
return null; | ||
} | ||
return new ScriptFormattingContext(formats, null); | ||
} | ||
|
||
public boolean hasFormat(String formatType) { | ||
return singleFormat != null || formats.containsKey(formatType); | ||
} | ||
|
||
public String formatOrNull(String formatType, String rawText, ScriptEntry entry) { | ||
ParseableTag formatTag = singleFormat == null ? formats.get(formatType) : singleFormat; | ||
return formatTag != null ? FormatScriptContainer.formatText(formatTag, rawText, entry.getContext(), entry.getScript()) : null; | ||
} | ||
|
||
public String format(String formatType, String rawText, ScriptEntry entry) { | ||
String formatted = formatOrNull(formatType, 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.
Uh oh!
There was an error while loading. Please reload this page.