Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom logger capabilities #180

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bwrsandman
Copy link
Contributor

Add levels to public API.
Default to old stderr logging method.
Add function pointer and userdata pointer as static globals.

Implements #179

@bwrsandman bwrsandman marked this pull request as ready for review January 3, 2024 05:25
bwrsandman added a commit to bwrsandman/openblack that referenced this pull request Jan 3, 2024
@bwrsandman
Copy link
Contributor Author

bwrsandman commented Jan 3, 2024

Example use of custom logger using spdlog in c++

void unshield_log_handler([[maybe_unused]] void* userdata, int level, const char* file, int line, const char* format, va_list args)
{
	spdlog::level::level_enum spdlogLevel;
	switch (level)
	{
	case UNSHIELD_LOG_LEVEL_TRACE:
		spdlogLevel = spdlog::level::trace;
		break;
	case UNSHIELD_LOG_LEVEL_WARNING:
		spdlogLevel = spdlog::level::warn;
		break;
	case UNSHIELD_LOG_LEVEL_ERROR:
		spdlogLevel = spdlog::level::err;
		break;
	}

	// Get the length of the message
	va_list args_copy;
	va_copy(args_copy, args);
	int length = vsnprintf(nullptr, 0, format, args_copy) + 1;
	va_end(args_copy);

	std::vector<char> message(length);
	vsnprintf(message.data(), message.size(), format, args);

	SPDLOG_LOGGER_CALL(spdlog::get("filesystem"), spdlogLevel, "unshield: {}", message.data());
}

@bwrsandman bwrsandman force-pushed the log-handler branch 2 times, most recently from a97300a to 6da5ca2 Compare January 3, 2024 06:42
bwrsandman added a commit to bwrsandman/openblack that referenced this pull request Jan 3, 2024
@twogood
Copy link
Owner

twogood commented Jan 3, 2024

@bwrsandman sorry about the merge conflict!

@twogood twogood self-assigned this Jan 3, 2024
Default to old stderr logging method.
Add function pointer and userdata pointer as static globals.
@bwrsandman
Copy link
Contributor Author

rebased!

bwrsandman added a commit to bwrsandman/openblack that referenced this pull request Jan 3, 2024
bwrsandman added a commit to bwrsandman/openblack that referenced this pull request Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants