Skip to content

Latest commit

 

History

History
137 lines (116 loc) · 5.86 KB

configuration-loading.md

File metadata and controls

137 lines (116 loc) · 5.86 KB

Configuration loading

LogIt++ configuration is split into explicit phases:

JSON value or text
    -> ConfigurationDocument
    -> validation and materialization
    -> programmatic binding resolution
    -> runtime preparation (ConfigurationPreparedSet)
    -> atomic append-only registration (Logger)

The core semantic model and runtime layers do not depend on a JSON library. The JSON frontend is optional and uses nlohmann::json only when LOGIT_WITH_JSON_CONFIG=ON is selected.

Enabling the JSON frontend

Install nlohmann_json or allow the source-tree fallback:

cmake -S . -B build \
  -DLOGIT_WITH_JSON_CONFIG=ON \
  -DLOGIT_USE_SUBMODULES=ON

Installed packages configured with this option use find_dependency(nlohmann_json 3.11 CONFIG). Installing a build that uses the bundled fallback is intentionally rejected; install nlohmann_json separately for an exportable package.

Parsing

With the option enabled, <logit.hpp> exposes overloads for JSON text and an already parsed nlohmann::json value:

#include <logit.hpp>

const std::string text = R"json({
  "schema_version": 1,
  "loggers": [{
    "id": "application-file",
    "type": "file",
    "strategy": {
      "level": "info",
      "formatter": {"pattern": "%v", "json": false}
    },
    "backend": {
      "directory": "logs",
      "async": true,
      "use_dedicated_executor": true,
      "queue_capacity": 8192,
      "queue_policy": "block"
    }
  }]
})json";

logit::ConfigurationDocument document;
logit::ConfigurationDiagnostics diagnostics;
if (!logit::parse_configuration_json(text, document, diagnostics)) {
    // Inspect diagnostics.items().
}

Parsing has no logger side effects and replaces document only after a complete successful conversion. The frontend reports JSON syntax and shape/conversion errors with path-aware diagnostics and uses configuration_parse_failed for syntax errors. Backend schema validation is a later core phase: backend unknown fields, backend types, enum/named values, defaults, and feature availability may be diagnosed by materialize_configuration() rather than by the JSON conversion itself. JSON floating-point values are rejected because schema v1 has only signed and unsigned integer numeric values.

Call materialize_configuration() and instantiate_configuration() explicitly for the later phases. The frontend does not read files, apply frontend-specific defaults, resolve callbacks, or construct loggers. instantiate_configuration() is the compatibility backend-instantiation API: the factory remains responsible for the logger's level and accepts the materialized formatter fields without constructing a formatter. Use prepare_configuration() when the schema strategy must be applied atomically; it constructs the formatter and assigns the explicit level or the TRACE default. Properties input and hot reload remain outside the current implementation.

Runtime preparation is available through prepare_configuration(). It returns a ConfigurationPreparedSet containing each stable logger ID, the concrete ILogger, its ILogFormatter, and the applied LogLevel. Omitted levels use TRACE; schema-v1 formatters use simple with the public SimpleLogFormatter::Config defaults, then apply explicit pattern and JSON overrides. The prepared result uses shared ownership to match the existing factory API. Preparation does not mutate the global singleton incrementally.

Atomic registration

Logger::register_configuration() appends the complete prepared batch with one dispatch snapshot publication. Existing indices and strategy flags are preserved:

logit::ConfigurationPreparedSet prepared; // Successful prepare_configuration() output.
logit::ConfigurationRegistrationOptions options;
// Optional per-ID overrides; unknown IDs are errors.
options["application-file"].enabled = true;
options["application-file"].single_mode = false;
std::shared_ptr<const logit::ConfigurationRegistrationResult> registered;
if (logit::Logger::get_instance().register_configuration(
        prepared, options, registered, diagnostics)) {
    const int file_index = registered->indices.at("application-file");
    logit::Logger::get_instance().set_logger_enabled(file_index, true);
}

The caller supplies a schema-v1 prepared set; the default-constructed set in this snippet must first be populated by a successful preparation call. The operation validates manually constructed sets too: IDs, non-null pairs, level metadata, routing-option IDs, object aliases, and index capacity. Levels and formatters are not reapplied during registration. Repeated IDs fail; this is not replacement, hot reload, or idempotent re-registration. Valid empty batches return an empty mapping without publishing a snapshot. Registry count is capped at INT_MAX, so the last supported index is INT_MAX - 1.

Failures preserve the prepared input, live registry, and previous result handle; diagnostics are replaced on each call. Allocation exceptions may propagate only before commit, with the same preservation guarantee. The complete result is prepared before commit and handed off without allocation. A race with shutdown either commits the whole batch for shutdown to observe, or fails unchanged.

The same complete backend/formatter object cannot appear in different strategies, including legacy entries and earlier batches, regardless of concurrency capability. A combined backend/formatter in one strategy requires shared ownership of one control block. Distinct objects sharing a control block are allowed. Retained prepared-set pointers preserve lifetime, not permission for unsynchronized direct invocation, mutation, or shutdown. Destroying the prepared set does not remove registered entries. Logger::add_logger(unique_ptr, unique_ptr, bool) remains available with its existing behavior and now also prepares its snapshot before mutating the registry. No singleton reset/restart API is introduced.