Skip to content

Commit

Permalink
build, logger: updated fmt submodule to 9.1.0; updated fmt integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Pospesel committed Sep 17, 2022
1 parent bb69fb5 commit b8a8f4f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/extern/fmt
Submodule fmt updated 131 files
1 change: 1 addition & 0 deletions src/libtego/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ add_library(
source/tor_stubs.cpp
source/user.cpp
source/user.hpp
source/utilities.cpp
source/utils/CryptoKey.cpp
source/utils/CryptoKey.h
source/utils/PendingOperation.cpp
Expand Down
6 changes: 1 addition & 5 deletions src/libtego/include/tego/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using std::experimental::source_location;
// fmt
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <fmt/std.h>

// wrapper around fmt::print that writes to singleton log file libtego.log
class logger
Expand Down Expand Up @@ -64,8 +65,3 @@ class logger
static void trace() {}
};
#endif // ENABLE_TEGO_LOGGER

// always provide these overloads
std::ostream& operator<<(std::ostream& out, const class QString& str);
std::ostream& operator<<(std::ostream& out, const class QByteArray& blob);

15 changes: 15 additions & 0 deletions src/libtego/include/tego/utilities.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once

// fmt
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <fmt/std.h>

#define TEGO_STRINGIFY_IMPL(X) #X
#define TEGO_STRINGIFY(X) TEGO_STRINGIFY_IMPL(X)

Expand All @@ -17,6 +22,16 @@

#define TEGO_THROW_IF_EQUAL(A, B) if((A) == (B)) { TEGO_THROW_MSG("{} and {} must not be equal", TEGO_STRINGIFY(A), TEGO_STRINGIFY(B)); }

// always provide these overloads
std::ostream& operator<<(std::ostream& out, const class QString& str);
std::ostream& operator<<(std::ostream& out, const class QByteArray& blob);

namespace fmt
{
template <> struct formatter<class QString> : ostream_formatter {};
template <> struct formatter<class QByteArray> : ostream_formatter {};
}

namespace tego
{
//
Expand Down
77 changes: 0 additions & 77 deletions src/libtego/source/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,80 +30,3 @@ double logger::get_timestamp()
}
#endif

//
// std::ostream << operators
//

std::ostream& operator<<(std::ostream& out, const QString& str)
{
auto utf8str = str.toUtf8();
out << utf8str.constData();
return out;
}

// hex dump QByteArray
std::ostream& operator<<(std::ostream& out, const QByteArray& blob)
{
constexpr size_t rowWidth = 32;
const size_t rowCount = static_cast<size_t>(blob.size()) / rowWidth;

const char* head = blob.data();
size_t address = 0;

out << '\n';

auto printRow = [&](size_t count) -> void
{
constexpr auto octetGrouping = 4;
fmt::print(out, "{:08x} : ", address);
for(size_t k = 0; k < count; k++)
{
if ((k % octetGrouping) == 0) {
fmt::print(out, " ");
}
fmt::print(out, "{:02x}", static_cast<uint8_t>(head[k]));
}
for(size_t k = count; k < rowWidth; k++)
{
if ((k % octetGrouping) == 0) {
fmt::print(out, " ");
}
fmt::print(out, "..");
}

fmt::print(out, " | ");
for(size_t k = 0; k < count; k++)
{
char c = head[k];
if (std::isprint(c))
{
fmt::print(out, "{}", c);
}
else
{
fmt::print(out, ".");
}
}

out << '\n';

address += rowWidth;
head += rowWidth;
};

// foreach row
for(size_t i = 0; i < rowCount; i++)
{
printRow(rowWidth);

}

// remainder
const size_t remainder = (static_cast<size_t>(blob.size()) % rowWidth);
if (remainder > 0)
{
printRow(remainder);
}

return out;
}
10 changes: 6 additions & 4 deletions src/libtego/source/precomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ extern "C" {
#ifdef __cplusplus
}

// include our public header
#include <tego/tego.hpp>

#endif // __cplusplus

// C++ headers
Expand All @@ -66,6 +63,7 @@ extern "C" {

// fmt
#include <fmt/format.h>
#include <fmt/std.h>
#include <fmt/ostream.h>

// Qt
Expand Down Expand Up @@ -137,4 +135,8 @@ namespace tego
}
}

#endif //__cplusplus#i
// include our public header
#include <tego/tego.hpp>

#endif //__cplusplus#

78 changes: 78 additions & 0 deletions src/libtego/source/utilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

//
// std::ostream << operators
//

std::ostream& operator<<(std::ostream& out, const QString& str)
{
auto utf8str = str.toUtf8();
out << utf8str.constData();
return out;
}

// hex dump QByteArray
std::ostream& operator<<(std::ostream& out, const QByteArray& blob)
{
constexpr size_t rowWidth = 32;
const size_t rowCount = static_cast<size_t>(blob.size()) / rowWidth;

const char* head = blob.data();
size_t address = 0;

out << '\n';

auto printRow = [&](size_t count) -> void
{
constexpr auto octetGrouping = 4;
fmt::print(out, "{:08x} : ", address);
for(size_t k = 0; k < count; k++)
{
if ((k % octetGrouping) == 0) {
fmt::print(out, " ");
}
fmt::print(out, "{:02x}", static_cast<uint8_t>(head[k]));
}
for(size_t k = count; k < rowWidth; k++)
{
if ((k % octetGrouping) == 0) {
fmt::print(out, " ");
}
fmt::print(out, "..");
}

fmt::print(out, " | ");
for(size_t k = 0; k < count; k++)
{
char c = head[k];
if (std::isprint(c))
{
fmt::print(out, "{}", c);
}
else
{
fmt::print(out, ".");
}
}

out << '\n';

address += rowWidth;
head += rowWidth;
};

// foreach row
for(size_t i = 0; i < rowCount; i++)
{
printRow(rowWidth);

}

// remainder
const size_t remainder = (static_cast<size_t>(blob.size()) % rowWidth);
if (remainder > 0)
{
printRow(remainder);
}

return out;
}

0 comments on commit b8a8f4f

Please sign in to comment.