2024-03-23 18:41:26 -07:00
|
|
|
#include "log.hpp"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2024-04-28 12:31:40 -07:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
2024-03-23 18:41:26 -07:00
|
|
|
namespace Looper::Log {
|
|
|
|
std::set<FILE*> LogStream::global_outputs;
|
|
|
|
int LogStream::log_level = 0;
|
|
|
|
std::set<FILE*> LogStream::get_used_outputs() {
|
|
|
|
std::set<FILE*> used_outputs;
|
|
|
|
for (auto my_output : outputs) {
|
|
|
|
used_outputs.insert(my_output);
|
|
|
|
}
|
|
|
|
for (auto global_output : global_outputs) {
|
|
|
|
used_outputs.insert(global_output);
|
|
|
|
}
|
|
|
|
return used_outputs;
|
|
|
|
}
|
2024-04-28 12:31:40 -07:00
|
|
|
std::string line;
|
2024-03-23 18:41:26 -07:00
|
|
|
void LogStream::writec(const char chr) {
|
|
|
|
bool is_newline = (chr == '\n' || chr == '\r');
|
|
|
|
if (my_log_level < log_level) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
writeprefix();
|
|
|
|
if (nested) {
|
|
|
|
for (auto &stream : streams) {
|
|
|
|
stream->writec(chr);
|
|
|
|
}
|
|
|
|
} else {
|
2024-04-28 12:31:40 -07:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
if (!is_newline) {
|
|
|
|
line += chr;
|
|
|
|
} else {
|
|
|
|
for (auto logPriority : android_outputs) {
|
|
|
|
__android_log_print(logPriority, "Looper", "%s", line.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2024-03-23 18:41:26 -07:00
|
|
|
std::set<FILE*> used_outputs = get_used_outputs();
|
|
|
|
for (auto &file : used_outputs) {
|
|
|
|
fwrite(&chr, 1, 1, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_newline) {
|
|
|
|
need_prefix = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LogStream::writeprefix() {
|
|
|
|
if (need_prefix) {
|
|
|
|
need_prefix = false;
|
|
|
|
for (auto name : names) {
|
|
|
|
writec('[');
|
|
|
|
writes(name);
|
|
|
|
writes("] ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LogStream::writes(const char *msg) {
|
|
|
|
while (*msg != '\0') {
|
|
|
|
writec(*(msg++));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LogStream::writesn(const char *msg, size_t n) {
|
|
|
|
for (size_t i = 0; i < n && msg[i] != '\0'; i++) {
|
|
|
|
writec(msg[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LogStream::writes(std::string msg) {
|
|
|
|
LogStream::writesn(msg.c_str(), msg.size());
|
|
|
|
}
|
|
|
|
void LogStream::writeln(const char *msg) {
|
|
|
|
LogStream::writes(msg);
|
|
|
|
LogStream::writec('\n');
|
|
|
|
}
|
|
|
|
void LogStream::writeln_n(const char *msg, size_t n) {
|
|
|
|
LogStream::writesn(msg, n);
|
|
|
|
LogStream::writec('\n');
|
|
|
|
}
|
|
|
|
void LogStream::writeln(std::string msg) {
|
|
|
|
LogStream::writes(msg);
|
|
|
|
LogStream::writec('\n');
|
|
|
|
}
|
|
|
|
void LogStream::writef(const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vwritef(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
void LogStream::vwritef(const char *fmt, va_list args) {
|
|
|
|
va_list args_backup;
|
|
|
|
va_copy(args_backup, args);
|
|
|
|
size_t n = vsnprintf(NULL, 0, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
va_copy(args, args_backup);
|
|
|
|
size_t bufsize = n + 1;
|
|
|
|
char *buf = (char*)malloc(bufsize);
|
|
|
|
memset(buf, 0, bufsize);
|
|
|
|
if (buf == NULL) {
|
|
|
|
va_end(args);
|
|
|
|
va_end(args_backup);
|
|
|
|
throw std::exception();
|
|
|
|
}
|
|
|
|
n = vsnprintf(buf, bufsize, fmt, args);
|
|
|
|
va_end(args_backup);
|
|
|
|
LogStream::writes(buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
void LogStream::vwritefln(const char *fmt, va_list args) {
|
|
|
|
vwritef(fmt, args);
|
|
|
|
writec('\n');
|
|
|
|
}
|
|
|
|
void LogStream::writefln(const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vwritefln(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
2024-04-28 12:31:40 -07:00
|
|
|
LogStream::LogStream(std::initializer_list<std::string> names, int log_level, bool nested, void *discriminator)
|
2024-03-23 18:41:26 -07:00
|
|
|
: names(names),
|
|
|
|
need_prefix(true),
|
|
|
|
my_log_level(log_level),
|
|
|
|
nested(nested)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
LogStream::LogStream(std::initializer_list<std::string> names, std::initializer_list<LogStream*> streams, int log_level)
|
2024-04-28 12:31:40 -07:00
|
|
|
: LogStream(names, log_level, true, nullptr)
|
2024-03-23 18:41:26 -07:00
|
|
|
{
|
|
|
|
this->streams = std::set(streams);
|
|
|
|
}
|
2024-04-28 12:31:40 -07:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
LogStream::LogStream(std::initializer_list<std::string> names, std::initializer_list<std::variant<FILE*, android_LogPriority>> outputs, int log_level)
|
|
|
|
#else
|
|
|
|
LogStream::LogStream(std::initializer_list<std::string> names, std::initializer_list<FILE*> outputs, int log_level)
|
|
|
|
#endif
|
|
|
|
: LogStream(names, log_level, false, nullptr)
|
2024-03-23 18:41:26 -07:00
|
|
|
{
|
2024-04-28 12:31:40 -07:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
std::set<FILE*> file_outputs;
|
|
|
|
std::set<android_LogPriority> android_outputs;
|
|
|
|
for (auto output : outputs) {
|
|
|
|
android_LogPriority *logPriority = std::get_if<android_LogPriority>(&output);
|
|
|
|
FILE **file = std::get_if<FILE*>(&output);
|
|
|
|
if (logPriority != nullptr) {
|
|
|
|
android_outputs.insert(*logPriority);
|
|
|
|
} else if (file != nullptr){
|
|
|
|
file_outputs.insert(*file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->android_outputs = android_outputs;
|
|
|
|
this->outputs = file_outputs;
|
|
|
|
#else
|
|
|
|
|
2024-03-23 18:41:26 -07:00
|
|
|
this->outputs = std::set(outputs);
|
2024-04-28 12:31:40 -07:00
|
|
|
#endif
|
2024-03-23 18:41:26 -07:00
|
|
|
}
|
|
|
|
static LogStream *debug_stream;
|
|
|
|
static LogStream *info_stream;
|
|
|
|
static LogStream *warning_stream;
|
|
|
|
static LogStream *error_stream;
|
|
|
|
void init_logging() {
|
2024-04-28 12:31:40 -07:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
debug_stream = new LogStream({"DEBUG"}, {ANDROID_LOG_DEBUG}, -1);
|
|
|
|
info_stream = new LogStream({"INFO"}, {ANDROID_LOG_INFO}, 0);
|
|
|
|
warning_stream = new LogStream({"WARNING"}, {ANDROID_LOG_WARN}, 1);
|
|
|
|
error_stream = new LogStream({"ERROR"}, {ANDROID_LOG_ERROR}, 2);
|
|
|
|
#else
|
2024-03-23 18:41:26 -07:00
|
|
|
debug_stream = new LogStream({"DEBUG"}, {stderr}, -1);
|
|
|
|
info_stream = new LogStream({"INFO"}, {stdout}, 0);
|
|
|
|
warning_stream = new LogStream({"WARNING"}, {stderr}, 1);
|
|
|
|
error_stream = new LogStream({"ERROR"}, {stderr}, 2);
|
2024-04-28 12:31:40 -07:00
|
|
|
#endif
|
2024-03-23 18:41:26 -07:00
|
|
|
}
|
|
|
|
LogStream &get_log_stream_by_level(int level) {
|
|
|
|
switch (level) {
|
|
|
|
case -1: {
|
|
|
|
return *debug_stream;
|
|
|
|
} break;
|
|
|
|
case 0: {
|
|
|
|
return *info_stream;
|
|
|
|
} break;
|
|
|
|
case 1: {
|
|
|
|
return *warning_stream;
|
|
|
|
} break;
|
|
|
|
case 2: {
|
|
|
|
return *error_stream;
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
return *info_stream;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|