Skip to content

Commit

Permalink
Added spdlog-based logger and JSON-related code
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiden committed Feb 22, 2025
1 parent c8509aa commit 2d0569c
Show file tree
Hide file tree
Showing 18 changed files with 1,301 additions and 40 deletions.
12 changes: 10 additions & 2 deletions src/projects/base/ovlibrary/AMS.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ include $(DEFAULT_VARIABLES)

LOCAL_TARGET := ovlibrary

LOCAL_HEADER_FILES := $(LOCAL_HEADER_FILES)
LOCAL_SOURCE_FILES := $(LOCAL_SOURCE_FILES)
LOCAL_HEADER_FILES := $(LOCAL_HEADER_FILES) \
$(call get_sub_header_list,logger) \
$(call get_sub_header_list,logger/formatters) \
$(call get_sub_header_list,logger/pattern_flags)

LOCAL_SOURCE_FILES := $(LOCAL_SOURCE_FILES) \
$(call get_sub_source_list,logger) \
$(call get_sub_source_list,logger/formatters) \
$(call get_sub_source_list,logger/pattern_flags)

$(call add_pkg_config,libpcre2-8)
$(call add_pkg_config,spdlog)

include $(BUILD_STATIC_LIBRARY)
61 changes: 30 additions & 31 deletions src/projects/base/ovlibrary/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@

namespace ov
{
constexpr const char *StringFromJsonValueType(::Json::ValueType value_type)
{
switch (value_type)
{
case ::Json::ValueType::nullValue:
return "null";
case ::Json::ValueType::intValue:
return "int";
case ::Json::ValueType::uintValue:
return "uint";
case ::Json::ValueType::realValue:
return "real";
case ::Json::ValueType::stringValue:
return "string";
case ::Json::ValueType::booleanValue:
return "boolean";
case ::Json::ValueType::arrayValue:
return "array";
case ::Json::ValueType::objectValue:
return "object";
}

return "unknown";
}

const char *StringFromJsonValueType(const ::Json::Value &value)
{
return StringFromJsonValueType(value.type());
}

ov::String Json::Stringify(const JsonObject &object)
{
return Stringify(object._value);
Expand Down Expand Up @@ -74,35 +104,4 @@ namespace ov

return JsonObject::NullObject();
}

const char *StringFromJsonValueType(::Json::ValueType value_type)
{
switch (value_type)
{
case ::Json::ValueType::nullValue:
return "null";
case ::Json::ValueType::intValue:
return "int";
case ::Json::ValueType::uintValue:
return "uint";
case ::Json::ValueType::realValue:
return "real";
case ::Json::ValueType::stringValue:
return "string";
case ::Json::ValueType::booleanValue:
return "boolean";
case ::Json::ValueType::arrayValue:
return "array";
case ::Json::ValueType::objectValue:
return "object";
}

return "unknown";
}

const char *StringFromJsonValueType(const ::Json::Value &value)
{
return StringFromJsonValueType(value.type());
}

} // namespace ov
9 changes: 4 additions & 5 deletions src/projects/base/ovlibrary/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
//==============================================================================
#pragma once

#include "./json_object.h"
#include "./string.h"
#include "./json_builder.h"

namespace ov
{
constexpr const char *StringFromJsonValueType(::Json::ValueType value_type);
const char *StringFromJsonValueType(const ::Json::Value &value);

class Json
{
public:
Expand All @@ -26,7 +28,4 @@ namespace ov
static JsonObject Parse(const ov::String &str);
static JsonObject Parse(const std::shared_ptr<const Data> &data);
};

const char *StringFromJsonValueType(::Json::ValueType value_type);
const char *StringFromJsonValueType(const ::Json::Value &value);
} // namespace ov
258 changes: 258 additions & 0 deletions src/projects/base/ovlibrary/json_builder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
//==============================================================================
//
// OvenMediaEngine
//
// Created by Hyunjun Jang
// Copyright (c) 2025 AirenSoft. All rights reserved.
//
//==============================================================================
#include "./json_builder.h"

#include "./json.h"

