uvgrtp-base/include/debug.hh

99 lines
2.5 KiB
C++
Raw Normal View History

2019-04-25 06:03:37 +00:00
#pragma once
2019-08-09 05:08:55 +00:00
#ifdef _WIN32
#include <winsock2.h>
#include <winsock.h>
#include <winbase.h>
#endif
2019-04-25 06:03:37 +00:00
#include <cstdio>
#include <cstdarg>
#include <cstring>
2019-05-17 06:18:48 +00:00
#include <string>
2019-04-25 06:03:37 +00:00
// TODO constexpr??
inline const char *className(const std::string& prettyFunction)
{
size_t colons = prettyFunction.find("::");
if (colons == std::string::npos)
return "";
size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
size_t end = colons - begin;
return prettyFunction.substr(begin,end).c_str();
}
2019-07-16 07:37:18 +00:00
#ifdef _WIN32
static inline void win_get_last_error(void)
{
wchar_t *s = NULL;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&s, 0, NULL
);
fprintf(stderr, "%S %d\n", s, WSAGetLastError());
LocalFree(s);
}
2019-07-16 07:37:18 +00:00
#define LOG_LEVEL_ERROR "ERROR"
#define LOG_LEVEL_WARN "WARNING"
#define LOG_LEVEL_INFO "INFO"
#else
2019-04-25 06:03:37 +00:00
#define LOG_LEVEL_ERROR "\x1b[31mERROR\x1b[0m"
#define LOG_LEVEL_WARN "\x1b[33mWARNING\x1b[0m"
#define LOG_LEVEL_INFO "\x1b[34mINFO\x1b[0m"
2019-07-16 07:37:18 +00:00
#endif
2019-04-25 06:03:37 +00:00
#define LOG_LEVEL_DEBUG "DEBUG"
#define uvgrtp_debug(level, fmt, ...) \
2019-04-25 06:03:37 +00:00
fprintf(stderr, "[RTPLIB][%s][%s::%s] " fmt "\n", level, \
2020-02-14 12:24:42 +00:00
"", __func__, ##__VA_ARGS__)
2019-04-25 06:03:37 +00:00
2019-06-12 08:19:43 +00:00
#ifndef NDEBUG
#define LOG_DEBUG(fmt, ...) uvgrtp_debug(LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
2019-04-25 06:03:37 +00:00
#else
#define LOG_DEBUG(fmt, ...) ;
#endif
#ifdef __RTP_SILENT__
#define LOG_ERROR(fmt, ...) ;
#define LOG_WARN(fmt, ...) ;
#define LOG_INFO(fmt, ...) ;
#undef LOG_DEBUG
#define LOG_DEBUG(fmt, ...) ;
#else
#define LOG_ERROR(fmt, ...) uvgrtp_debug(LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
#define LOG_WARN(fmt, ...) uvgrtp_debug(LOG_LEVEL_WARN, fmt, ##__VA_ARGS__)
#define LOG_INFO(fmt, ...) uvgrtp_debug(LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
2019-04-25 06:03:37 +00:00
#endif
static inline void log_platform_error(const char *aux)
{
#ifdef __linux__
if (aux) {
LOG_ERROR("%s: %s %d\n", aux, strerror(errno), errno);
} else {
LOG_ERROR("%s %d\n", strerror(errno), errno);
}
#else
wchar_t *s = NULL;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&s, 0, NULL
);
if (aux) {
LOG_ERROR("%s: %s %d\n", aux, s, WSAGetLastError());
} else {
LOG_ERROR("%s %d\n", s, WSAGetLastError());
}
LocalFree(s);
#endif
}