add windows code page manager

This commit is contained in:
ValKmjolnir 2024-06-02 22:08:17 +08:00
parent 4ac4675491
commit f6d4d79c51
3 changed files with 52 additions and 32 deletions

View File

@ -1,10 +1,7 @@
#pragma once
#include "ast_visitor.h"
#ifdef _WIN32
#include <windows.h>
#endif
#include "util/util.h"
#include <iostream>
#include <cstring>
@ -93,19 +90,10 @@ public:
public:
void dump(code_block* root) {
#ifdef _WIN32
// store previous code page
auto cp = GetConsoleOutputCP();
// allow 65001 code page
SetConsoleOutputCP(CP_UTF8);
#endif
util::windows_code_page_manager wcpm;
wcpm.set_utf8_output();
root->accept(this);
#ifdef _WIN32
// restore previous code page
SetConsoleOutputCP(cp);
#endif
wcpm.restore_code_page();
}
};

View File

@ -301,17 +301,17 @@ void gc::info() const {
for(usize i = 0; i<indent; ++i) {
indent_string += "-";
}
auto last_line = indent_string + "+" +
indent_string + "-" + indent_string + "-" + indent_string;
const auto last_line = "+" + indent_string + "+" +
indent_string + "-" + indent_string + "-" + indent_string + "+";
indent_string = indent_string + "+" +
indent_string + "+" + indent_string + "+" + indent_string;
std::clog << "\n" << indent_string << "\n";
std::clog << " " << left << setw(indent) << setfill(' ') << "object type";
std::clog << "\n+" << indent_string << "+\n";
std::clog << "| " << left << setw(indent) << setfill(' ') << "object type";
std::clog << " | " << left << setw(indent) << setfill(' ') << "gc count";
std::clog << " | " << left << setw(indent) << setfill(' ') << "alloc count";
std::clog << " | " << left << setw(indent) << setfill(' ') << "memory size";
std::clog << "\n" << indent_string << "\n";
std::clog << " |\n+" << indent_string << "+\n";
double total = 0;
for(u8 i = 0; i<gc_type_size; ++i) {
@ -319,32 +319,32 @@ void gc::info() const {
continue;
}
total += static_cast<f64>(gcnt[i]);
std::clog << " " << left << setw(indent) << setfill(' ') << name[i];
std::clog << "| " << left << setw(indent) << setfill(' ') << name[i];
std::clog << " | " << left << setw(indent) << setfill(' ') << gcnt[i];
std::clog << " | " << left << setw(indent) << setfill(' ') << acnt[i];
std::clog << " | " << left << setw(indent) << setfill(' ') << size[i];
std::clog << "\n";
std::clog << " |\n";
}
std::clog << indent_string << "\n";
std::clog << "+" << indent_string << "+\n";
auto den = std::chrono::high_resolution_clock::duration::period::den;
std::clog << " " << left << setw(indent) << setfill(' ') << "detail";
std::clog << "| " << left << setw(indent) << setfill(' ') << "detail";
std::clog << " | " << left << setw(indent) << setfill(' ') << "time spend";
std::clog << " | " << left << setw(indent) << setfill('x') << "x";
std::clog << " | " << left << setw(indent) << setfill('x') << "x";
std::clog << "\n" << indent_string << "\n";
std::clog << " |\n+" << indent_string << "+\n";
std::clog << " " << left << setw(indent) << setfill(' ') << "gc time";
std::clog << "| " << left << setw(indent) << setfill(' ') << "gc time";
std::clog << " | " << worktime*1.0/den*1000 << " ms\n";
std::clog << " " << left << setw(indent) << setfill(' ') << "avg time";
std::clog << "| " << left << setw(indent) << setfill(' ') << "avg time";
std::clog << " | " << worktime*1.0/den*1000/total << " ms\n";
std::clog << " " << left << setw(indent) << setfill(' ') << "max gc";
std::clog << "| " << left << setw(indent) << setfill(' ') << "max gc";
std::clog << " | " << max_time*1.0/den*1000 << " ms\n";
std::clog << " " << left << setw(indent) << setfill(' ') << "max mark";
std::clog << "| " << left << setw(indent) << setfill(' ') << "max mark";
std::clog << " | " << max_mark_time*1.0/den*1000 << " ms\n";
std::clog << " " << left << setw(indent) << setfill(' ') << "max sweep";
std::clog << "| " << left << setw(indent) << setfill(' ') << "max sweep";
std::clog << " | " << max_sweep_time*1.0/den*1000 << " ms\n";
std::clog << " " << left << setw(indent) << setfill(' ') << "concurrent";
std::clog << "| " << left << setw(indent) << setfill(' ') << "concurrent";
std::clog << " | " << (flag_concurrent_mark_triggered? "true\n":"false\n");
std::clog << last_line << "\n";
}

View File

@ -2,6 +2,10 @@
#include "nasal.h"
#ifdef _WIN32
#include <windows.h>
#endif
#include <cstring>
#include <sstream>
@ -38,4 +42,32 @@ f64 dec_to_f64(const char*);
f64 str_to_num(const char*);
class windows_code_page_manager {
private:
u32 code_page;
public:
windows_code_page_manager() {
#ifdef _WIN32
code_page = GetConsoleOutputCP();
#endif
}
void set_utf8_output() {
#ifdef _WIN32
// store previous code page
code_page = GetConsoleOutputCP();
// allow 65001 code page
SetConsoleOutputCP(CP_UTF8);
#endif
}
void restore_code_page() {
#ifdef _WIN32
// restore previous code page
SetConsoleOutputCP(code_page);
#endif
}
};
}