namespace ov
{
JsonBuilder::JsonBuilder(const PrivateToken &token)
{
}

std::shared_ptr<JsonBuilder> JsonBuilder::Builder(JsonBuilderModifier modifier)
{
auto builder = std::make_shared<JsonBuilder>(PrivateToken());

auto modified_builder = (modifier != nullptr) ? modifier(builder.get()) : builder.get();

if (modified_builder != nullptr)
{
return modified_builder->GetSharedPtrAs<JsonBuilder>();
}

return nullptr;
}

void JsonBuilder::PushBackInternal(const char *key, JsonValueType value)
{
if (_value_type == ::Json::ValueType::nullValue)
{
_value_type = ::Json::ValueType::objectValue;
}

if (_value_type != ::Json::ValueType::objectValue)
{
OV_ASSERT(false, "The current JSON value is not an object");
return;
}

_value_map.PushBack(key, std::move(value));
}

JsonBuilder *JsonBuilder::PushBack(const char *key, ::Json::Value value)
{
PushBackInternal(key, std::move(value));

return this;
}

JsonBuilder *JsonBuilder::PushBack(const char *key, std::shared_ptr<JsonBuilder> builder)
{
if (builder == nullptr)
{
PushBackInternal(key, ::Json::Value::null);
}
else
{
PushBackInternal(key, builder);
}

return this;
}

JsonBuilder *JsonBuilder::PushBack(const char *key, JsonBuilderModifier modifier)
{
if (modifier == nullptr)
{
PushBackInternal(key, ::Json::Value::null);
}
else
{
PushBackInternal(key, Builder(modifier));
}

return this;
}

void JsonBuilder::PushBackInternal(JsonValueType item)
{
if (_value_type == ::Json::ValueType::nullValue)
{
_value_type = ::Json::ValueType::arrayValue;
}

if (_value_type != ::Json::ValueType::arrayValue)
{
OV_ASSERT(false, "The current JSON value is not an array");
return;
}

_value_list.push_back(std::move(item));
}

JsonBuilder *JsonBuilder::PushBack(::Json::Value value)
{
PushBackInternal(std::move(value));

return this;
}

JsonBuilder *JsonBuilder::PushBack(std::shared_ptr<JsonBuilder> builder)
{
if (builder == nullptr)
{
PushBackInternal(::Json::Value::null);
}
else
{
PushBackInternal(builder);
}

return this;
}

JsonBuilder *JsonBuilder::PushBack(JsonBuilderModifier modifier)
{
if (modifier == nullptr)
{
PushBackInternal(::Json::Value::null);
}
else
{
PushBackInternal(Builder(modifier));
}

return this;
}

ov::String JsonBuilder::Stringify() const
{
ov::String output;
return Stringify(output);
}

ov::String &JsonBuilder::Stringify(ov::String &output) const
{
switch (_value_type)
{
case ::Json::ValueType::nullValue:
output = "";
break;

case ::Json::ValueType::objectValue:
StringifyObject(output);
break;

case ::Json::ValueType::arrayValue:
StringifyArray(output);
break;

default:
OV_ASSERT(false, "Invalid JSON value type");
break;
}

return output;
}

static std::string EscapeString(ov::String &output, const ov::String &value)
{
::Json::StreamWriterBuilder builder;

return ::Json::writeString(builder, value.CStr()).c_str();
}

ov::String &JsonBuilder::StringifyObject(ov::String &output) const
{
auto end = _value_map.end();

output.Append('{');

for (auto it = _value_map.begin(); it != end; ++it)
{
if (it != _value_map.begin())
{
output.Append(',');
}

auto &key = it->first;
auto &value = it->second;

// Print key
output.AppendFormat("%s:", EscapeString(output, key).c_str());

// Print value
if (std::holds_alternative<::Json::Value>(value))
{
auto json = std::get<::Json::Value>(value);

output.Append(Json::Stringify(json, false));
}
else if (std::holds_alternative<std::shared_ptr<JsonBuilder>>(value))
{
auto builder = std::get<std::shared_ptr<JsonBuilder>>(value);

builder->Stringify(output);
}
else
{
OV_ASSERT(false, "Invalid JSON value type: %d", value.index());
}
}

output.Append('}');

return output;
}

ov::String &JsonBuilder::StringifyArray(ov::String &output) const
{
auto end = _value_list.end();

output.Append('[');

for (auto it = _value_list.begin(); it != end; ++it)
{
if (it != _value_list.begin())
{
output.Append(',');
}

auto &value = *it;

// Print value
if (std::holds_alternative<::Json::Value>(value))
{
auto json = std::get<::Json::Value>(value);

output.Append(Json::Stringify(json, false));
}
else if (std::holds_alternative<std::shared_ptr<JsonBuilder>>(value))
{
auto builder = std::get<std::shared_ptr<JsonBuilder>>(value);

builder->Stringify(output);
}
else
{
OV_ASSERT(false, "Invalid JSON value type: %d", value.index());
}
}

output.Append(']');

return output;
}

::Json::Value JsonBuilder::ToJsonValue() const
{
return ::Json::Value();
}
} // namespace ov
Loading

0 comments on commit 2d0569c

Please sign in to comment.