[ox] Add file:line error tracing

This commit is contained in:
2018-05-31 22:45:57 -05:00
parent 956415a6a2
commit ea7cf59ec7
17 changed files with 438 additions and 215 deletions

View File

@@ -21,6 +21,7 @@ install(
assert.hpp
bitops.hpp
byteswap.hpp
error.hpp
math.hpp
memops.hpp
new.hpp

View File

@@ -33,11 +33,11 @@ void _assert<Error>([[maybe_unused]]const char *file, [[maybe_unused]]int line,
if (err) {
auto ei = ErrorInfo(err);
std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << msg << '\n';
std::cerr << "\tError Info: " << ei.errCode;
std::cerr << "\tError Code:\t" << ei.errCode << '\n';
if (ei.file != nullptr) {
std::cerr << " (" << reinterpret_cast<const char*>(ei.file) << ':' << ei.line << ')';
std::cerr << "\tError Location:\t" << reinterpret_cast<const char*>(ei.file) << ':' << ei.line << '\n';
}
std::cerr << std::endl;
std::cerr << std::flush;
std::abort();
}
#endif

View File

@@ -14,7 +14,7 @@
#include <ox/__buildinfo/defines.hpp>
#include "types.hpp"
#include "error.hpp"
namespace ox {

74
deps/ox/src/ox/std/error.hpp vendored Normal file
View File

@@ -0,0 +1,74 @@
/*
* Copyright 2015 - 2018 gtalent2@gmail.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "types.hpp"
#ifdef DEBUG
#define OxError(x) x ? reinterpret_cast<uint64_t>(__FILE__) | _errorTags(__LINE__, x) : 0
#else
#define OxError(x) x
#endif
namespace ox {
using Error = uint64_t;
constexpr Error errCode(Error err) {
return (err >> 58) & onMask<Error>(5);
}
struct ErrorInfo {
const char *file = nullptr;
int line = -1;
Error errCode = 0;
ErrorInfo() = default;
ErrorInfo(Error err) {
this->file = reinterpret_cast<const char*>(err & onMask<Error>(48));
this->line = static_cast<int>((err >> 48) & onMask<Error>(11));
this->errCode = ox::errCode(err);
}
};
static constexpr Error _errorTags(Error line, Error errCode) {
line &= onMask<Error>(11);
line <<= 48;
errCode &= onMask<Error>(5);
errCode <<= 59;
return errCode | line;
}
template<typename T>
struct ValErr {
T value;
Error error;
inline constexpr ValErr() = default;
inline constexpr ValErr(T value, Error error = 0): value(value), error(error) {
}
inline constexpr operator const T&() const {
return value;
}
inline constexpr operator T&() {
return value;
}
inline constexpr bool ok() const {
return error == 0;
}
};
}

View File

@@ -13,17 +13,17 @@
namespace ox {
template<typename T>
inline const T &min(const T &a, const T &b) {
inline constexpr const T &min(const T &a, const T &b) {
return a < b ? a : b;
}
template<typename T>
inline const T &max(const T &a, const T &b) {
inline constexpr const T &max(const T &a, const T &b) {
return a > b ? a : b;
}
template<typename I>
inline I pow(I v, int e) {
inline constexpr I pow(I v, int e) {
I out = 1;
for (I i = 0; i < e; i++) {
out *= v;

View File

@@ -11,6 +11,7 @@
#include "assert.hpp"
#include "bitops.hpp"
#include "byteswap.hpp"
#include "error.hpp"
#include "math.hpp"
#include "memops.hpp"
#include "new.hpp"

View File

@@ -52,68 +52,6 @@ typedef uint32_t uintptr_t;
namespace ox {
using Error = uint64_t;
struct ErrorInfo {
const char *file = nullptr;
int line = -1;
Error errCode = 0;
ErrorInfo() = default;
ErrorInfo(Error err) {
this->file = reinterpret_cast<const char*>(err & onMask<Error>(48));
this->line = static_cast<int>((err >> 48) & onMask<Error>(11));
this->errCode = (err >> 58) & onMask<Error>(5);
}
};
constexpr Error ErrorTags(Error line, Error errCode) {
line &= onMask<Error>(11);
line <<= 48;
errCode &= onMask<Error>(5);
errCode <<= 59;
return errCode | line;
}
}
#ifdef DEBUG
#define OxError(x) x ? reinterpret_cast<uint64_t>(__FILE__) | ErrorTags(__LINE__, x) : 0
#else
#define OxError(x) x
#endif
namespace ox {
template<typename T>
struct ValErr {
T value;
Error error;
inline constexpr ValErr() = default;
inline constexpr ValErr(T value, Error error = 0): value(value), error(error) {
}
inline constexpr operator const T&() const {
return value;
}
inline constexpr operator T&() {
return value;
}
inline constexpr bool ok() const {
return error == 0;
}
};
}
namespace std {
using nullptr_t = decltype(nullptr);