Skip to content

Commit

Permalink
Merge pull request #8 from mkn/pr
Browse files Browse the repository at this point in the history
KLOG >=3 disabled with -DNDEBUG / + formatting
  • Loading branch information
PhilipDeegan authored May 16, 2020
2 parents b76e74c + d56fb6e commit 928fa3f
Show file tree
Hide file tree
Showing 44 changed files with 309 additions and 309 deletions.
18 changes: 8 additions & 10 deletions inc/kul/asio/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,23 @@ class Message {
}
};
class LogMessage : public Message {
private:
const char *f;
const char *fn;
const uint16_t &l;

public:
~LogMessage() { LogMan::INSTANCE().log(f, fn, l, m, ss.str()); }
LogMessage(const char *f, const char *fn, const uint16_t &l, const kul::log::mode &m)
: Message(m), f(f), fn(fn), l(l) {}

private:
const char *f, *fn;
const uint16_t &l;
};
class OutMessage : public Message {
public:
~OutMessage() { LogMan::INSTANCE().out(m, ss.str()); }
struct OutMessage : public Message {
OutMessage(const kul::log::mode &m = kul::log::mode::NON) : Message(m) {}
~OutMessage() { LogMan::INSTANCE().out(m, ss.str()); }
};
class ErrMessage : public Message {
public:
~ErrMessage() { LogMan::INSTANCE().err(ss.str()); }
struct ErrMessage : public Message {
ErrMessage() : Message(kul::log::mode::ERR) {}
~ErrMessage() { LogMan::INSTANCE().err(ss.str()); }
};

#define KASIO_LOG_INF kul::asio::LogMessage(__FILE__, __func__, __LINE__, kul::log::mode::INF)
Expand Down
21 changes: 11 additions & 10 deletions inc/kul/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,32 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "kul/signal.hpp"

namespace kul{
struct Assert{
namespace kul {
struct Assert {
template <typename T>
Assert(T t) {
#if !defined(NDEBUG)
if(!(t)) {
if (!(t)) {
auto tid = kul::this_thread::id();
KOUT(NON) << tid << " : Stacktrace:";
for(auto const& s : kul::this_thread::stacktrace()) KOUT(NON) << tid << " : " << s;
for (auto const& s : kul::this_thread::stacktrace()) KOUT(NON) << tid << " : " << s;
exit(111);
}
#endif
}
};
}
} // namespace kul

#if !defined(KASSERT)
#define KASSERT(b) kul::Assert{(b)}
#define KASSERT(b) \
kul::Assert { (b) }
#endif

#if defined(KASSERT_REPLACE_ASSERT)
#ifdef assert
#undef assert
#endif
#define assert(b) KASSERT(b)
#ifdef assert
#undef assert
#endif
#define assert(b) KASSERT(b)
#endif

#endif /* _KUL_ASSERT_HPP_ */
9 changes: 4 additions & 5 deletions inc/kul/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <vector>

#include "kul/env.hpp"
#include "kul/except.hpp"
#include "kul/map.hpp"
#include "kul/os/cli.hpp"
#include "kul/except.hpp"
#include "kul/serial.hpp"
#include "kul/string.hpp"

Expand Down Expand Up @@ -112,21 +112,20 @@ class EnvVar {
enum ArgType { FLAG = 0, STRING, MAYBE };

class Arg : public Cmd {
static constexpr char INVALID_CHAR = ' ';
static constexpr char INVALID_CHAR = ' ';

public:
Arg(const char d, char const *dd, ArgType t, bool m = false) : Cmd(dd), man(m), d(d), t(t) {}
Arg(const char d, char const *dd, bool m = false) : Cmd(dd), man(m), d(d) {}

Arg(char const *dd, ArgType t, bool m = false) : Cmd(dd), man(m), t(t) {}
Arg(char const *dd, bool m = false) : Cmd(dd), man(m){}
Arg(char const *dd, bool m = false) : Cmd(dd), man(m) {}

bool mandatory() const { return man; }
char dash() const { return d; }
char const *dashdash() const { return command(); }
const ArgType &type() const { return t; }


private:
bool man;
const char d = ' ';
Expand Down Expand Up @@ -262,6 +261,7 @@ class Args {
std::vector<Cmd> cmds;
std::vector<Arg> args;
hash::map::S2S vals;

public:
#include "kul/serial/cli.arg.end.hpp"
};
Expand All @@ -283,5 +283,4 @@ inline std::vector<std::string> asArgs(const std::string &cmd) {

#include "kul/serial/cli.bottom.hpp"


#endif /* _KUL_CLI_HPP_ */
2 changes: 1 addition & 1 deletion inc/kul/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "kul/defs.hpp"

#if KUL_IS_NIX
#if KUL_IS_NIX
#include "kul/os/nix/cpu.hpp"
#elif KUL_IS_BSD
#include "kul/os/bsd/cpu.hpp"
Expand Down
27 changes: 14 additions & 13 deletions inc/kul/dbg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace kul {
namespace dbg {

class StackTrace{
class StackTrace {
private:
std::vector<std::string> _stk;
std::vector<std::string> _stk;

public:
StackTrace() = delete;
StackTrace(const StackTrace &) = delete;
StackTrace &operator=(const StackTrace &) = delete;
StackTrace &operator=(const StackTrace &&) = delete;
StackTrace(const std::string &s) { _stk.emplace_back(s); }
StackTrace(const StackTrace &&that) {
if(this != &that) this->_stk = that._stk;
}
const std::vector<std::string> &stack() const { return _stk; }
StackTrace() = delete;
StackTrace(const StackTrace &) = delete;
StackTrace &operator=(const StackTrace &) = delete;
StackTrace &operator=(const StackTrace &&) = delete;
StackTrace(const std::string &s) { _stk.emplace_back(s); }
StackTrace(const StackTrace &&that) {
if (this != &that) this->_stk = that._stk;
}
const std::vector<std::string> &stack() const { return _stk; }
};

#if !defined(NDEBUG) || defined(KUL_FORCE_TRACE)
Expand All @@ -69,10 +70,10 @@ class StackTrace{
#endif // KUL_DBG_FUNC_ENTER

#ifndef KUL_DBG_FUNC_ON_ENTER
#define KUL_DBG_FUNC_ON_ENTER \
#define KUL_DBG_FUNC_ON_ENTER \
KOUT(TRC) << kul::LogMan::INSTANCE().str(m_fi, m_fu, m_li, kul::log::mode::TRC, "", \
"[%M]: %T - %D : %F:%L fn(%N)") \
<< " - ENTERED";
<< " - ENTERED";
#endif // KUL_DBG_FUNC_ON_ENTER

#ifndef KUL_DBG_FUNC_ON_EXIT
Expand Down
2 changes: 1 addition & 1 deletion inc/kul/defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define KUL_PRIVATE
#endif

#if defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__)
#if defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__)
#define KUL_IS_BSD 1
#endif

Expand Down
2 changes: 1 addition & 1 deletion inc/kul/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace kul {
namespace env {

class Var {
class Var {
public:
enum Mode { APPE = 0, PREP, REPL };

Expand Down
17 changes: 6 additions & 11 deletions inc/kul/except.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef _KUL_EXCEPT_HPP_
#define _KUL_EXCEPT_HPP_

#include <sstream>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include "kul/defs.hpp"
Expand Down Expand Up @@ -61,11 +61,9 @@ class Exception : public std::runtime_error {
Exception(const char *f, const uint16_t &l, const std::string &s = "")
: std::runtime_error(s), _f(f), _l(l), _ep(std::current_exception()), err(s) {}
Exception(Exception const &e)
: std::runtime_error(e), _f(e.file()), _l(e.line()), _ep(e._ep), err(e.err) {
}
: std::runtime_error(e), _f(e.file()), _l(e.line()), _ep(e._ep), err(e.err) {}
Exception(Exception const &&e)
: std::runtime_error(e), _f(e.file()), _l(e.line()), _ep(e._ep), err(e.err) {
}
: std::runtime_error(e), _f(e.file()), _l(e.line()), _ep(e._ep), err(e.err) {}
virtual ~Exception() KNOTHROW {}

std::string debug() const {
Expand All @@ -74,12 +72,8 @@ class Exception : public std::runtime_error {
return ss.str();
}

const char* what() const noexcept override {
return err.c_str();
}
std::string str() const noexcept {
return err;
}
const char *what() const noexcept override { return err.c_str(); }
std::string str() const noexcept { return err; }

const char *file() const { return _f; }
const uint16_t &line() const { return _l; }
Expand Down Expand Up @@ -107,6 +101,7 @@ class Exception : public std::runtime_error {
err += msg.str();
return *this;
}

protected:
const char *_f;
const uint16_t _l;
Expand Down
20 changes: 9 additions & 11 deletions inc/kul/for.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,17 @@ namespace kul {
/*
struct Apply {
template<size_t i>
decltype(auto) operator()(){}
constexpr decltype(auto) operator()(){}
};*/
template<typename Apply, size_t... Is>
constexpr decltype(auto) for_N(Apply& f, std::integer_sequence<size_t, Is...> const&)
{
if constexpr (!std::is_same_v<decltype(f.template operator()<0>()), void>)
return std::make_tuple(f.template operator()<Is>()...);
(f.template operator()<Is>(), ...);
template <typename Apply, size_t... Is>
constexpr decltype(auto) for_N(Apply& f, std::integer_sequence<size_t, Is...> const&) {
if constexpr (!std::is_same_v<decltype(f.template operator()<0>()), void>)
return std::make_tuple(f.template operator()<Is>()...);
(f.template operator()<Is>(), ...);
}
template<size_t T, typename Apply>
constexpr decltype(auto) for_N(Apply&& f)
{
return for_N(f, std::make_integer_sequence<size_t, T>{});
template <size_t T, typename Apply>
constexpr decltype(auto) for_N(Apply&& f) {
return for_N(f, std::make_integer_sequence<size_t, T>{});
}

} // namespace kul
Expand Down
2 changes: 1 addition & 1 deletion inc/kul/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class AReader {
std::vector<char> v;
v.resize(l);
_f.read(&v[0], l);
v.resize((size_t) _f.gcount());
v.resize((size_t)_f.gcount());
s1 = std::string(v.begin(), v.end());
std::strcpy(c, s1.c_str());
return s1.size();
Expand Down
58 changes: 47 additions & 11 deletions inc/kul/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,35 @@ class Message {
}
};
class LogMessage : public Message {
public:
LogMessage(const char *_f, const char *_fn, const uint16_t &_l, const log::mode &_m)
: Message(_m), f(_f), fn(_fn), l(_l) {}
~LogMessage() { LogMan::INSTANCE().log(f, fn, l, m, ss.str()); }

private:
const char *f;
const char *fn;
const char *f, *fn;
const uint16_t &l;

};
class DBgMessage : public Message {
public:
~LogMessage() { LogMan::INSTANCE().log(f, fn, l, m, ss.str()); }
LogMessage(const char *_f, const char *_fn, const uint16_t &_l, const log::mode &_m)
~DBgMessage() {
#if !defined(NDEBUG)
LogMan::INSTANCE().log(f, fn, l, m, ss.str());
#endif
}
DBgMessage(const char *_f, const char *_fn, const uint16_t &_l, const log::mode &_m)
: Message(_m), f(_f), fn(_fn), l(_l) {}
template <class T>
DBgMessage &operator<<(const T &s) {
#if !defined(NDEBUG)
ss << s;
#endif
return *this;
}

private:
const char *f, *fn;
const uint16_t &l;
};
class OutMessage : public Message {
public:
Expand All @@ -212,21 +232,37 @@ class ErrMessage : public Message {
~ErrMessage() { LogMan::INSTANCE().err(ss.str()); }
ErrMessage() : Message(log::mode::ERR) {}
};
class DBoMessage : public Message {
public:
~DBoMessage() {
#if !defined(NDEBUG)
LogMan::INSTANCE().out(m, ss.str());
#endif
}
DBoMessage(const log::mode &_m = kul::log::mode::NON) : Message(_m) {}
template <class T>
DBoMessage &operator<<(const T &s) {
#if !defined(NDEBUG)
ss << s;
#endif
return *this;
}
};

#define KLOG_NON kul::LogMessage(__FILE__, __func__, __LINE__, kul::log::mode::NON)
#define KLOG_INF kul::LogMessage(__FILE__, __func__, __LINE__, kul::log::mode::INF)
#define KLOG_ERR kul::LogMessage(__FILE__, __func__, __LINE__, kul::log::mode::ERR)
#define KLOG_DBG kul::LogMessage(__FILE__, __func__, __LINE__, kul::log::mode::DBG)
#define KLOG_OTH kul::LogMessage(__FILE__, __func__, __LINE__, kul::log::mode::OTH)
#define KLOG_TRC kul::LogMessage(__FILE__, __func__, __LINE__, kul::log::mode::TRC)
#define KLOG_DBG kul::DBgMessage(__FILE__, __func__, __LINE__, kul::log::mode::DBG)
#define KLOG_OTH kul::DBgMessage(__FILE__, __func__, __LINE__, kul::log::mode::OTH)
#define KLOG_TRC kul::DBgMessage(__FILE__, __func__, __LINE__, kul::log::mode::TRC)
#define KLOG(sev) KLOG_##sev

#define KOUT_NON kul::OutMessage()
#define KOUT_INF kul::OutMessage(kul::log::mode::INF)
#define KOUT_ERR kul::OutMessage(kul::log::mode::ERR)
#define KOUT_DBG kul::OutMessage(kul::log::mode::DBG)
#define KOUT_OTH kul::OutMessage(kul::log::mode::OTH)
#define KOUT_TRC kul::OutMessage(kul::log::mode::TRC)
#define KOUT_DBG kul::DBoMessage(kul::log::mode::DBG)
#define KOUT_OTH kul::DBoMessage(kul::log::mode::OTH)
#define KOUT_TRC kul::DBoMessage(kul::log::mode::TRC)
#define KOUT(sev) KOUT_##sev

#define KERR kul::ErrMessage()
Expand Down
Loading

0 comments on commit 928fa3f

Please sign in to comment.