Skip to content

Commit

Permalink
log colors and info tool
Browse files Browse the repository at this point in the history
  • Loading branch information
ate47 committed Jul 21, 2024
1 parent cae24aa commit 2948d98
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/acts/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ int MainActs(int argc, const char* _argv[], HINSTANCE hInstance, int nShowCmd) {
const char* query[]{ argv[1] };
tool::search(query, 1, [&find](const tool::toolfunctiondata* tool) {
if (!find) {
LOG_ERROR("Similar tool name(s):");
LOG_INFO("Similar tool name(s):");
find = true;
}
LOG_ERROR("- {}", tool->m_name);
LOG_INFO("- {}", tool->m_name);
});

return -1;
Expand Down
75 changes: 75 additions & 0 deletions src/acts/tools/tools.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <includes.hpp>
#include <clicolor.hpp>
#include <tools/tools_ui.hpp>
#include <tools/gsc_opcodes.hpp>

std::map<std::string, tool::toolfunctiondata*>& tool::tools() {
static std::map<std::string, tool::toolfunctiondata*> map{};
Expand Down Expand Up @@ -194,7 +197,79 @@ namespace {
}
return tool::OK;
}
#ifndef CI_BUILD
int color(Process& proc, int argc, const char* argv[]) {
for (int b = 0; b < 6; b++) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
std::cout << " " << clicolor::ColorBackground(i, j, b) << i << j << b << clicolor::Reset();
}
std::cout << " ";
for (int j = 0; j < 6; j++) {
std::cout << " " << clicolor::Color(i, j, b) << i << j << b << clicolor::Reset();
}
std::cout << "\n";
}
}

std::cout << "\n";

LOG_TRACE("Trace log");
LOG_DEBUG("Debug log");
LOG_INFO("Info log");
LOG_WARNING("Warning log");
LOG_ERROR("Error log");

return tool::OK;
}
ADD_TOOL("color", "dev", "", "color tool", nullptr, color);
#endif
int info(Process& proc, int argc, const char* argv[]) {
LOG_INFO("Loading acts data...");
hashutils::ReadDefaultFile();
tool::gsc::opcode::RegisterOpCodes();
LOG_INFO("----- acts");
LOG_INFO("version .. {} (0x{:x})", actsinfo::VERSION, actsinfo::VERSION_ID);
LOG_INFO("tools .... {} ({} categories)", tool::tools().size(), tool::toolsCategories().size());
LOG_INFO("tools ui . {}", tool::ui::tools().size());
LOG_INFO("hash(es) . {}", hashutils::GetMap().size());
LOG_INFO("path ..... {}", utils::GetProgDir().string());
LOG_INFO("----- games");

auto& gameMap = tool::gsc::opcode::GetVMMaps();

if (gameMap.empty()) {
LOG_WARNING("No game available");
}

for (auto& [vm, info] : gameMap) {
const char* out = utils::va("- %s (%s/%x) ->", info.name, info.codeName, (int)info.vm);

for (size_t i = 0; i < tool::gsc::opcode::PLATFORM_COUNT; i++) {
tool::gsc::opcode::Platform plt = (tool::gsc::opcode::Platform)i;

if (!info.HasPlatform(plt)) continue;

size_t count{};

auto it = info.opcodemappltlookup.find(plt);

if (it != info.opcodemappltlookup.end()) {
for (auto& [op, reg] : it->second) {
count += reg.size();
}
}
out = utils::va("%s %s:0x%llx", out,
utils::MapString(utils::va("%s", tool::gsc::opcode::PlatformName(plt)), [](char c) { return c == ' ' ? '_' : c; }),
count);
}

LOG_INFO("{}", out);
}

return tool::OK;
}
}
ADD_TOOL("list", "acts", "", "list all the tools", nullptr, list);
ADD_TOOL("info", "acts", "", "acts info", nullptr, info);
ADD_TOOL("search", "acts", " (args)*", "search a tool", nullptr, search);
25 changes: 25 additions & 0 deletions src/shared/clicolor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "includes_shared.hpp"
#include "clicolor.hpp"
#include "utils.hpp"

namespace clicolor {
bool ConsoleAllowColor() {
static bool color = [] {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode{};
return handle && GetConsoleMode(handle, &mode) && (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0;
}();
return color;
}

}
std::ostream& operator<<(std::ostream& out, clicolor::ColorData data) {
if (data) {
if (data & clicolor::CD_BACKGROUND) {
return out << "\033[48;5;" << std::dec << (int)(data & clicolor::CD_COLOR_MASK) << "m";
}
return out << "\033[38;5;" << std::dec << (int)(data & clicolor::CD_COLOR_MASK) << "m";
}

return out << "\033[0m";
}
30 changes: 30 additions & 0 deletions src/shared/clicolor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

namespace clicolor {

enum ColorData : uint32_t {
CD_RESET = 0,
CD_BACKGROUND = 0x10000000,
CD_COLOR_MASK = ~(CD_BACKGROUND),
};

bool ConsoleAllowColor();

constexpr ColorData Reset() {
return CD_RESET;
}

constexpr ColorData Color(int r, int g, int b) {
return (ColorData)(16 + 36 * r + 6 * g + b);
}

constexpr ColorData ResetBackground() {
return CD_RESET;
}

constexpr ColorData ColorBackground(int r, int g, int b) {
return (ColorData)(16 + 36 * r + 6 * g + b | CD_BACKGROUND);
}

}
std::ostream& operator<<(std::ostream& out, clicolor::ColorData data);
38 changes: 31 additions & 7 deletions src/shared/logs.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <includes_shared.hpp>
#include "logs.hpp"
#include "clicolor.hpp"

const char* alogs::name(loglevel lvl) {
switch (lvl)
Expand Down Expand Up @@ -61,28 +62,51 @@ void alogs::log(loglevel level, const std::string& str) {
return;
}

auto f = [&](std::ostream& out) {
auto f = [&](std::ostream& out, bool color) {
if (!g_basiclog) {
if (color) {
out << clicolor::Color(5, 5, 5);
}
std::tm tm = localtime_xp(std::time(nullptr));
out
<< "[" << std::put_time(&tm, "%H:%M:%S") << "]"
;
}
if (color) {
switch (level) {
case alogs::LVL_ERROR: out << clicolor::Color(5, 1, 1); break;
case alogs::LVL_WARNING: out << clicolor::Color(5, 4, 1); break;
case alogs::LVL_INFO: out << clicolor::Color(5, 5, 5); break;
case alogs::LVL_DEBUG: out << clicolor::Color(4, 5, 1); break;
case alogs::LVL_TRACE: out << clicolor::Color(1, 5, 5); break;
default: out << clicolor::Reset(); break;
}
}

if (!g_basiclog) {
out
<< '[' << name(level) << "] "
;
}
out
<< str
<< "\n";
};
out << str;

if (color) {
out << clicolor::Reset();
}
out << "\n";
};

auto* lf = logfile();
if (lf) {
std::ofstream out{ lf, std::ios::app };

f(out);
f(out, false);

out.close();
}
else {
f(level < LVL_WARNING ? std::cout : std::cerr);
static bool allowColor = clicolor::ConsoleAllowColor();

f(level < LVL_WARNING ? std::cout : std::cerr, allowColor);
}
}

0 comments on commit 2948d98

Please sign in to comment.