Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ branches:
protection:
required_status_checks:
contexts:
- bazel-opt
- Codacy Static Code Analysis
- code-review/reviewable
80 changes: 31 additions & 49 deletions cpp/src/util/logging.h
Original file line number Diff line number Diff line change
@@ -1,83 +1,65 @@
#pragma once

#include <array>
#include <cstdint>
#include <ostream>
#include <vector>


struct scope_counter
{
struct scope_counter {
static std::size_t scope;

scope_counter ()
{ scope++; }
scope_counter() { scope++; }

~scope_counter ()
{ scope--; }
~scope_counter() { scope--; }

scope_counter (scope_counter const &rhs) = delete;
scope_counter(scope_counter const &rhs) = delete;
};

std::ostream &scope_indent (std::ostream &os, int line);
std::ostream &scope_indent(std::ostream &os, int line);

#define SCOPE scope_counter const _scope


#ifdef HAVE_GLOG
# include <glog/logging.h>
# undef LOG
# define LOG(KIND) scope_indent (COMPACT_GOOGLE_LOG_##KIND.stream(), __LINE__)
#include <glog/logging.h>
#undef LOG
#define LOG(KIND) scope_indent(COMPACT_GOOGLE_LOG_##KIND.stream(), __LINE__)
#else
namespace google
{
inline void InitGoogleLogging (char const *) { }
inline void ShutdownGoogleLogging () { }
inline void LogToStderr () { }
}
namespace google {
inline void InitGoogleLogging(char const *) {}
inline void ShutdownGoogleLogging() {}
inline void LogToStderr() {}
} // namespace google

struct null_ostream
: std::ostream
{
};
struct null_ostream : std::ostream {};

# define LOG(LEVEL) (null_ostream ())
# define LOG_ASSERT(cond) assert (cond)
#define LOG(LEVEL) (null_ostream())
#define LOG_ASSERT(cond) assert(cond)
#endif

void output_hex (std::ostream &os, uint8_t const *data, size_t length);

void output_hex(std::ostream &os, uint8_t const *data, size_t length);

struct formatter
{
formatter (formatter &&fmt)
: text_ (std::move (fmt.text_))
{ }
struct formatter {
formatter(formatter &&fmt) : text_(std::move(fmt.text_)) {}

explicit formatter (std::vector<char> text)
: text_ (std::move (text))
{ }
explicit formatter(std::vector<char> text) : text_(std::move(text)) {}

friend std::ostream &operator << (std::ostream &os, formatter const &fmt);
friend std::ostream &operator<<(std::ostream &os, formatter const &fmt);

private:
std::vector<char> const text_;
};

template <size_t N, typename... Args>
formatter format(char const (&fmt)[N], Args const &...args) {
std::vector<char> text(snprintf(nullptr, 0, fmt, args...) + 1);
snprintf(text.data(), text.size(), fmt, args...);

template<size_t N, typename ...Args>
formatter
format (char const (&fmt)[N], Args const &...args)
{
std::vector<char> text (snprintf (nullptr, 0, fmt, args...) + 1);
snprintf (text.data (), text.size (), fmt, args...);

return formatter (std::move (text));
return formatter(std::move(text));
}

template<std::size_t N>
std::ostream &
operator << (std::ostream &os, std::array<uint8_t, N> const &array)
{
output_hex (os, array.data (), array.size ());
template <std::size_t N>
std::ostream &operator<<(std::ostream &os,
std::array<uint8_t, N> const &array) {
output_hex(os, array.data(), array.size());
return os;
}