2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_OTLP_OTLPJSONSERIALIZER_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_OTLP_OTLPJSONSERIALIZER_HPP_INCLUDED
17#include <unordered_map>
34 out.reserve(value.size() + 16);
36 for (
size_t i = 0; i < value.size(); ++i) {
37 const unsigned char c =
static_cast<unsigned char>(value[i]);
40 case '"': out +=
"\\\"";
break;
41 case '\\': out +=
"\\\\";
break;
42 case '\b': out +=
"\\b";
break;
43 case '\f': out +=
"\\f";
break;
44 case '\n': out +=
"\\n";
break;
45 case '\r': out +=
"\\r";
break;
46 case '\t': out +=
"\\t";
break;
49 static const char* hex =
"0123456789abcdef";
51 out += hex[(c >> 4) & 0x0F];
54 out +=
static_cast<char>(c);
103 <<
"\",\"value\":{\"intValue\":\"" << value <<
"\"}}";
112 <<
"\",\"value\":{\"boolValue\":" << (value ?
"true" :
"false") <<
"}}";
120 if (std::isfinite(value)) {
122 <<
"\",\"value\":{\"doubleValue\":"
123 << std::setprecision(std::numeric_limits<double>::max_digits10)
127 <<
"\",\"value\":{\"stringValue\":\"" <<
otlp_json_escape(std::to_string(value)) <<
"\"}}";
136 if (value <=
static_cast<uint64_t
>((std::numeric_limits<int64_t>::max)())) {
140 <<
"\",\"value\":{\"stringValue\":\"" << value <<
"\"}}";
148 std::string result = name;
149 for (
char& c : result) {
150 if (!std::isalnum(
static_cast<unsigned char>(c)) && c !=
'_' && c !=
'.' && c !=
'-') {
162 std::ostringstream& os,
166 const int64_t time_unix_nano = r.
timestamp_ms * 1000000LL;
169 os <<
"\"timeUnixNano\":\"" << time_unix_nano <<
"\",";
173 os <<
"\"attributes\":[";
204 std::unordered_map<std::string, std::size_t> key_count;
206 for (std::size_t i = 0; i < r.
args_array.size(); ++i) {
211 bool all_underscore =
true;
212 for (std::size_t j = 0; j < sanitized.size(); ++j) {
213 if (sanitized[j] !=
'_') {
214 all_underscore =
false;
220 if (sanitized.empty() || all_underscore) {
226 auto it = key_count.find(key);
227 if (it != key_count.end()) {
229 key +=
"." + std::to_string(it->second);
300 const std::vector<OtlpLogItem>& batch,
302 std::ostringstream os;
305 os <<
"\"resourceLogs\":[{";
306 os <<
"\"resource\":{\"attributes\":[";
308 bool resource_first =
true;
328 os <<
"\"scopeLogs\":[{";
329 os <<
"\"scope\":{\"name\":\"logit-cpp\"},";
330 os <<
"\"logRecords\":[";
332 for (
size_t i = 0; i < batch.size(); ++i) {
Defines a stable snapshot of LogRecord data for asynchronous OTLP export.
The primary namespace for the LogIt++ library.
void otlp_write_uint_attr(std::ostringstream &os, const std::string &key, uint64_t value)
Writes a uint64 OTLP attribute.
void otlp_write_double_attr(std::ostringstream &os, const std::string &key, double value)
Writes a double OTLP attribute.
void otlp_write_string_attr(std::ostringstream &os, const std::string &key, const std::string &value)
Writes a string OTLP attribute.
@ LOG_LVL_TRACE
Trace level logging.
@ LOG_LVL_ERROR
Error level logging.
@ LOG_LVL_INFO
Information level logging.
@ LOG_LVL_FATAL
Fatal level logging.
@ LOG_LVL_DEBUG
Debug level logging.
@ LOG_LVL_WARN
Warning level logging.
void otlp_write_comma_if_needed(std::ostringstream &os, bool &first)
Writes a comma before the next JSON value when needed.
std::string sanitize_otlp_key(const std::string &name)
Sanitizes an attribute key for OTLP.
std::string otlp_json_escape(const std::string &value)
Escapes a string for JSON output.
void otlp_write_bool_attr(std::ostringstream &os, const std::string &key, bool value)
Writes a bool OTLP attribute.
void otlp_write_log_record_json(std::ostringstream &os, const OtlpLogItem &item, const OtlpJsonFormatConfig &config)
Writes one OTLP JSON log record.
std::string build_otlp_logs_json_payload(const std::vector< OtlpLogItem > &batch, const OtlpJsonFormatConfig &config)
Builds an OTLP ExportLogsServiceRequest JSON payload.
void otlp_write_int_attr(std::ostringstream &os, const std::string &key, int64_t value)
Writes an int OTLP attribute.
const char * to_c_str(LogLevel level, int mode=0)
Convert LogLevel to a C-style string representation.
int otlp_severity_number(LogLevel level)
Maps LogIt++ levels to OpenTelemetry severity numbers.
Owns one formatted log message and its structured metadata.
OtlpRecordSnapshot record
Structured log metadata snapshot.
std::string message
Formatted message body.
Owns LogRecord fields required after original logging call returns.
std::string format
Original message or format string.
LogLevel log_level
Log severity level.
int64_t timestamp_ms
Timestamp in milliseconds.
int line
Source line number.
std::string thread_id
Stringified std::thread::id.
std::vector< VariableValue > args_array
Structured argument values.
std::string file
Source file path.
int logger_index
Target logger index, or -1 for all.
std::string arg_names
Original argument names.
std::string function
Source function name.
bool fmt_mode
fmt formatting mode flag.
bool raw_mode
Raw log mode flag.
bool print_mode
Raw argument print mode flag.
Structure for storing values of various types, including enumerations.
long double long_double_value
std::string name
Variable name.
std::string to_string() const
Method to get the value as a string.
enum logit::VariableValue::ValueType type
Specifies the type of the stored value in the VariableValue structure.
@ BOOL_VAL
Value of type bool.
@ FLOAT_VAL
Value of type float (single-precision floating point).
@ INT16_VAL
Value of type int16_t (signed 16-bit integer).
@ UINT64_VAL
Value of type uint64_t (unsigned 64-bit integer).
@ UINT32_VAL
Value of type uint32_t (unsigned 32-bit integer).
@ INT64_VAL
Value of type int64_t (signed 64-bit integer).
@ DOUBLE_VAL
Value of type double (double-precision floating point).
@ INT32_VAL
Value of type int32_t (signed 32-bit integer).
@ UINT8_VAL
Value of type uint8_t (unsigned 8-bit integer).
@ LONG_DOUBLE_VAL
Value of type long double (extended-precision floating point).
@ INT8_VAL
Value of type int8_t (signed 8-bit integer).
@ UINT16_VAL
Value of type uint16_t (unsigned 16-bit integer).
union logit::VariableValue::@0 pod_value
Union to store POD types.