2024-03-23 18:41:26 -07:00
|
|
|
#include "log.hpp"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2024-05-02 14:52:11 -07:00
|
|
|
#include "util.hpp"
|
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 {
|
2024-08-08 13:12:37 -07:00
|
|
|
|
2024-03-23 18:41:26 -07:00
|
|
|
std::set<FILE*> LogStream::global_outputs;
|
2024-05-01 09:07:08 -07:00
|
|
|
int LogStream::log_level =
|
|
|
|
#ifdef DEBUG_MODE
|
|
|
|
-2;
|
|
|
|
#else
|
|
|
|
0;
|
|
|
|
#endif
|
2024-03-23 18:41:26 -07:00
|
|
|
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-08-08 13:12:37 -07:00
|
|
|
std::recursive_mutex log_mutex;
|
2024-03-23 18:41:26 -07:00
|
|
|
void LogStream::writec(const char chr) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
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() {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
if (need_prefix) {
|
|
|
|
need_prefix = false;
|
|
|
|
for (auto name : names) {
|
|
|
|
writec('[');
|
|
|
|
writes(name);
|
|
|
|
writes("] ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LogStream::writes(const char *msg) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
while (*msg != '\0') {
|
|
|
|
writec(*(msg++));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LogStream::writesn(const char *msg, size_t n) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
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) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
LogStream::writes(msg);
|
|
|
|
LogStream::writec('\n');
|
|
|
|
}
|
|
|
|
void LogStream::writeln_n(const char *msg, size_t n) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
LogStream::writesn(msg, n);
|
|
|
|
LogStream::writec('\n');
|
|
|
|
}
|
|
|
|
void LogStream::writeln(std::string msg) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
LogStream::writes(msg);
|
|
|
|
LogStream::writec('\n');
|
|
|
|
}
|
|
|
|
void LogStream::writef(const char *fmt, ...) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vwritef(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
void LogStream::vwritef(const char *fmt, va_list args) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-05-02 14:52:11 -07:00
|
|
|
const char *buf = vcformat(fmt, args);
|
2024-03-23 18:41:26 -07:00
|
|
|
va_end(args);
|
|
|
|
if (buf == NULL) {
|
|
|
|
throw std::exception();
|
|
|
|
}
|
|
|
|
LogStream::writes(buf);
|
2024-05-02 14:52:11 -07:00
|
|
|
free((void*)buf);
|
2024-03-23 18:41:26 -07:00
|
|
|
}
|
|
|
|
void LogStream::vwritefln(const char *fmt, va_list args) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
vwritef(fmt, args);
|
|
|
|
writec('\n');
|
|
|
|
}
|
|
|
|
void LogStream::writefln(const char *fmt, ...) {
|
2024-08-08 13:12:37 -07:00
|
|
|
log_mutex_guard guard(log_mutex);
|
2024-03-23 18:41:26 -07:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vwritefln(fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
2024-08-08 13:12:37 -07:00
|
|
|
void LogStream::write_level_prefix(size_t level) {
|
|
|
|
log_mutex_guard guard(log_mutex);
|
|
|
|
for (size_t i = 0; i < level; i++) {
|
|
|
|
writes(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LogStream::write_level(size_t level, const char *msg) {
|
|
|
|
log_mutex_guard guard(log_mutex);
|
|
|
|
write_level_prefix(level);
|
|
|
|
writeln(msg);
|
|
|
|
}
|
|
|
|
void LogStream::write_level(size_t level, std::string msg) {
|
|
|
|
log_mutex_guard guard(log_mutex);
|
|
|
|
write_level_prefix(level);
|
|
|
|
writeln(msg);
|
|
|
|
}
|
|
|
|
void LogStream::vwritef_level(size_t level, const char *fmt, va_list args) {
|
|
|
|
log_mutex_guard guard(log_mutex);
|
|
|
|
write_level_prefix(level);
|
|
|
|
vwritefln(fmt, args);
|
|
|
|
}
|
|
|
|
void LogStream::writef_level(size_t level, const char *fmt, ...) {
|
|
|
|
log_mutex_guard guard(log_mutex);
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vwritef_level(level, 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)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2024-08-08 13:12:37 -07:00
|
|
|
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
|
|
|
}
|
2024-08-08 13:12:37 -07:00
|
|
|
LogStreamMap log_streams;
|
|
|
|
void make_log_stream(int stream_id, log_stream_creation_function_t fn) {
|
|
|
|
if (!log_streams.contains(stream_id)) {
|
|
|
|
log_streams[stream_id] = std::stack<LogStream*>();
|
|
|
|
}
|
|
|
|
LogStream *new_stream = fn(stream_id);
|
|
|
|
log_streams[stream_id].push(new_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::recursive_mutex log_stream_mutex;
|
|
|
|
void init_logging_custom(log_stream_creation_function_t fn) {
|
|
|
|
std::lock_guard<std::recursive_mutex> guard(log_stream_mutex);
|
|
|
|
make_log_stream(-1, fn);
|
|
|
|
make_log_stream(0, fn);
|
|
|
|
make_log_stream(1, fn);
|
|
|
|
make_log_stream(2, fn);
|
|
|
|
}
|
|
|
|
void pop_log_stream(int i) {
|
|
|
|
log_streams[i].pop();
|
|
|
|
}
|
|
|
|
void pop_log_streams() {
|
|
|
|
std::lock_guard<std::recursive_mutex> guard(log_stream_mutex);
|
|
|
|
pop_log_stream(-1);
|
|
|
|
pop_log_stream(0);
|
|
|
|
pop_log_stream(1);
|
|
|
|
pop_log_stream(2);
|
|
|
|
}
|
|
|
|
LogStream *_init_logging_normal(int id) {
|
|
|
|
std::map<int, std::string> stream_names = {
|
|
|
|
{-1, "DEBUG"},
|
|
|
|
{0, "INFO"},
|
|
|
|
{1, "WARNING"},
|
|
|
|
{2, "ERROR"}
|
|
|
|
};
|
2024-04-28 12:31:40 -07:00
|
|
|
#ifdef __ANDROID__
|
2024-08-08 13:12:37 -07:00
|
|
|
std::map<int, int> stream_files = {
|
|
|
|
{-1, ANDROID_LOG_DEBUG},
|
|
|
|
{0, ANDROID_LOG_INFO},
|
|
|
|
{1, ANDROID_LOG_WARN},
|
|
|
|
{2, ANDROID_LOG_ERROR}
|
|
|
|
};
|
2024-04-28 12:31:40 -07:00
|
|
|
#else
|
2024-08-08 13:12:37 -07:00
|
|
|
std::map<int, FILE*> stream_files = {
|
|
|
|
{-1, stderr},
|
|
|
|
{0, stdout},
|
|
|
|
{1, stderr},
|
|
|
|
{2, stderr}
|
|
|
|
};
|
2024-04-28 12:31:40 -07:00
|
|
|
#endif
|
2024-08-08 13:12:37 -07:00
|
|
|
return new LogStream({stream_names[id]}, {stream_files[id]}, id);
|
|
|
|
}
|
|
|
|
void init_logging() {
|
|
|
|
init_logging_custom(_init_logging_normal);
|
2024-03-23 18:41:26 -07:00
|
|
|
}
|
|
|
|
LogStream &get_log_stream_by_level(int level) {
|
2024-08-08 13:12:37 -07:00
|
|
|
return *log_streams[level].top();
|
2024-03-23 18:41:26 -07:00
|
|
|
}
|
|
|
|
|
2024-05-02 14:52:11 -07:00
|
|
|
}
|
|
|
|
void write_log(int level, const char *log) {
|
|
|
|
LOG(level).writes(log);
|
|
|
|
}
|
|
|
|
void write_logln(int level, const char *log) {
|
|
|
|
LOG(level).writeln(log);
|
2024-08-08 13:12:37 -07:00
|
|
|
}
|