-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
add syslog function to wled #4664
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
KrX3D
wants to merge
19
commits into
wled:main
Choose a base branch
from
KrX3D:syslog
base: main
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
19 commits
Select commit
Hold shift + click to select a range
25c4c5b
add syslog function to wled
KrX3D afccadd
Update wled00/syslog.cpp
KrX3D a48b05e
added coderabbitai suggestions like:
KrX3D 070b6b2
inject html to settings_sync.htm only when syslog enabled to save siz…
KrX3D cbee84e
coderabbitai improvements
KrX3D 4c9b735
hide feature was removed since it no longer needed
KrX3D 3007dcf
more coderabbitai improvements
KrX3D 14910fc
more coderabbitai improvements
KrX3D 0f50d28
more coderabbitai improvements
KrX3D f72815d
removed configuration options that do not add value
KrX3D ad5d0ab
doSerializeConfig -> configNeedsWrite
KrX3D e56abf2
remvoed Facility, Severity from GUI
KrX3D dada672
- Fix ompiling porblem with esp32_dev board
KrX3D 056ac1c
moved strings to flash - suggestion by DjordjeMandic
KrX3D 11f5dd2
removed (commented out) not used features - request netmindz
KrX3D 51bd04a
added comments by coderabbitai request
KrX3D a77b85a
changes suggested by netmindz
KrX3D 238e1c4
syslog inject fix / coderabbit improments
KrX3D 77c8f6d
removed forgotten commented out code
KrX3D 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# pio-scripts/inject_syslog_ui.py | ||
|
||
""" | ||
PlatformIO build script to conditionally inject Syslog UI elements into the settings HTML file. | ||
|
||
This script: | ||
1. Injects Syslog UI elements when WLED_ENABLE_SYSLOG is defined in build flags | ||
2. Restores the original HTML file after build completion | ||
3. Tracks state between builds to force UI rebuilds when necessary | ||
""" | ||
|
||
import os, re, shutil | ||
from SCons.Script import Import | ||
|
||
Import("env") | ||
|
||
# detect full vs. partial compile | ||
is_full_build = env.get("PIOENV") is not None | ||
|
||
# Track the state between builds | ||
def get_previous_syslog_state(project_dir): | ||
state_file = os.path.join(project_dir, "wled00/data/.syslog_state") | ||
if os.path.exists(state_file): | ||
with open(state_file, 'r') as f: | ||
return f.read().strip() == "1" | ||
return None # None means no previous state recorded | ||
|
||
def set_syslog_state(project_dir, enabled): | ||
state_file = os.path.join(project_dir, "wled00/data/.syslog_state") | ||
with open(state_file, 'w') as f: | ||
f.write("1" if enabled else "0") | ||
|
||
# This is the HTML we want to inject | ||
SYSLOG_HTML = """<h3>Syslog</h3> | ||
<div id="Syslog"> | ||
Enable Syslog: <input type="checkbox" name="SL_en"><br> | ||
Host: <input type="text" name="SL_host" maxlength="32"><br> | ||
Port: <input type="number" name="SL_port" min="1" max="65535" value="%SL_port%"><br> | ||
</div>""" | ||
|
||
def inject_syslog_ui(source, target, env, retry_count=0): | ||
print("\033[44m==== inject_syslog_ui.py (PRE BUILD) ====\033[0m") | ||
if not is_full_build: | ||
print("\033[43mNot a full build, skipping Syslog UI operations.\033[0m") | ||
return | ||
|
||
# Check for the define in BUILD_FLAGS | ||
build_flags = env.get("BUILD_FLAGS", "") | ||
if isinstance(build_flags, list): | ||
build_flags = " ".join(build_flags) | ||
has_syslog = bool(re.search(r'-D\s*WLED_ENABLE_SYSLOG\b', build_flags)) | ||
|
||
project_dir = env.subst("$PROJECT_DIR") | ||
html_path = os.path.join(project_dir, "wled00/data/settings_sync.htm") | ||
bak = html_path + ".backup" | ||
|
||
# Detect state change → touch to force rebuild | ||
prev = get_previous_syslog_state(project_dir) | ||
if prev is not None and prev != has_syslog: | ||
print(f"\033[43mSYSLOG state changed from {prev} to {has_syslog}, forcing UI rebuild.\033[0m") | ||
if os.path.exists(html_path): | ||
with open(html_path, 'a'): | ||
os.utime(html_path, None) | ||
|
||
set_syslog_state(project_dir, has_syslog) | ||
|
||
if not has_syslog: | ||
print("\033[43mWLED_ENABLE_SYSLOG not defined, skipping injection.\033[0m") | ||
# restore if backup exists | ||
if os.path.exists(bak): | ||
print("Restoring original file from backup...") | ||
shutil.copy2(bak, html_path) | ||
os.remove(bak) | ||
return | ||
|
||
# backup + inject only once | ||
if not os.path.exists(bak): | ||
print("Backing up and injecting Syslog UI...") | ||
shutil.copyfile(html_path, bak) | ||
try: | ||
with open(html_path, 'r', encoding='utf8') as f: | ||
original = f.read() | ||
modified = original | ||
|
||
# replace the single comment with HTML | ||
if '<!-- SYSLOG-INJECT -->' in modified: | ||
modified = modified.replace('<!-- SYSLOG-INJECT -->', SYSLOG_HTML) | ||
else: | ||
# insert before last <hr> | ||
idx = modified.rfind('<hr>') | ||
if idx == -1: | ||
print("\033[41mCould not find <hr> to insert Syslog UI!\033[0m") | ||
# Clean up backup since injection failed | ||
if os.path.exists(bak): | ||
os.remove(bak) | ||
return | ||
modified = ( | ||
modified[:idx] | ||
+ SYSLOG_HTML + '\n' | ||
+ modified[idx:] | ||
) | ||
|
||
with open(html_path, 'w', encoding='utf8') as f: | ||
f.write(modified) | ||
print("\033[42mSyslog UI injected successfully!\033[0m") | ||
|
||
except (IOError, OSError) as e: | ||
print(f"\033[41mFile operation error during injection: {e}\033[0m") | ||
# injection failed → remove backup so we'll retry next time | ||
if os.path.exists(bak): | ||
os.remove(bak) | ||
except Exception as e: | ||
print(f"\033[41mUnexpected error during injection: {e}\033[0m") | ||
# injection failed → remove backup so we’ll retry next time | ||
if os.path.exists(bak): | ||
os.remove(bak) | ||
else: | ||
print("Backup exists; assume already injected.") | ||
# verify that SYSLOG markers really are in the file | ||
with open(html_path, 'r', encoding='utf8') as f: | ||
content = f.read() | ||
if '<h3>Syslog</h3>' not in content: | ||
print("Backup exists but SYSLOG markers missing—forcing re-injection.") | ||
os.remove(bak) | ||
# only retry up to 3 times | ||
if retry_count < 3: | ||
# Add a small delay before retrying | ||
import time | ||
time.sleep(0.5 * (retry_count + 1)) # Increasing delay with each retry | ||
inject_syslog_ui(source, target, env, retry_count + 1) | ||
else: | ||
print("\033[41mToo many retry attempts. Manual intervention required.\033[0m") | ||
else: | ||
print("Backup exists and markers found; already injected.") | ||
|
||
def restore_syslog_ui(source, target, env): | ||
print("\033[44m==== inject_syslog_ui.py (POST BUILD) ====\033[0m") | ||
project_dir = env.subst("$PROJECT_DIR") | ||
html_path = os.path.join(project_dir, "wled00/data/settings_sync.htm") | ||
bak = html_path + ".backup" | ||
|
||
# restore only if backup file is present | ||
if os.path.exists(bak): | ||
print("Restoring original file from backup...") | ||
shutil.copy2(bak, html_path) | ||
os.remove(bak) | ||
|
||
# always register the post-action on checkprogsize so it runs every build | ||
env.AddPostAction("checkprogsize", restore_syslog_ui) | ||
|
||
# only inject on full build | ||
if is_full_build: | ||
inject_syslog_ui(None, None, env) |
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
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.
Now the html is so much smaller, i wonder if we really need the complication of this selective injection or if we should follow the pattern used for optional settings that is used for DMX Input support
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.
you mean move the html part to the settings_sync.htm with "hide function" and remove the injection py script?
like i had it in the begining and had to change it because it adds size?