Skip to content

Commit

Permalink
remove the prefix "LOG_" to all log_level variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Apr 30, 2024
1 parent 1b1c5ff commit 707eb2d
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 149 deletions.
90 changes: 51 additions & 39 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ enum {
};

enum log_level {
LOG_ERROR,
LOG_WARN,
LOG_INFO,
LOG_DEBUG,
LOG_NONE // display no prefix for this.
ERROR,
WARN,
INFO,
DEBUG,
NONE // display no prefix for this.
};

bool hasEnding(string_view fullString, string_view ending);
Expand Down Expand Up @@ -103,86 +103,98 @@ template <typename T>
constexpr bool is_fmt_convertible_v = is_fmt_convertible<T>::value;

template <typename... Args>
void log_println(int log, const fmt::text_style& ts, std::string_view fmt, Args&&... args) {
void log_println(log_level log, const fmt::text_style& ts, std::string_view fmt, Args&&... args) {
switch (log) {
case LOG_ERROR:
case ERROR:
fmt::print(BOLD_TEXT(color.red), "ERROR: ");
break;
case LOG_WARN:
case WARN:
fmt::print(BOLD_TEXT(color.yellow), "Warning: ");
break;
case LOG_INFO:
case INFO:
fmt::print(BOLD_TEXT(color.cyan), "Info: ");
break;
case LOG_DEBUG:
case DEBUG:
if (!config->debug)
return;
fmt::print(BOLD_TEXT(color.magenta), "[DEBUG]: ");
break;
case NONE:
default:
break;
}
// I don't want to add a '\n' each time i'm writing a log_printf(), I just forget it all the time
fmt::println(ts, fmt::runtime(fmt), std::forward<Args>(args)...);
}

template <typename... Args>
void log_println(int log, std::string_view fmt, Args&&... args) {
void log_println(log_level log, std::string_view fmt, Args&&... args) {
switch (log) {
case LOG_ERROR:
case ERROR:
fmt::print(BOLD_TEXT(color.red), "ERROR: ");
break;
case LOG_WARN:
case WARN:
fmt::print(BOLD_TEXT(color.yellow), "Warning: ");
break;
case LOG_INFO:
case INFO:
fmt::print(BOLD_TEXT(color.cyan), "Info: ");
break;
case LOG_DEBUG:
case DEBUG:
if (!config->debug)
return;
fmt::print(BOLD_TEXT(color.magenta), "[DEBUG]: ");
break;
case NONE:
default:
break;
}
fmt::println(fmt::runtime(fmt), std::forward<Args>(args)...);
}

template <typename... Args>
void log_printf(int log, std::string_view fmt, Args&&... args) {
void log_printf(log_level log, std::string_view fmt, Args&&... args) {
switch (log) {
case LOG_ERROR:
case ERROR:
fmt::print(BOLD_TEXT(color.red), "ERROR: ");
break;
case LOG_WARN:
case WARN:
fmt::print(BOLD_TEXT(color.yellow), "Warning: ");
break;
case LOG_INFO:
case INFO:
fmt::print(BOLD_TEXT(color.cyan), "Info: ");
break;
case LOG_DEBUG:
case DEBUG:
if (!config->debug)
return;
fmt::print(BOLD_TEXT(color.magenta), "[DEBUG]: ");
break;
case NONE:
default:
break;
}
fmt::print(fmt::runtime(fmt), std::forward<Args>(args)...);
}

template <typename... Args>
void log_printf(int log, const fmt::text_style& ts, std::string_view fmt, Args&&... args) {
void log_printf(log_level log, const fmt::text_style& ts, std::string_view fmt, Args&&... args) {
switch (log) {
case LOG_ERROR:
case ERROR:
fmt::print(BOLD_TEXT(color.red), "ERROR: ");
break;
case LOG_WARN:
case WARN:
fmt::print(BOLD_TEXT(color.yellow), "Warning: ");
break;
case LOG_INFO:
case INFO:
fmt::print(BOLD_TEXT(color.cyan), "Info: ");
break;
case LOG_DEBUG:
case DEBUG:
if (!config->debug)
return;
fmt::print(BOLD_TEXT(color.magenta), "[DEBUG]: ");
break;
case NONE:
default:
break;
}
fmt::print(ts, fmt::runtime(fmt), std::forward<Args>(args)...);
}
Expand All @@ -200,25 +212,25 @@ bool askUserYorN(bool def, prompt_yn pr, Args&&... args) {

switch (pr) {
case PROMPT_YN_DIFF:
log_printf(LOG_INFO, BOLD, "View the diffs for {}? {}", std::forward<Args>(args)..., inputs_str);
log_printf(INFO, BOLD, "View the diffs for {}? {}", std::forward<Args>(args)..., inputs_str);
if (config->noconfirm) return NO;
break;
case PROMPT_YN_CONTINUE_WITHOUT_DIFF:
log_printf(LOG_WARN, BOLD,
log_printf(WARN, BOLD,
"With your current settings, viewing PKGBUILD diffs is unsupported (maybe useGit is false?), continue with the installation anyway? {}",
inputs_str);
if (config->noconfirm) return YES;
break;
case PROMPT_YN_EDIT_PKGBUILD:
log_printf(LOG_INFO, BOLD, "Review PKGBUILD for {}? {}", std::forward<Args>(args)..., inputs_str);
log_printf(INFO, BOLD, "Review PKGBUILD for {}? {}", std::forward<Args>(args)..., inputs_str);
if (config->noconfirm) return NO;
break;
case PROMPT_YN_PROCEED_INSTALL:
log_printf(LOG_INFO, BOLD, "Proceed with the installation? {}", inputs_str);
log_printf(INFO, BOLD, "Proceed with the installation? {}", inputs_str);
if (config->noconfirm) return YES;
break;
case PROMPT_YN_PROCEED_TRANSACTION:
log_printf(LOG_INFO, BOLD, "Would you like to proceed with this transaction? {}", inputs_str);
log_printf(INFO, BOLD, "Would you like to proceed with this transaction? {}", inputs_str);
if (config->noconfirm) return NO;
break;
default:
Expand All @@ -228,7 +240,7 @@ bool askUserYorN(bool def, prompt_yn pr, Args&&... args) {
//std::cin.sync();
// while the getline function works, and the result is not 1 character long, keep reminding the user.
while (std::getline(std::cin, result) && (result.length() > 1))
log_printf(LOG_WARN, "Please provide a valid response {}", inputs_str);
log_printf(WARN, "Please provide a valid response {}", inputs_str);

ctrl_d_handler();

Expand Down Expand Up @@ -257,15 +269,15 @@ vector<T> askUserForList(vector<T> &list, prompt_list pr, bool required = false)

switch (pr) {
case PROMPT_LIST_CLEANBUILDS:
log_printf(LOG_INFO, BOLD, "Packages to completely rebuild: ");
log_printf(INFO, BOLD, "Packages to completely rebuild: ");
if (config->noconfirm && !required) return {};
break;
case PROMPT_LIST_REVIEWS:
log_printf(LOG_INFO, BOLD, "Packages you'd like to review: ");
log_printf(INFO, BOLD, "Packages you'd like to review: ");
if (config->noconfirm && !required) return {};
break;
case PROMPT_LIST_REMOVE_PKGS:
log_printf(LOG_INFO, BOLD, "Packages you'd like to remove: ");
log_printf(INFO, BOLD, "Packages you'd like to remove: ");
if (config->noconfirm && !required) return {};
break;
default:
Expand Down Expand Up @@ -295,7 +307,7 @@ vector<T> askUserForList(vector<T> &list, prompt_list pr, bool required = false)
if (input_indices[i].find('-') != std::string::npos) {
vector<string> loop_bounds = split(input_indices[i], '-');
if (loop_bounds.size() != 2 || !is_numerical(loop_bounds[0]) || !is_numerical(loop_bounds[1])) {
log_printf(LOG_WARN, "Invalid loop range! (loop ranges look like \"0-5\"): ");
log_printf(WARN, "Invalid loop range! (loop ranges look like \"0-5\"): ");
breakandcontinue = true;
break;
}
Expand All @@ -304,7 +316,7 @@ vector<T> askUserForList(vector<T> &list, prompt_list pr, bool required = false)
int higherbound = std::stoi(loop_bounds[1]);

if ((0 > lowerbound || lowerbound > list.size()) || (lowerbound > higherbound || higherbound >= list.size())) {
log_printf(LOG_WARN, "Invalid loop range! (loop ranges must stay in bounds and in order): ");
log_printf(WARN, "Invalid loop range! (loop ranges must stay in bounds and in order): ");
breakandcontinue = true;
break;
}
Expand All @@ -318,13 +330,13 @@ vector<T> askUserForList(vector<T> &list, prompt_list pr, bool required = false)
}

if (!is_numerical(input_indices[i])) {
log_printf(LOG_WARN, "{}: ", sep_str);
log_printf(WARN, "{}: ", sep_str);
continue;
}

int index = std::stoi(input_indices[i]);
if (0 > index || index >= list.size()) {
log_println(LOG_WARN, "Invalid index! Ignoring index #{}.", input_indices[i]);
log_println(WARN, "Invalid index! Ignoring index #{}.", input_indices[i]);
continue;
}
result.push_back(list[index]);
Expand All @@ -335,7 +347,7 @@ vector<T> askUserForList(vector<T> &list, prompt_list pr, bool required = false)
continue;

if (added_elements == 0) {
log_printf(LOG_WARN, "{}: ", sep_str);
log_printf(WARN, "{}: ", sep_str);
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions src/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ alpm_list_smart_deleter taur_targets(nullptr, free_list_and_internals);

void invalid_opt(int used, string_view opt1, string_view opt2) {
if (used)
log_println(LOG_ERROR, "invalid option: '{}' and '{}' may not be used together", opt1, opt2);
log_println(ERROR, "invalid option: '{}' and '{}' may not be used together", opt1, opt2);
}

/** Helper function for parsing operation from command-line arguments.
Expand Down Expand Up @@ -118,11 +118,11 @@ int parsearg_sync(int opt) {
switch (opt) {
case OP_SYSUPGRADE:
case 'u':
(op.op_s_upgrade)++;
op.op_s_upgrade = 1;
break;
case OP_REFRESH:
case 'y':
(op.op_s_sync)++;
op.op_s_sync = 1;
break;
case OP_SEARCH:
case 's':
Expand Down
14 changes: 7 additions & 7 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ void Config::init(string configFile, string themeFile) {
bool newUser = false;

if (!std::filesystem::exists(configDir)) {
log_println(LOG_NONE, "Warning: TabAUR config folder was not found, Creating folders at {}!", configDir);
log_println(NONE, "Warning: TabAUR config folder was not found, Creating folders at {}!", configDir);
std::filesystem::create_directories(configDir);

newUser = true;
}
if (!std::filesystem::exists(configFile)) {
log_println(LOG_NONE, "Warning: {} not found, generating new one", configFile);
log_println(NONE, "Warning: {} not found, generating new one", configFile);
// https://github.com/hyprwm/Hyprland/blob/main/src/config/ConfigManager.cpp#L681
ofstream f(configFile, std::ios::trunc);
f << AUTOCONFIG;
f.close();
}
if (!std::filesystem::exists(themeFile)) {
log_println(LOG_NONE, "Warning: {} not found, generating new one", themeFile);
log_println(NONE, "Warning: {} not found, generating new one", themeFile);
ofstream f(themeFile, std::ios::trunc);
f << AUTOTHEME;
f.close();
Expand All @@ -52,7 +52,7 @@ void Config::init(string configFile, string themeFile) {

this->initialized = true;
if (!std::filesystem::exists(config->cacheDir)) {
log_println(LOG_WARN, "TabAUR cache folder was not found, Creating folders at {}!", config->cacheDir);
log_println(WARN, "TabAUR cache folder was not found, Creating folders at {}!", config->cacheDir);
std::filesystem::create_directories(config->cacheDir);
}

Expand Down Expand Up @@ -113,7 +113,7 @@ void Config::loadConfigFile(string_view filename) {
try {
this->tbl = toml::parse_file(filename);
} catch (const toml::parse_error& err) {
log_println(LOG_NONE, "ERROR: Parsing config file {} failed:", filename);
log_println(NONE, "ERROR: Parsing config file {} failed:", filename);
std::cerr << err << std::endl;
exit(-1);
}
Expand All @@ -135,7 +135,7 @@ void Config::loadThemeFile(string_view filename) {
try {
this->theme_tbl = toml::parse_file(filename);
} catch (const toml::parse_error& err) {
log_println(LOG_ERROR, "Parsing theme file {} failed:", filename);
log_println(ERROR, "Parsing theme file {} failed:", filename);
std::cerr << err << std::endl;
exit(-1);
}
Expand Down Expand Up @@ -229,7 +229,7 @@ void Config::loadPacmanConfigFile(string filename) {

bool serversStatus = addServers(db, ini[section.data()]["Include"], section);
if (!serversStatus)
log_println(LOG_ERROR, "Failed to open mirrors file! ({})", ini[section.data()]["Include"]);
log_println(ERROR, "Failed to open mirrors file! ({})", ini[section.data()]["Include"]);

alpm_db_set_usage(db, ALPM_DB_USAGE_ALL);

Expand Down
Loading

0 comments on commit 707eb2d

Please sign in to comment.