[ox/std] Add support for explicity printing to stdout and stderr

This commit is contained in:
Gary Talent 2021-04-16 21:18:31 -05:00
parent 022bc7e512
commit 86a38c7197
6 changed files with 42 additions and 27 deletions

View File

@ -8,12 +8,21 @@
#pragma once
#include <ox/std/string.hpp>
#include <ox/std/strops.hpp>
#include <ox/std/types.hpp>
#include <ox/std/typetraits.hpp>
namespace ox::detail {
constexpr const char *stringify(const char *s) noexcept {
return s;
}
constexpr const char *stringify(const ox::String &s) noexcept {
return s.c_str();
}
class FmtArg {
private:
@ -29,7 +38,7 @@ class FmtArg {
} else if constexpr(is_integral_v<T>) {
out = ox_itoa(v, dataStr);
} else {
out = v;
out = stringify(v);
}
}

View File

@ -177,15 +177,6 @@ char &String::operator[](std::size_t i) noexcept {
return m_buff[i];
}
char *String::data() noexcept {
return m_buff.data();
}
const char *String::c_str() const noexcept {
return static_cast<const char*>(m_buff.data());
}
std::size_t String::len() const noexcept {
std::size_t length = 0;
for (std::size_t i = 0; i < m_buff.size(); i++) {

View File

@ -82,12 +82,19 @@ class String {
char &operator[](std::size_t i) noexcept;
[[nodiscard]] char *data() noexcept;
[[nodiscard]]
constexpr char *data() noexcept {
return m_buff.data();
}
[[nodiscard]] const char *c_str() const noexcept;
[[nodiscard]]
constexpr const char *c_str() const noexcept {
return static_cast<const char*>(m_buff.data());
}
#ifdef OX_USE_STDLIB
[[nodiscard]] inline std::string toStdString() const {
[[nodiscard]]
inline std::string toStdString() const {
return c_str();
}
#endif
@ -95,12 +102,14 @@ class String {
/**
* Returns the number of characters in this string.
*/
[[nodiscard]] std::size_t len() const noexcept;
[[nodiscard]]
std::size_t len() const noexcept;
/**
* Returns the number of bytes used for this string.
*/
[[nodiscard]] std::size_t bytes() const noexcept;
[[nodiscard]]
std::size_t bytes() const noexcept;
};

View File

@ -167,12 +167,18 @@ void init();
#define oxLogError(err) ox::trace::logError(__FILE__, __LINE__, err)
#define oxTrace(...) ox::trace::TraceStream(__FILE__, __LINE__, __VA_ARGS__)
#define oxOut(...) ox::trace::TraceStream(__FILE__, __LINE__, "stdout", __VA_ARGS__)
#define oxErr(...) ox::trace::TraceStream(__FILE__, __LINE__, "stderr", __VA_ARGS__)
#ifdef OX_USE_STDLIB
// Non-GCC compilers don't like ##__VA_ARGS__, so use initializer list, which relies on std lib
#define oxTracef(ch, fmt, ...) ox::trace::TraceStream(__FILE__, __LINE__, ch, ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#define oxOutf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stdout", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#define oxErrf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stderr", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#else
#define oxTracef(ch, fmt, ...) ox::trace::TraceStream(__FILE__, __LINE__, ch, ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), ##__VA_ARGS__)
#define oxOutf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stdout", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), ##__VA_ARGS__)
#define oxErrf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stderr", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), ##__VA_ARGS__)
#endif
#define oxInfo(...) oxTrace("info", __VA_ARGS__)

View File

@ -31,6 +31,10 @@ void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line,
std::cout << " " << file << ':' << line << "\n";
} else if (ox_strcmp(ch, "debug") == 0 || ox_strcmp(ch, "info") == 0) {
std::cout << msg << '\n';
} else if (ox_strcmp(ch, "stdout") == 0) {
std::cout << msg;
} else if (ox_strcmp(ch, "stderr") == 0) {
std::cerr << msg << '\n';
} else if (ox_strcmp(ch, "error") == 0) {
std::cerr << "\033[31;1;1mERROR:\033[0m (" << file << ':' << line << "): " << msg << '\n';
}

View File

@ -60,9 +60,15 @@ class Vector {
void resize(std::size_t size);
[[nodiscard]] T *data() noexcept;
[[nodiscard]]
constexpr T *data() noexcept {
return m_items;
}
[[nodiscard]] const T *data() const noexcept;
[[nodiscard]]
constexpr const T *data() const noexcept {
return m_items;
}
[[nodiscard]] bool contains(T) const;
@ -230,16 +236,6 @@ void Vector<T>::resize(std::size_t size) {
m_size = size;
}
template<typename T>
T *Vector<T>::data() noexcept {
return m_items;
}
template<typename T>
const T *Vector<T>::data() const noexcept {
return m_items;
}
template<typename T>
bool Vector<T>::contains(T v) const {
for (std::size_t i = 0; i < m_size; i++) {