Skip to content

docs(native-examples): demonstrate EnableFileLogging in windows-monitor#1223

Merged
lotgon merged 2 commits into
masterfrom
feat/native-example-file-logging
Jul 3, 2026
Merged

docs(native-examples): demonstrate EnableFileLogging in windows-monitor#1223
lotgon merged 2 commits into
masterfrom
feat/native-example-file-logging

Conversation

@lotgon

@lotgon lotgon commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

What

Enable the collector's built-in file logger in the windows-monitor example:

  • collector.EnableFileLogging(log_dir, LogLevel::Info) before Start().
  • log_dir defaults to ./Logs, overridable via the HSM_LOG_DIR env var.
  • Header comment documents the NLog-parity output (DataCollector_<date>.txt + DataCollector_error_<date>.txt).

Why

#1221 added a built-in async file logger to the native collector for consumers that don't supply their own sink (e.g. the tt-aggregator native adapter). This wires it into the live windows-monitor example so the feature is demonstrated end-to-end in a runnable app — a run now streams to the server and writes the parity log files next to it.

Example-only; no library/production change. Both sinks (the existing stderr callback + the file logger) receive every message.

🤖 Generated with Claude Code

Enable the collector's built-in async file logger (#1221) in the windows-monitor example: call EnableFileLogging before Start so a run also writes the NLog-parity DataCollector_<date>.txt (+ _error_) files alongside the stderr callback. Directory defaults to ./Logs, overridable via HSM_LOG_DIR.

Exercises the collector's built-in file sink (the aggregator's logging feature) in a runnable example — distinct from the HSM agent, which supplies its own file logger via SetLogger. Example-only, no library change.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

PR Review: docs(native-examples): demonstrate EnableFileLogging in windows-monitor

Scope: Single file, src/native/collector/examples/windows-monitor/main.cpp (+12 / -1). This adds a demonstration of the already-existing Collector::EnableFileLogging API to the Windows monitor example, plus an HSM_LOG_DIR env override and an updated status line.

Overall this is a small, low-risk, well-commented change. I verified the API it calls (EnableFileLogging), the LogLevel enum, and the C ABI level values against the source. No bugs, security issues, or performance concerns. One minor documentation-accuracy nit below.


Minor: "all levels" / "both sinks receive every message" is inaccurate for Debug

main.cpp:50 enables the file logger with hc::LogLevel::Info:

collector.EnableFileLogging(log_dir, hc::LogLevel::Info);

The LogLevel enum (enums.hpp / hsm_collector.h) is ordered Debug = 0 < Info = 1 < Error = 2, and the file sink filters to >= min_level. So with Info, any Debug-level messages are not written to the file. Meanwhile the SetLogger stderr callback (main.cpp:43) has no threshold and receives every message. That makes two of the newly-added comments slightly misleading:

  • main.cpp:8 — "DataCollector_<date>.txt for all levels" — really it's all levels at or above Info; Debug is excluded.
  • main.cpp:47-49 — "both sinks receive every message" — the file sink applies the Info threshold, so Debug messages reach only the stderr callback, not the file.

This is purely a comment/expectation mismatch (the code behaves correctly). Suggested fixes, either:

  • pass hc::LogLevel::Debug to genuinely capture "all levels" and make both sinks equivalent, or
  • reword the comments to say "levels >= Info" so the example doesn't imply Debug lands in the file.

Things that look good

  • HSM_LOG_DIR handling (main.cpp:28-29) correctly guards against both a missing var and an empty string (log_dir_env && *log_dir_env) before falling back to "Logs".
  • EnableFileLogging is called before Start(), matching the API's documented ordering requirement.
  • The relative "Logs" default and directory-clearing-on-empty semantics match the underlying implementation.
  • Status-line change is a harmless, useful addition.

Recommendation: Approve after (optionally) tightening the "all levels" / "every message" wording, or switching the min level to Debug.

The HSM_LOG_DIR std::getenv lookup tripped C4996 (getenv deprecation) under the http-transport lane's /WX, failing the Windows build. Define _CRT_SECURE_NO_WARNINGS before the CRT headers — the lookup is a read-only env read.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

PR #1223 Review — feat/native-example-file-logging

Scope: A single-file change to the windows-monitor example (src/native/collector/examples/windows-monitor/main.cpp, +16/-1) that enables the built-in async file logger and lets the output directory be overridden via HSM_LOG_DIR.

Overall this is a clean, well-commented, low-risk example change. The EnableFileLogging(dir, LogLevel::Info) call matches the public API in collector.hpp:249, the directory is auto-created by FileLogger (hsm_collector.cpp:784), and the HSM_LOG_DIR fallback logic is correct. I found no bugs, security issues, or performance concerns. A couple of minor documentation nits below.

Correctness ✅

  • std::getenv is used correctly and guarded (log_dir_env && *log_dir_env), so an unset or empty HSM_LOG_DIR both fall back to "Logs". Good.
  • <cstdlib> is already included (line 18), so std::getenv is properly declared.
  • #define _CRT_SECURE_NO_WARNINGS is placed before the first #include (lines 13/15), which is the correct location for suppressing MSVC's C4996 on getenv.
  • EnableFileLogging is called before Start(), as the API doc requires.

Nits (non-blocking)

  1. Inline comment slightly overstates behavior (lines 52–53). The comment says the file logger and the stderr callback are "independent... both sinks receive every message." But the file sink filters by min_level (here LogLevel::Info), whereas the SetLogger stderr callback is unfiltered. So Trace/Debug messages would reach stderr but not the file. This matches the header doc (collector.hpp:246, "levels >= min_level"), so the comment is just imprecise — consider "both sinks receive every message at Info and above" or similar.

  2. _CRT_SECURE_NO_WARNINGS is a broad hammer for one getenv call. It's a fine, documented choice for an example. If you wanted to keep the example strictly warning-clean without the global macro, _dupenv_s (MSVC) is the alternative — but that's more ceremony than an example warrants, so the current approach is reasonable.

Security / Performance

Nothing of concern. HSM_LOG_DIR is read once at startup and passed to create_directories; for a local demo binary there's no injection or traversal surface worth mitigating.

Verdict: LGTM. The nits are optional polish, not blockers.

@lotgon
lotgon merged commit 01abb13 into master Jul 3, 2026
20 checks passed
@lotgon
lotgon deleted the feat/native-example-file-logging branch July 3, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant