Skip to content

Commit

Permalink
fix tabs/spaces, remove unused struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nithax committed Mar 28, 2021
1 parent 0ab9400 commit 25fda1f
Show file tree
Hide file tree
Showing 7 changed files with 998 additions and 1,018 deletions.
109 changes: 51 additions & 58 deletions logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

#include "utils.hpp"

enum class log_level : std::uint32_t
{
enum class log_level : std::uint32_t {
LOG_FATAL,
LOG_ERROR,
LOG_WARN,
Expand All @@ -20,37 +19,33 @@ enum class log_level : std::uint32_t
LOG_NOPREFIX, // keep this last
};

class logger
{
class logger {
public:

logger( const std::wstring_view& title_name = {} )
{
logger(const std::wstring_view &title_name = {}) {

AllocConsole();
AttachConsole( GetCurrentProcessId() );
AttachConsole(GetCurrentProcessId());

if ( !title_name.empty() )
{
SetConsoleTitle( title_name.data() );
if (!title_name.empty()) {
SetConsoleTitle(title_name.data());
}

FILE* in = nullptr;
FILE* out = nullptr;
FILE *in = nullptr;
FILE *out = nullptr;

freopen_s( &in, "conin$", "r", stdin );
freopen_s( &out, "conout$", "w", stdout );
freopen_s( &out, "conout$", "w", stderr );
freopen_s(&in, "conin$", "r", stdin);
freopen_s(&out, "conout$", "w", stdout);
freopen_s(&out, "conout$", "w", stderr);

console_handle = GetStdHandle( STD_OUTPUT_HANDLE );
console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
}

~logger()
{
~logger() {
FreeConsole();
}

enum class console_colors : std::uint8_t
{
enum class console_colors : std::uint8_t {
BLACK,
DARK_BLUE,
DARK_GREEN,
Expand All @@ -69,8 +64,8 @@ class logger
WHITE,
};

struct log_type_info_t
{
struct log_type_info_t {

std::string prefix{};
console_colors fg = console_colors::WHITE;
console_colors bg = console_colors::BLACK;
Expand All @@ -79,92 +74,90 @@ class logger
};

template<typename ... arg>
void print_colored( console_colors fg, console_colors bg, bool newline, std::string_view fmt, arg ... args )
{
std::unique_lock< decltype( m )> lock( m );
void print_colored(console_colors fg, console_colors bg, bool newline, std::string_view fmt, arg ... args) {

const auto txt = utils::format_string( fmt, args... );
std::unique_lock< decltype(m)> lock(m);

set_console_color( fg, bg );
const auto txt = utils::format_string(fmt, args...);

set_console_color(fg, bg);

std::cout << txt.c_str();

set_console_color( console_colors::WHITE, console_colors::BLACK );
set_console_color(console_colors::WHITE, console_colors::BLACK);

if (newline) {
std::cout << std::endl;
}
}

template< typename ... arg >
void print( log_level level, std::string_view fmt, arg ... args ) {
std::unique_lock< decltype( m )> lock( m );
void print(log_level level, std::string_view fmt, arg ... args) {

std::unique_lock< decltype(m)> lock(m);

const auto &info = console_type_info[level];
const auto txt = utils::format_string( fmt, args... );
const auto txt = utils::format_string(fmt, args...);

set_console_color( info.fg, info.bg );
set_console_color(info.fg, info.bg);

if ( level < log_level::LOG_NOPREFIX ) {
if (level < log_level::LOG_NOPREFIX) {
std::cout << info.prefix;
}

std::cout << txt.c_str();

set_console_color( console_colors::WHITE, console_colors::BLACK );
set_console_color(console_colors::WHITE, console_colors::BLACK);

std::cout << std::endl;
}

template< typename ... arg >
void print_with_func( log_level level, std::string_view func_name, std::string_view fmt, arg ... args )
{
std::unique_lock< decltype( m )> lock( m );
void print_with_func(log_level level, std::string_view func_name, std::string_view fmt, arg ... args) {

const auto &info = console_type_info[ level ];
const auto txt = utils::format_string( fmt, args... );
std::unique_lock< decltype(m)> lock(m);

set_console_color( info.fg, info.bg );
const auto &info = console_type_info[level];
const auto txt = utils::format_string(fmt, args...);

set_console_color(info.fg, info.bg);

if ( level < log_level::LOG_NOPREFIX )
{
if (level < log_level::LOG_NOPREFIX) {
std::cout << info.prefix;
}

std::cout << "[ " << func_name.data() << " ] " << txt.c_str();

set_console_color( console_colors::WHITE, console_colors::BLACK );
set_console_color(console_colors::WHITE, console_colors::BLACK);

std::cout << std::endl;
}

private:
inline bool set_console_color( const console_colors fg, const console_colors bg )
{
if ( console_handle != INVALID_HANDLE_VALUE )
{
WORD color = (uint8_t)fg | ( (uint8_t)bg << 4 );
return SetConsoleTextAttribute( console_handle, color );
inline bool set_console_color(const console_colors fg, const console_colors bg) {

if (console_handle != INVALID_HANDLE_VALUE) {
WORD color = (uint8_t)fg | ((uint8_t)bg << 4);
return SetConsoleTextAttribute(console_handle, color);
}

return false;
}

std::unordered_map< log_level, log_type_info_t > console_type_info =
{
{ log_level::LOG_FATAL, { "[ ! ] ", console_colors::RED, console_colors::WHITE } },
{ log_level::LOG_ERROR, { "[ - ] ", console_colors::RED, console_colors::BLACK } },
{ log_level::LOG_WARN, { "[ # ] ", console_colors::BLACK, console_colors::YELLOW } },
{ log_level::LOG_OK, { "[ + ] ", console_colors::GREEN, console_colors::BLACK } },
{ log_level::LOG_INFO, { "[ ~ ] ", console_colors::WHITE, console_colors::BLACK } },
{ log_level::LOG_DEBUG, { " ", console_colors::DARK_GRAY, console_colors::BLACK } },
std::unordered_map< log_level, log_type_info_t > console_type_info = {
{log_level::LOG_FATAL, {"[ ! ] ", console_colors::RED, console_colors::WHITE}},
{log_level::LOG_ERROR, {"[ - ] ", console_colors::RED, console_colors::BLACK}},
{log_level::LOG_WARN, {"[ # ] ", console_colors::BLACK, console_colors::YELLOW}},
{log_level::LOG_OK, {"[ + ] ", console_colors::GREEN, console_colors::BLACK}},
{log_level::LOG_INFO, {"[ ~ ] ", console_colors::WHITE, console_colors::BLACK}},
{log_level::LOG_DEBUG, {" ", console_colors::DARK_GRAY, console_colors::BLACK}},
};

std::mutex m;
HANDLE console_handle = INVALID_HANDLE_VALUE;
};

inline auto g_logger = std::make_unique< logger >( L"~ rpgmaker scraper by nit ~" );
inline auto g_logger = std::make_unique< logger >(L"~ rpgmaker scraper by nit ~");
#define log_colored_nnl(fg, bg, ...) g_logger->print_colored(fg, bg, false, __VA_ARGS__)
#define log_colored( fg, bg, ... ) g_logger->print_colored( fg, bg, true, __VA_ARGS__ )
#define _log(log_type, ...) g_logger->print( log_type, __VA_ARGS__ )
Expand Down
134 changes: 67 additions & 67 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,71 @@

int main(int argc, const char *argv[]) {

constexpr uint32_t expected_minimum_argc = 3;

constexpr const char* search_type_variables = "-v";
constexpr const char* search_type_switches = "-s";

const auto print_usage = []() {
log_err(R"(Usage: RPGMakerScraper -v 143 test_output.txt)");
};

// check the argument count
if (argc < expected_minimum_argc) {
print_usage();
return 1;
}
const std::string search_type{ argv[1] };
const std::string id_str{ argv[2] };

const bool output_to_file = argc == 4;

// check search types
if (search_type != search_type_variables) {
log_err(R"(invalid search type. Only '-v' for variables is supported at the moment.")");
print_usage();
return 1;
}

// search variable ids
if (search_type == search_type_variables) {
// make sure this id is actually a number
if (!std::all_of(id_str.begin(), id_str.end(), isdigit)) {
log_err(R"(invalid variable id. Please provide a number.")");
print_usage();
return 1;
}

const uint32_t variable_id = static_cast<uint32_t>(std::stoul(id_str));

try {
RPGMakerScraper scraper{ ScrapeMode::VARIABLES, variable_id };
scraper.scrape_variables();

if (output_to_file) {
log_info(R"(writing results to %s...)", argv[3]);
const std::string file_name{ argv[3] };
std::ofstream file(file_name, std::ios_base::out);

if (!file.is_open() || !file.good()) {
throw std::invalid_argument(R"(unable to create output file)");
}

file << scraper;
file.close();

log_ok(R"(results wrote successfully.)");
}

} catch (const std::exception &e) {
log_err(R"(exception caught: %s)", e.what());
}
}

log_nopre("\n");
log_ok(R"(press any key to close the program...)");

std::cin.get();
return 0;
constexpr uint32_t expected_minimum_argc = 3;

constexpr const char *search_type_variables = "-v";
constexpr const char *search_type_switches = "-s";

const auto print_usage = []() {
log_err(R"(Usage: RPGMakerScraper -v 143 test_output.txt)");
};

// check the argument count
if (argc < expected_minimum_argc) {
print_usage();
return 1;
}

const std::string search_type{argv[1]};
const std::string id_str{argv[2]};

const bool output_to_file = argc == 4;

// check search types
if (search_type != search_type_variables) {
log_err(R"(invalid search type. Only '-v' for variables is supported at the moment.")");
print_usage();
return 1;
}

// search variable ids
if (search_type == search_type_variables) {
// make sure this id is actually a number
if (!std::all_of(id_str.begin(), id_str.end(), isdigit)) {
log_err(R"(invalid variable id. Please provide a number.")");
print_usage();
return 1;
}

const uint32_t variable_id = static_cast<uint32_t>(std::stoul(id_str));

try {
RPGMakerScraper scraper{ScrapeMode::VARIABLES, variable_id};
scraper.scrape_variables();

if (output_to_file) {
log_info(R"(writing results to %s...)", argv[3]);
const std::string file_name{argv[3]};
std::ofstream file(file_name, std::ios_base::out);

if (!file.is_open() || !file.good()) {
throw std::invalid_argument(R"(unable to create output file)");
}

file << scraper;
file.close();

log_ok(R"(results wrote successfully.)");
}

} catch (const std::exception &e) {
log_err(R"(exception caught: %s)", e.what());
}
}

log_nopre("\n");
log_ok(R"(press any key to close the program...)");

std::cin.get();
return 0;
}
Loading

0 comments on commit 25fda1f

Please sign in to comment.