2019-04-20 15:15:51 +08:00
|
|
|
// fish logging
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "flog.h"
|
2019-10-14 06:50:48 +08:00
|
|
|
|
|
|
|
#include <vector>
|
2019-04-20 15:15:51 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "enum_set.h"
|
2019-10-14 06:50:48 +08:00
|
|
|
#include "global_safety.h"
|
2019-04-20 15:15:51 +08:00
|
|
|
#include "parse_util.h"
|
|
|
|
#include "wildcard.h"
|
|
|
|
|
|
|
|
namespace flog_details {
|
|
|
|
|
|
|
|
// Note we are relying on the order of global initialization within this file.
|
|
|
|
// It is important that 'all' be initialized before 'g_categories', because g_categories wants to
|
|
|
|
// append to all in the ctor.
|
|
|
|
/// This is not modified after initialization.
|
|
|
|
static std::vector<category_t *> s_all_categories;
|
|
|
|
|
|
|
|
/// When a category is instantiated it adds itself to the 'all' list.
|
|
|
|
category_t::category_t(const wchar_t *name, const wchar_t *desc, bool enabled)
|
|
|
|
: name(name), description(desc), enabled(enabled) {
|
|
|
|
s_all_categories.push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Instantiate all categories.
|
|
|
|
/// This is deliberately leaked to avoid pointless destructor registration.
|
|
|
|
category_list_t *const category_list_t::g_instance = new category_list_t();
|
|
|
|
|
|
|
|
logger_t::logger_t() : file_(stderr) {}
|
|
|
|
|
|
|
|
owning_lock<logger_t> g_logger;
|
|
|
|
|
|
|
|
void logger_t::log1(const wchar_t *s) { std::fputws(s, file_); }
|
2019-06-01 00:31:11 +08:00
|
|
|
|
|
|
|
void logger_t::log1(const char *s) {
|
|
|
|
// Note glibc prohibits mixing narrow and wide I/O, so always use wide-printing functions.
|
|
|
|
// See #5900.
|
|
|
|
std::fwprintf(file_, L"%s", s);
|
|
|
|
}
|
2019-04-20 15:15:51 +08:00
|
|
|
|
|
|
|
void logger_t::log1(wchar_t c) { std::fputwc(c, file_); }
|
2019-06-01 00:31:11 +08:00
|
|
|
|
|
|
|
void logger_t::log1(char c) { std::fwprintf(file_, L"%c", c); }
|
2019-04-20 15:15:51 +08:00
|
|
|
|
|
|
|
void logger_t::log_fmt(const category_t &cat, const wchar_t *fmt, ...) {
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
log1(cat.name);
|
2019-05-29 04:39:47 +08:00
|
|
|
log1(L": ");
|
2019-04-20 15:15:51 +08:00
|
|
|
std::vfwprintf(file_, fmt, va);
|
2019-05-29 04:39:47 +08:00
|
|
|
log1(L'\n');
|
2019-04-20 15:15:51 +08:00
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
void logger_t::log_fmt(const category_t &cat, const char *fmt, ...) {
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
log1(cat.name);
|
|
|
|
log1(": ");
|
|
|
|
std::vfprintf(file_, fmt, va);
|
|
|
|
log1('\n');
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace flog_details
|
|
|
|
|
|
|
|
using namespace flog_details;
|
|
|
|
|
|
|
|
/// For each category, if its name matches the wildcard, set its enabled to the given sense.
|
|
|
|
static void apply_one_wildcard(const wcstring &wc_esc, bool sense) {
|
|
|
|
wcstring wc = parse_util_unescape_wildcards(wc_esc);
|
|
|
|
for (category_t *cat : s_all_categories) {
|
|
|
|
if (wildcard_match(cat->name, wc)) {
|
|
|
|
cat->enabled = sense;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 08:38:09 +08:00
|
|
|
void activate_flog_categories_by_pattern(const wcstring &inwc) {
|
|
|
|
// Normalize underscores to dashes, allowing the user to be sloppy.
|
|
|
|
wcstring wc = inwc;
|
|
|
|
std::replace(wc.begin(), wc.end(), L'_', L'-');
|
2019-04-20 15:15:51 +08:00
|
|
|
for (const wcstring &s : split_string(wc, L',')) {
|
2019-05-30 05:21:50 +08:00
|
|
|
if (string_prefixes_string(L"-", s)) {
|
2019-04-20 15:15:51 +08:00
|
|
|
apply_one_wildcard(s.substr(1), false);
|
|
|
|
} else {
|
|
|
|
apply_one_wildcard(s, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_flog_output_file(FILE *f) { g_logger.acquire()->set_file(f); }
|
|
|
|
|
|
|
|
std::vector<const category_t *> get_flog_categories() {
|
|
|
|
std::vector<const category_t *> result(s_all_categories.begin(), s_all_categories.end());
|
|
|
|
std::sort(result.begin(), result.end(), [](const category_t *a, const category_t *b) {
|
|
|
|
return wcscmp(a->name, b->name) < 0;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|