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

[FR] binary/hex data display #639

Closed
zhuzhzh opened this issue Jan 4, 2025 · 2 comments
Closed

[FR] binary/hex data display #639

zhuzhzh opened this issue Jan 4, 2025 · 2 comments

Comments

@zhuzhzh
Copy link

zhuzhzh commented Jan 4, 2025

I think we could refer to spdlog. basicly, we need one pre-defineed formatting for display hex or binary data, especially for vector or array.

// many types of std::container<char> types can be used.
// ranges are supported too.
// format flags:
// {:X} - print in uppercase.
// {:s} - don't separate each byte with space.
// {:p} - don't print the position on each line start.
// {:n} - don't split the output into lines.
// {:a} - show ASCII if :n is not set.

#include "spdlog/fmt/bin_to_hex.h"

void binary_example()
{
    auto console = spdlog::get("console");
    std::array<char, 80> buf;
    console->info("Binary example: {}", spdlog::to_hex(buf));
    console->info("Another binary example:{:n}", spdlog::to_hex(std::begin(buf), std::begin(buf) + 10));
    // more examples:
    // logger->info("uppercase: {:X}", spdlog::to_hex(buf));
    // logger->info("uppercase, no delimiters: {:Xs}", spdlog::to_hex(buf));
    // logger->info("uppercase, no delimiters, no position info: {:Xsp}", spdlog::to_hex(buf));
}
@zhuzhzh zhuzhzh changed the title binary/hex data display [feature request] binary/hex data display Jan 4, 2025
@zhuzhzh zhuzhzh changed the title [feature request] binary/hex data display [FR] binary/hex data display Jan 4, 2025
@odygrd
Copy link
Owner

odygrd commented Jan 4, 2025

Hey, there is a similar function in quill/Utility.h. Note that it is doing the formatting on the hot path - like spdlog

For example

#include "quill/Backend.h"
#include "quill/Frontend.h"
#include "quill/LogMacros.h"
#include "quill/Logger.h"
#include "quill/sinks/ConsoleSink.h"
#include "quill/Utility.h"

#include <iostream>
#include <string>
#include <utility>
#include <array>


/**
 * Trivial logging example to console
 * Note: You can also pass STL types by including the relevant header files from quill/std/
 */

int main()
{
  quill::BackendOptions backend_options;
  quill::Backend::start(backend_options);

  // Frontend
  auto console_sink = quill::Frontend::create_or_get_sink<quill::ConsoleSink>(
    "sink_id_1", quill::ConsoleSink::ColourMode::Automatic);

  quill::Logger* logger = quill::Frontend::create_or_get_logger("root", std::move(console_sink));

  std::array<char, 80> buffer;
  buffer[0] = 'A';
  buffer[1] = 'B';
  LOG_INFO(logger, "Hex: {}", quill::utility::to_hex(buffer.data(), buffer.size()));
}

@zhuzhzh
Copy link
Author

zhuzhzh commented Jan 5, 2025

@odygrd Thanks! It's great that it's already supported.

@zhuzhzh zhuzhzh closed this as completed Jan 5, 2025
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

No branches or pull requests

2 participants