🎨 improve error log output format.

This commit is contained in:
ValKmjolnir 2022-09-02 02:04:03 +08:00
parent aa0023a23b
commit 5715c1df5f
6 changed files with 44 additions and 33 deletions

View File

@ -93,7 +93,7 @@ nas_ref builtin_system(nas_ref* local,nasal_gc& gc)
{
nas_ref str=local[1];
if(str.type!=vm_str)
return nas_err("system","\"str\" must be string");
return {vm_num,(f64)-1};
return {vm_num,(f64)system(str.str().c_str())};
}
nas_ref builtin_input(nas_ref* local,nasal_gc& gc)
@ -110,7 +110,7 @@ nas_ref builtin_fin(nas_ref* local,nasal_gc& gc)
{
nas_ref val=local[1];
if(val.type!=vm_str)
return nas_err("io.fin","\"filename\" must be string");
return nas_err("io::fin","\"filename\" must be string");
std::ifstream fin(val.str(),std::ios::binary);
std::stringstream rd;
if(!fin.fail())
@ -122,12 +122,12 @@ nas_ref builtin_fout(nas_ref* local,nasal_gc& gc)
nas_ref val=local[1];
nas_ref str=local[2];
if(val.type!=vm_str)
return nas_err("io.fout","\"filename\" must be string");
return nas_err("io::fout","\"filename\" must be string");
if(str.type!=vm_str)
return nas_err("io.fout","\"str\" must be string");
return nas_err("io::fout","\"str\" must be string");
std::ofstream fout(val.str());
if(fout.fail())
return nas_err("io.fout","cannot open <"+val.str()+">");
return nas_err("io::fout","cannot open <"+val.str()+">");
fout<<str.str();
return nil;
}
@ -389,8 +389,7 @@ nas_ref builtin_die(nas_ref* local,nasal_gc& gc)
nas_ref str=local[1];
if(str.type!=vm_str)
return nas_err("die","\"str\" must be string");
std::cerr<<bold_cyan<<"[vm] "
<<bold_red<<"error: "
std::cerr<<bold_red<<"[vm] error: "
<<bold_white<<str.str()<<'\n'
<<reset;
return {vm_none};
@ -399,11 +398,7 @@ nas_ref builtin_find(nas_ref* local,nasal_gc& gc)
{
nas_ref needle=local[1];
nas_ref haystack=local[2];
if(needle.type!=vm_str)
return nas_err("find","\"needle\" must be string");
if(haystack.type!=vm_str)
return nas_err("find","\"haystack\" must be string");
usize pos=haystack.str().find(needle.str());
usize pos=haystack.tostr().find(needle.tostr());
if(pos==string::npos)
return {vm_num,(f64)-1};
return {vm_num,(f64)pos};
@ -479,10 +474,8 @@ nas_ref builtin_cmp(nas_ref* local,nasal_gc& gc)
{
nas_ref a=local[1];
nas_ref b=local[2];
if(a.type!=vm_str)
return nas_err("cmp","\"a\" must be string");
if(b.type!=vm_str)
return nas_err("cmp","\"b\" must be string");
if(a.type!=vm_str || b.type!=vm_str)
return nas_err("cmp","\"a\" and \"b\" must be string");
return {vm_num,(f64)strcmp(a.str().c_str(),b.str().c_str())};
}
nas_ref builtin_chr(nas_ref* local,nasal_gc& gc)

View File

@ -1,6 +1,7 @@
#ifndef __NASAL_CODEGEN_H__
#define __NASAL_CODEGEN_H__
#include "nasal_err.h"
#include <iomanip>
#include <list>
#include <stack>
@ -136,7 +137,7 @@ void opcode::print(const char* header,
const u32 index,
bool deftnum=false) const
{
std::cout<<header<<std::hex<<"0x"
std::cout<<bold_cyan<<header<<reset<<std::hex<<"0x"
<<std::setw(8)<<std::setfill('0')<<index<<": "
<<std::setw(2)<<std::setfill('0')<<(u32)op<<" "
<<std::setw(2)<<std::setfill('0')<<((num>>24)&0xff)<<" "

View File

@ -1,6 +1,7 @@
#ifndef __NASAL_DBG_H__
#define __NASAL_DBG_H__
#include "nasal_err.h"
#include "nasal_vm.h"
#include <algorithm>
@ -119,7 +120,7 @@ void nasal_dbg::stepinfo()
begin=(line>>3)==0?0:((line>>3)<<3);
end=(1+(line>>3))<<3;
for(u32 i=begin;i<end && i<src.size();++i)
std::cout<<(i==line?"--> ":" ")<<src[i]<<"\n";
std::cout<<bold_cyan<<(i==line?"--> ":" ")<<reset<<src[i]<<"\n";
std::cout<<"next bytecode:\n";
begin=(pc>>3)==0?0:((pc>>3)<<3);
end=(1+(pc>>3))<<3;
@ -134,7 +135,8 @@ void nasal_dbg::interact()
if(bytecode[pc].op==op_intg)
{
std::cout
<<"[debug] nasal debug mode\n"
<<bold_cyan<<"[debug] "<<reset
<<"nasal debug mode\n"
<<"input \'h\' to get help\n";
}
else if(bytecode[pc].op==op_exit)
@ -308,7 +310,7 @@ vmexit:
callsort(count);
gc.clear();
imm.clear();
std::cout<<"[debug] debugger exited\n";
std::cout<<bold_cyan<<"[debug] "<<reset<<"debugger exited\n";
return;
#ifndef _MSC_VER
#define dbg(op,num) {\

View File

@ -77,7 +77,7 @@ public:
std::ifstream fin(f,std::ios::binary);
if(fin.fail())
{
std::cerr<<bold_cyan<<"[src] "<<reset<<"cannot open file <"<<f<<">\n";
std::cerr<<bold_red<<"src: "<<reset<<"cannot open <"<<f<<">\n";
std::exit(1);
}
string line;
@ -101,30 +101,47 @@ class nasal_err:public fstreamline
{
private:
u32 error;
string identation(usize len)
{
string tmp="";
tmp.resize(len,' ');
return tmp;
}
public:
nasal_err():error(0){}
void err(const char* stage,const string& info)
{
++error;
std::cerr<<bold_cyan<<"["<<stage<<"] "<<reset<<info<<"\n";
std::cerr<<bold_red<<stage<<": "
<<bold_white<<info<<reset<<"\n\n";
}
void err(const char* stage,u32 line,u32 column,const string& info)
{
++error;
const string& code=res[line-1];
std::cerr<<bold_cyan<<"["<<stage<<"] "
<<bold_orange<<file<<":"<<line<<":"<<column<<" "
<<bold_white<<info<<reset<<"\n"<<code<<"\n";
const string ident=identation(std::to_string(line).length());
std::cerr<<bold_red<<stage<<": "
<<bold_white<<info<<reset<<"\n"
<<bold_cyan<<" --> "<<reset
<<bold_orange<<file<<":"<<line<<":"<<column<<"\n"
<<bold_cyan<<ident<<" | "<<reset<<"\n"
<<bold_cyan<<line<<" | "<<reset<<code<<"\n"
<<bold_cyan<<ident<<" | "<<reset;
for(i32 i=0;i<(i32)column-1;++i)
std::cerr<<char(" \t"[code[i]=='\t']);
std::cerr<<bold_red<<"^\n"<<reset;
std::cerr<<bold_red<<"^ "<<info<<reset<<"\n\n";
}
void err(const char* stage,u32 line,const string& info)
{
++error;
std::cerr<<bold_cyan<<"["<<stage<<"] "
<<bold_orange<<file<<":"<<line<<" "
<<bold_white<<info<<reset<<"\n"<<res[line-1]<<'\n';
const string ident=identation(std::to_string(line).length());
std::cerr<<bold_red<<stage<<": "
<<bold_white<<info<<reset<<"\n"
<<bold_cyan<<" --> "<<reset
<<bold_orange<<file<<":"<<line<<"\n"
<<bold_cyan<<ident<<" | "<<reset<<"\n"
<<bold_cyan<<line<<" | "<<reset<<res[line-1]<<"\n"
<<bold_cyan<<ident<<" | "<<reset<<"\n\n";
}
void chkerr(){if(error)std::exit(1);}
};

View File

@ -723,8 +723,7 @@ void nasal_gc::ctxreserve()
// use to print error log and return error value
nas_ref nas_err(const string& err_f,const string& info)
{
std::cerr<<bold_cyan<<"[vm] "
<<bold_red<<err_f<<": "
std::cerr<<bold_red<<"[vm] "<<err_f<<": "
<<bold_white<<info<<"\n"
<<reset;
return {vm_none};

View File

@ -317,8 +317,7 @@ void nasal_vm::detail()
[[noreturn]]
void nasal_vm::die(const string& str)
{
std::cout<<bold_cyan<<"[vm] "
<<bold_red<<"error: "
std::cout<<bold_red<<"[vm] error: "
<<bold_white<<str<<"\n"
<<reset;
traceback();