5#include <linux/limits.h>
33std::string
Logger::log_printf(uint8_t level,
const char *prefix,
const char *file,
int line,
const char *format, ...) {
58 static char buffer[4096 + PATH_MAX];
64 va_start(args, format);
65 printf_size = vsnprintf(
nullptr, 0, format, args) + PATH_MAX;
70 if (printf_size >
sizeof (buffer)) {
71 ptr =
new char[printf_size + PATH_MAX + 1];
72 printf_size += (PATH_MAX + 1);
75 printf_size =
sizeof (buffer);
80 va_start(args, format);
81 VERIFY(vsnprintf(ptr, printf_size, format, args) >= 0);
85 size_t size = strlen(ptr);
89 if (ptr[size - 2] ==
'\r' && ptr[size - 1] ==
'\r') {
98 const char * file_name = strrchr(file,
'/');
99 snprintf(ptr, printf_size,
" (%s:%d)%s", ((file_name && *file_name ==
'/') ? file_name + 1 : file), line, nl ?
"\n" :
"");
102 snprintf(ptr, printf_size,
"%s", nl ?
"\n" :
"");
106 if (ptr != &buffer[0]) {
118 if (c >=
'0' && c <=
'9') {
121 if (c >=
'a' && c <=
'f') {
122 return c - 0x61 + 10;
124 if (c >=
'A' && c <=
'F') {
125 return c - 0x41 + 10;
127 LOG_RUNTIME(
"the symbol '%c' is not a hex digit!", c);
130size_t HexToBin(
const char * str, uint8_t * buf,
const size_t size) {
133 ASSERT((strlen(str) % 2) == 0);
135 memset(buf, 0, size);
138 size_t count = std::min(strlen(str), size * 2);
140 while (pos < count) {
147int HexToBinEq(
const char * str,
const uint8_t * buf,
size_t size) {
150 ASSERT(strlen(str) % 2 == 0);
152 size_t len = strlen(str);
153 if (len / 2 != size) {
156 for (
size_t pos = 0; pos < len; pos += 2) {
159 if (
byte != buf[pos / 2]) {
168std::string
BinToHex(
const uint8_t * buf,
const size_t size) {
172 for (
size_t i = 0; i < size; i++) {
181 for (
size_t pos = 0; pos < hex_str.size(); pos++) {
185 switch (hex_str[pos]) {
242 LOG_ERROR(
"Non hex symbol %c", hex_str[pos]);
249size_t BinToHexBuffer(
const uint8_t * buf,
const size_t size,
char * str,
const size_t str_size) {
252 if (!str || !buf || !size || !str_size) {
255 size_t max_size = std::min(size * 2, str_size - 1);
256 for (
size_t i = 0; i < size && (i * 2 < max_size); i++) {
260 str[max_size] =
'\0';
static Logger * m_instance
static std::string log_printf(uint8_t level, char const *prefix, char const *file, int line, char const *format,...)
LogLevelType GetLogLevel()
static std::string GetStackTrace()
static const char * GetLogLevelDesc(LogLevelType level)
static Logger * Instance()
static const char bin_to_hex_chars[]
__attribute__((weak)) std
size_t HexToBin(const char *str, uint8_t *buf, const size_t size)
size_t BinToHexBuffer(const uint8_t *buf, const size_t size, char *str, const size_t str_size)
std::string HexStrToBinStr(std::string &hex_str)
std::string BinToHex(const uint8_t *buf, const size_t size)
uint8_t HexToByte(const char c)
int HexToBinEq(const char *str, const uint8_t *buf, size_t size)
#define LOG_RUNTIME(format,...)
#define LOG_LEVEL_DEFAULT
#define ASSERT(condition)
#define LOG_LEVEL_WARNING