108#ifndef SENZING_SDK_SZLOGGING_HPP
109#define SENZING_SDK_SZLOGGING_HPP
122#include <string_view>
238 constexpr std::string_view kSpace =
" \t\n\r\f\v";
239 const auto first = text.find_first_not_of(kSpace);
240 if (first == std::string_view::npos) {
243 const auto last = text.find_last_not_of(kSpace);
244 std::string out{text.substr(first, last - first + 1)};
245 std::transform(out.begin(), out.end(), out.begin(), [](
char c) {
246 return (c >=
'a' && c <=
'z') ? static_cast<char>(c -
'a' +
'A') : c;
258 if (name ==
"WARNING") {
261 const auto* match = std::find_if(
263 [&name](
SzLogLevel level) { return name == ToString(level); });
267 throw SzBadInputException(
"unknown log level \"" + std::string{text} +
276 if (name ==
"JSON") {
279 const auto* match = std::find_if(
281 [&name](
SzLogFormat format) { return name == ToString(format); });
285 throw SzBadInputException(
"unknown log format \"" + std::string{text} +
338 std::filesystem::path filePath) {
346 [[nodiscard]]
const std::filesystem::path&
Path() const noexcept {
351 [[nodiscard]]
bool IsAppend() const noexcept {
return append_; }
358 : type_(outputType), path_(std::move(filePath)), append_(appendMode) {}
361 std::filesystem::path path_;
382 : minLevel_(minLevel) {}
387 : minLevel_(minLevel), format_(format) {}
429 std::optional<std::chrono::system_clock::time_point>
Timestamp;
456 SzLogLevel level, std::string_view rawModule, std::string_view message,
457 std::optional<int64_t> timestampNs = std::nullopt,
458 std::optional<uint64_t> threadID = std::nullopt) noexcept
460 rawModule_(rawModule),
462 timestampNs_(timestampNs),
463 threadID_(threadID) {}
470 [[nodiscard]]
constexpr std::string_view
Module() const noexcept {
471 const auto colon = rawModule_.rfind(
':');
472 return colon == std::string_view::npos ? rawModule_
473 : rawModule_.substr(0, colon);
479 [[nodiscard]]
constexpr std::optional<std::string_view>
ThreadHash()
481 const auto colon = rawModule_.rfind(
':');
482 if (colon == std::string_view::npos) {
485 return rawModule_.substr(colon + 1);
489 [[nodiscard]]
constexpr std::string_view
Message() const noexcept {
494 [[nodiscard]]
constexpr std::string_view
RawModule() const noexcept {
507 [[nodiscard]] std::optional<std::chrono::system_clock::time_point>
512 return std::chrono::system_clock::time_point{
513 std::chrono::duration_cast<std::chrono::system_clock::duration>(
514 std::chrono::nanoseconds{*timestampNs_})};
519 [[nodiscard]]
constexpr std::optional<uint64_t>
ThreadID() const noexcept {
526 entry.
Level = level_;
531 entry.
Message = std::string{message_};
539 std::string_view rawModule_;
540 std::string_view message_;
541 std::optional<int64_t> timestampNs_;
542 std::optional<uint64_t> threadID_;
623 std::shared_ptr<Buffer> buffer) noexcept
624 : registration_(std::move(registration)), buffer_(std::move(buffer)) {}
628 [[nodiscard]] std::vector<SzLogEntry>
Finish() {
629 registration_.reset();
633 std::lock_guard<std::mutex> guard(buffer_->Mutex);
634 return std::exchange(buffer_->Entries, {});
638 std::unique_ptr<SzLogRegistration> registration_;
639 std::shared_ptr<Buffer> buffer_;
680 [[nodiscard]]
virtual std::unique_ptr<SzLogRegistration>
763 auto buffer = std::make_shared<SzLogCapture::Buffer>();
765 options, [sink = buffer](
const SzLogRecord& record) {
766 std::lock_guard<std::mutex> guard(sink->Mutex);
767 sink->Entries.push_back(record.
ToEntry());
769 return SzLogCapture{std::move(registration), std::move(buffer)};
Registration parameters: the callback's minimum level and the format native applies to SzLogRecord::M...
Definition SzLogging.hpp:377
constexpr SzLogFormat Format() const noexcept
Format native applies to the message.
Definition SzLogging.hpp:401
constexpr SzLogCallbackOptions WithFormat(SzLogFormat format) const noexcept
Returns a copy of these options with format instead.
Definition SzLogging.hpp:390
constexpr SzLogCallbackOptions(SzLogLevel minLevel) noexcept
Options with minLevel and the Bare format.
Definition SzLogging.hpp:381
constexpr SzLogCallbackOptions(SzLogLevel minLevel, SzLogFormat format) noexcept
Options with minLevel and format.
Definition SzLogging.hpp:385
constexpr bool operator==(const SzLogCallbackOptions &other) const =default
constexpr SzLogLevel MinLevel() const noexcept
Minimum level delivered to the callback.
Definition SzLogging.hpp:396
Collects the records logged on the thread that created it until Finish().
Definition SzLogging.hpp:614
SzLogCapture(std::unique_ptr< SzLogRegistration > registration, std::shared_ptr< Buffer > buffer) noexcept
Definition SzLogging.hpp:622
std::vector< SzLogEntry > Finish()
Unregisters, then returns everything captured.
Definition SzLogging.hpp:628
Destination of native's default output.
Definition SzLogging.hpp:306
static SzLogOutput Null()
Discard; only registered callbacks receive messages.
Definition SzLogging.hpp:320
bool IsAppend() const noexcept
True for a file output that keeps existing content.
Definition SzLogging.hpp:351
static SzLogOutput Stderr()
Standard error (the native default).
Definition SzLogging.hpp:312
static SzLogOutput AppendFile(std::filesystem::path filePath)
A file output that appends to the file, keeping what is already there (SZ_LOG_OUTPUT_FILE_APPEND).
Definition SzLogging.hpp:337
static SzLogOutput Callback()
A default callback (get-only: SetDefaultOutput rejects it).
Definition SzLogging.hpp:325
bool operator==(const SzLogOutput &other) const =default
SzLogOutputType OutputType() const noexcept
The kind of destination.
Definition SzLogging.hpp:343
static SzLogOutput Stdout()
Standard output.
Definition SzLogging.hpp:315
SzLogOutput()=default
Standard error (the native default).
const std::filesystem::path & Path() const noexcept
The file path (empty unless OutputType() is File).
Definition SzLogging.hpp:346
static SzLogOutput File(std::filesystem::path filePath)
A file output that truncates the file when it is opened (SZ_LOG_OUTPUT_FILE).
Definition SzLogging.hpp:331
One log message, borrowed from native for the duration of a callback.
Definition SzLogging.hpp:451
constexpr SzLogRecord(SzLogLevel level, std::string_view rawModule, std::string_view message, std::optional< int64_t > timestampNs=std::nullopt, std::optional< uint64_t > threadID=std::nullopt) noexcept
Builds a record over native's fields.
Definition SzLogging.hpp:455
constexpr std::optional< uint64_t > ThreadID() const noexcept
OS id of the thread that logged the message.
Definition SzLogging.hpp:519
constexpr std::optional< std::string_view > ThreadHash() const noexcept
The thread hash after the last ':' of native's module ("82eb15f5faba9bab"), if present.
Definition SzLogging.hpp:479
constexpr std::string_view Message() const noexcept
The message exactly as native formatted it for this registration.
Definition SzLogging.hpp:489
constexpr SzLogLevel Level() const noexcept
The message's severity.
Definition SzLogging.hpp:466
SzLogEntry ToEntry() const
An owned copy, for queues and buffers.
Definition SzLogging.hpp:524
constexpr std::optional< int64_t > TimestampNs() const noexcept
When native logged the message: UTC nanoseconds since the Unix epoch.
Definition SzLogging.hpp:500
constexpr std::string_view RawModule() const noexcept
Native's module string, unparsed ("szstatic:82eb15f5faba9bab").
Definition SzLogging.hpp:494
constexpr std::string_view Module() const noexcept
The module name: native's module up to its last ':' (for "szstatic:82eb15f5faba9bab",...
Definition SzLogging.hpp:470
std::optional< std::chrono::system_clock::time_point > Timestamp() const
When native logged the message (UTC), at the platform's system_clock precision.
Definition SzLogging.hpp:508
virtual void SetLevel(SzLogLevel minLevel)=0
Changes this callback's minimum level in place (SzLogging_setCallbackLevel).
SzLogRegistration & operator=(const SzLogRegistration &)=delete
virtual void Unregister()=0
Unregisters now (same as destroying the registration). Idempotent.
virtual SzLogLevel Level()=0
This callback's current minimum level (SzLogging_getCallbackLevel).
SzLogRegistration()=default
virtual void Leak()=0
Keeps the callback registered (and called) for the rest of the process; the destructor then does noth...
virtual ~SzLogRegistration()=default
Unregisters the callback unless Unregister() or Leak() already ran.
SzLogRegistration(const SzLogRegistration &)=delete
SzLogRegistration & operator=(SzLogRegistration &&)=delete
SzLogRegistration(SzLogRegistration &&)=delete
Senzing native logging (libSzLogging).
Definition SzLogging.hpp:657
SzLogCapture CaptureThreadLogs(const SzLogCallbackOptions &options)
Captures the records logged on the calling thread until SzLogCapture::Finish(): one thread-local regi...
Definition SzLogging.hpp:761
virtual void SetDefaultFormat(SzLogFormat format)=0
Sets the default output's format (SzLogging_setDefaultFormatter).
virtual ~SzLogging()=default
virtual SzLogLevel GetEffectiveLogLevel()=0
The effective level (SzLogging_getLogLevel): the minimum over the default output and every registered...
virtual void SetDefaultLogLevel(SzLogLevel level)=0
Sets the default output's level (SzLogging_setLogLevel).
virtual std::unique_ptr< SzLogRegistration > RegisterCallback(const SzLogCallbackOptions &options, SzLogCallback callback)=0
Registers callback for messages from every thread (SzLogging_registerCallbackWithFormat).
virtual SzLogFormat GetDefaultFormat()=0
The default output's format (SzLogging_getDefaultFormatter).
virtual void SetDefaultOutput(const SzLogOutput &output)=0
Sets the default output (SzLogging_setDefaultOutput).
virtual std::unique_ptr< SzLogRegistration > RegisterThreadLocalCallback(const SzLogCallbackOptions &options, SzLogCallback callback)=0
Registers callback for messages logged on the CALLING OS THREAD only.
virtual SzLogLevel GetDefaultLogLevel()=0
The default output's level, exactly what SetDefaultLogLevel set (SzLogging_getDefaultLogLevel).
virtual void SetDefaultCallback(const SzLogCallbackOptions &options, SzLogCallback callback)=0
Makes callback the default output, at options.MinLevel() and in options.Format() (SzLogging_setDefaul...
virtual SzLogOutput GetDefaultOutput()=0
The current default output (SzLogging_getDefaultOutput).
Definition SzLogging.hpp:234
std::string NormalizeLogName(std::string_view text)
Upper-cases ASCII letters and strips surrounding whitespace.
Definition SzLogging.hpp:237
Definition SzCoreConfig.hpp:20
std::function< void(const SzLogRecord &)> SzLogCallback
A log callback.
Definition SzLogging.hpp:553
constexpr std::array< SzLogFormat, 4 > SzLogFormats
All formats.
Definition SzLogging.hpp:178
SzLogOutputType
The kind of destination of native's default output (SZ_LOG_OUTPUT_*).
Definition SzLogging.hpp:294
@ File
A file opened by native (truncate or append).
Definition SzLogging.hpp:297
@ Stdout
Standard output.
Definition SzLogging.hpp:296
@ Callback
A default callback is set.
Definition SzLogging.hpp:301
@ Stderr
Standard error (the native default).
Definition SzLogging.hpp:295
@ Null
Discard; only registered callbacks receive messages.
Definition SzLogging.hpp:298
SzLogLevel
Log severity.
Definition SzLogging.hpp:141
@ Critical
Critical errors (SZ_LOG_LEVEL_CRITICAL).
Definition SzLogging.hpp:147
@ Info
Informational messages (SZ_LOG_LEVEL_INFO).
Definition SzLogging.hpp:144
@ Warn
Warnings (SZ_LOG_LEVEL_WARN).
Definition SzLogging.hpp:145
@ Error
Errors (SZ_LOG_LEVEL_ERROR).
Definition SzLogging.hpp:146
@ Debug
Debug information (SZ_LOG_LEVEL_DEBUG).
Definition SzLogging.hpp:143
@ Off
Nothing is delivered (SZ_LOG_LEVEL_OFF).
Definition SzLogging.hpp:150
@ Trace
Most verbose: execution flow (SZ_LOG_LEVEL_TRACE).
Definition SzLogging.hpp:142
constexpr std::array< SzLogLevel, 7 > SzLogLevels
All levels, most verbose first.
Definition SzLogging.hpp:154
SzLogFormat ParseSzLogFormat(std::string_view text)
Parses a format name.
Definition SzLogging.hpp:274
SzLogFormat
Message format, applied by native (SZ_LOG_FORMAT_*).
Definition SzLogging.hpp:163
@ Legacy
YYYY-MM-DD HH:MM:SS.mmm [module:hash] SEV: msg, in local time.
Definition SzLogging.hpp:165
@ Bare
The message text only.
Definition SzLogging.hpp:170
@ Jsonl
One JSON object per line: timestamp, module, severity, message.
Definition SzLogging.hpp:168
@ Default
For the default output: spdlog's default pattern [YYYY-MM-DD HH:MM:SS.mmm] [module] [level] msg (loca...
Definition SzLogging.hpp:174
constexpr std::string_view ToString(SzLogLevel level) noexcept
The canonical upper-case name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL" or "OFF" ("UNKNO...
Definition SzLogging.hpp:188
SzLogLevel ParseSzLogLevel(std::string_view text)
Parses a level name.
Definition SzLogging.hpp:256
std::ostream & operator<<(std::ostream &os, SzLogLevel level)
Writes ToString(level).
Definition SzLogging.hpp:225
The buffer shared between the capture and its callback.
Definition SzLogging.hpp:617
std::mutex Mutex
Definition SzLogging.hpp:618
std::vector< SzLogEntry > Entries
Definition SzLogging.hpp:619
An owned copy of an SzLogRecord, for queues and buffers.
Definition SzLogging.hpp:418
std::string Module
The module name (SzLogRecord::Module()).
Definition SzLogging.hpp:422
std::optional< std::string > ThreadHash
The thread hash (SzLogRecord::ThreadHash()).
Definition SzLogging.hpp:424
std::string Message
The message as native formatted it.
Definition SzLogging.hpp:426
SzLogLevel Level
The message's severity.
Definition SzLogging.hpp:420
bool operator==(const SzLogEntry &other) const =default
std::optional< std::chrono::system_clock::time_point > Timestamp
When native logged the message (UTC); std::nullopt until Senzing >= 4.5.1 / 4.6.0.
Definition SzLogging.hpp:429
std::optional< uint64_t > ThreadID
OS id of the logging thread; std::nullopt until Senzing >= 4.5.1 / 4.6.0.
Definition SzLogging.hpp:432