Skip to content

Commit 4411313

Browse files
authored
[k2] update k2-header (#1365)
1 parent 6b2d2b9 commit 4411313

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

runtime-light/k2-platform/k2-api.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <expected>
1212
#include <format>
1313
#include <memory>
14+
#include <optional>
1415
#include <span>
1516
#include <string_view>
1617
#include <sys/utsname.h>
@@ -163,8 +164,12 @@ inline uint8_t take_update(k2::descriptor* descriptor) noexcept {
163164
return k2_take_update(descriptor);
164165
}
165166

166-
inline void log(size_t level, size_t len, const char* str) noexcept {
167-
k2_log(level, len, str);
167+
using LogTaggedEntry = ::LogKeyValuePair;
168+
169+
inline void log(size_t level, std::string_view msg, std::optional<std::span<LogTaggedEntry>> tags) noexcept {
170+
const auto tags_count{tags.value_or(std::span<LogTaggedEntry>{}).size()};
171+
const auto* tags_data{tags.value_or(std::span<LogTaggedEntry>{}).data()};
172+
k2_log(level, msg.size(), msg.data(), tags_count, tags_data);
168173
}
169174

170175
inline size_t log_level_enabled() noexcept {

runtime-light/k2-platform/k2-header.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,24 @@ void k2_free_descriptor(uint64_t descriptor);
314314
uint8_t k2_take_update(uint64_t* update_d);
315315

316316
/**
317-
* Only utf-8 string supported.
317+
* Represents a key-value pair that can be added to a log.
318+
* Each instance of this struct must contain valid, non-null pointers for both the key and the value.
319+
* The lengths of the key and value are specified by `key_len` and `value_len`, respectively.
320+
*
321+
* If an instance is intended to represent only a key without an associated value,
322+
* the `value` pointer should point to an empty string (""), and `value_len` should be set to zero.
323+
*/
324+
struct LogKeyValuePair {
325+
const char* key;
326+
const char* value;
327+
size_t key_len;
328+
size_t value_len;
329+
};
330+
331+
/**
332+
* Writes a structured log message. Only UTF-8 encoded strings are supported.
333+
*
334+
* @param `level` The severity level of the log message.
318335
* Possible `level` values:
319336
* 1 => Error
320337
* 2 => Warn
@@ -323,8 +340,13 @@ uint8_t k2_take_update(uint64_t* update_d);
323340
* 5 => Trace
324341
* Any other value will cause the log to be skipped
325342
* if `level` > `log_level_enabled()` log will be skipped
343+
* @param `len` length of msg in bytes
344+
* @param `msg` A pointer to the log message
345+
* @param `kv_count` The number of key-value pairs.
346+
* @param `kv_pairs` An array of key-value pairs.
326347
*/
327-
void k2_log(size_t level, size_t len, const char* str);
348+
349+
void k2_log(size_t level, size_t len, const char* msg, size_t kv_count, const struct LogKeyValuePair* kv_pairs);
328350

329351
// Use for optimization, see `k2_log`
330352
size_t k2_log_level_enabled();

runtime-light/utils/logs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void log(level level, std::optional<std::span<void* const>> trace, std::format_s
6363
}
6464

6565
*out = '\0';
66-
k2::log(std::to_underlying(level), size, log_buffer.data());
66+
k2::log(std::to_underlying(level), std::string_view{log_buffer.data(), static_cast<std::string_view::size_type>(size)}, std::nullopt);
6767
}
6868

6969
template<typename... Args>

0 commit comments

Comments
 (0)