mirror of
https://github.com/ValKmjolnir/Nasal-Interpreter.git
synced 2026-07-23 11:28:45 +08:00
🚀 add colorful error info print.
This commit is contained in:
+26
-3
@@ -6,6 +6,10 @@
|
||||
#include <sstream> // MSVC need this to use std::getline
|
||||
#include <cstring>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h> // use SetConsoleTextAttribute
|
||||
#endif
|
||||
|
||||
class fstreamline
|
||||
{
|
||||
protected:
|
||||
@@ -45,18 +49,36 @@ class nasal_err:public fstreamline
|
||||
{
|
||||
private:
|
||||
u32 error;
|
||||
void printstg(const char* stage)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CONSOLE_SCREEN_BUFFER_INFO scrinfo;
|
||||
GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE),&scrinfo);
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),0x03);
|
||||
std::cerr<<"[";
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),0x0c);
|
||||
std::cerr<<stage;
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),0x03);
|
||||
std::cerr<<"] ";
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),scrinfo.wAttributes);
|
||||
#else
|
||||
std::cerr<<"\033[36m[\033[91m"<<stage<<"\033[36m]\033[0m ";
|
||||
#endif
|
||||
}
|
||||
public:
|
||||
nasal_err():error(0){}
|
||||
void err(const char* stage,const string& info)
|
||||
{
|
||||
++error;
|
||||
std::cerr<<"["<<stage<<"] "<<info<<"\n";
|
||||
printstg(stage);
|
||||
std::cerr<<info<<"\n";
|
||||
}
|
||||
void err(const char* stage,u32 line,u32 column,const string& info)
|
||||
{
|
||||
++error;
|
||||
const string& code=res[line-1];
|
||||
std::cerr<<"["<<stage<<"] "<<file<<":"<<line<<":"<<column<<" "<<info<<"\n"<<code<<"\n";
|
||||
printstg(stage);
|
||||
std::cerr<<file<<":"<<line<<":"<<column<<" "<<info<<"\n"<<code<<"\n";
|
||||
for(i32 i=0;i<(i32)column-1;++i)
|
||||
std::cerr<<char(" \t"[code[i]=='\t']);
|
||||
std::cerr<<"^\n";
|
||||
@@ -64,7 +86,8 @@ public:
|
||||
void err(const char* stage,u32 line,const string& info)
|
||||
{
|
||||
++error;
|
||||
std::cerr<<"["<<stage<<"] "<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
|
||||
printstg(stage);
|
||||
std::cerr<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
|
||||
}
|
||||
void chkerr(){if(error)std::exit(1);}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user