Skip to content

Commit

Permalink
extended print and syslog with color support
Browse files Browse the repository at this point in the history
needs linux or atleast windows11
  • Loading branch information
turleypol committed Sep 5, 2023
1 parent 8d829cd commit 2440d90
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 32 deletions.
4 changes: 3 additions & 1 deletion pol-core/clib/logfacility.h
Expand Up @@ -25,6 +25,8 @@ extern bool LogfileTimestampEveryLine;

namespace Logging
{
constexpr char CONSOLE_RESET_COLOR[] = "\x1b[0m";

struct LogFileBehaviour;
class LogFacility;

Expand Down Expand Up @@ -194,7 +196,7 @@ void initLogging( LogFacility* logger ); // initalize the logging


// several helper defines
//#define DEBUG_LOG_PRINTS
// #define DEBUG_LOG_PRINTS
#ifdef DEBUG_LOG_PRINTS
#ifdef WINDOWS
#define __FILENAME__ ( strrchr( __FILE__, '\\' ) ? strrchr( __FILE__, '\\' ) + 1 : __FILE__ )
Expand Down
24 changes: 17 additions & 7 deletions pol-core/pol/module/basiciomod.cpp
Expand Up @@ -5,13 +5,13 @@


#include "basiciomod.h"
#include "../../clib/logfacility.h"
#include "bscript/berror.h"
#include "bscript/impstr.h"
#include "clib/logfacility.h"

#include <module_defs/basicio.h>

namespace Pol
{
namespace Module
namespace Pol::Module
{
BasicIoExecutorModule::BasicIoExecutorModule( Bscript::Executor& exec )
: Bscript::TmplExecutorModule<BasicIoExecutorModule, Bscript::ExecutorModule>( exec )
Expand All @@ -20,8 +20,18 @@ BasicIoExecutorModule::BasicIoExecutorModule( Bscript::Executor& exec )

Bscript::BObjectImp* BasicIoExecutorModule::mf_Print()
{
INFO_PRINT << exec.getParamImp( 0 )->getStringRep() << "\n";
const Bscript::String* color;
if ( !exec.getStringParam( 1, color ) )
return new Bscript::BError( "Invalid parameter type" );
if ( color->length() )
{
INFO_PRINT << color->value() << exec.getParamImp( 0 )->getStringRep()
<< Clib::Logging::CONSOLE_RESET_COLOR << "\n";
}
else
{
INFO_PRINT << exec.getParamImp( 0 )->getStringRep() << "\n";
}
return new Bscript::UninitObject;
}
} // namespace Module
} // namespace Pol
} // namespace Pol::Module
25 changes: 22 additions & 3 deletions pol-core/pol/module/osmod.cpp
Expand Up @@ -451,16 +451,35 @@ BObjectImp* OSExecutorModule::mf_SysLog()
{
BObjectImp* imp = exec.getParamImp( 0 );
int log_verbose;
if ( !exec.getParam( 1, log_verbose ) )
const String* color;
if ( !exec.getParam( 1, log_verbose ) || !exec.getStringParam( 2, color ) )
return new BError( "Invalid parameter type" );
std::string strval = imp->getStringRep();
if ( log_verbose )
{
POLLOG << "[" << exec.scriptname() << "]: " << strval << "\n";
INFO_PRINT << "syslog [" << exec.scriptname() << "]: " << strval << "\n";
if ( color->length() )
{
INFO_PRINT << color->value() << "syslog [" << exec.scriptname() << "]: " << strval
<< Clib::Logging::CONSOLE_RESET_COLOR << "\n";
}
else
{
INFO_PRINT << "syslog [" << exec.scriptname() << "]: " << strval << "\n";
}
}
else
POLLOG_INFO << strval << "\n";
{
if ( color->length() )
{
POLLOG << strval << "\n";
INFO_PRINT << color->value() << strval << Clib::Logging::CONSOLE_RESET_COLOR << "\n";
}
else
{
POLLOG_INFO << strval << "\n";
}
}
return new BLong( 1 );
}

Expand Down
10 changes: 9 additions & 1 deletion pol-core/support/scripts/basicio.em
@@ -1,4 +1,12 @@
Print( anything );
const CONSOLE_COLOR_RED:="\x1b[31m";
const CONSOLE_COLOR_GREEN:="\x1b[32m";
const CONSOLE_COLOR_YELLOW:="\x1b[33m";
const CONSOLE_COLOR_BLUE:="\x1b[34m";
const CONSOLE_COLOR_MAGENTA:="\x1b[35m";
const CONSOLE_COLOR_CYAN:="\x1b[36m";


Print( anything, console_color:="" );

// below this point not yet implemented
//input();
Expand Down
2 changes: 1 addition & 1 deletion pol-core/support/scripts/os.em
Expand Up @@ -81,7 +81,7 @@ Run_Script( script_name, param := 0 );
// syslog(text): write text to the console, and to the log file
// includes context (calling script name)
//
SysLog( text, log_verbose:=1 );
SysLog( text, log_verbose:=1, console_color:="" );
//
// clear_event_queue(): Empties the event queue of the current script.
Expand Down
2 changes: 1 addition & 1 deletion testsuite/escript/struct/struct010.exr
@@ -1 +1 @@
PC=25: some fool used operator[] on a struct, with an Integer index
some fool used operator[] on a struct, with an Integer index
36 changes: 18 additions & 18 deletions testsuite/pol/scripts/tests.src
Expand Up @@ -8,10 +8,10 @@ include "testutil";
program tests()
var test_count:=CInt(GetEnvironmentVariable("POLCORE_TEST_RUN"));
syslog(
"\n##########################\n"
+ "# starting testscripts #\n"
+ "# Run: "+test_count+" #\n"
+ "##########################\n",0);
$"\n{CONSOLE_COLOR_CYAN}##########################\n"
+ $"{CONSOLE_COLOR_CYAN}# starting testscripts #\n"
+ $"{CONSOLE_COLOR_CYAN}# Run: "+test_count+" #\n"
+ $"{CONSOLE_COLOR_CYAN}##########################\n",0,CONSOLE_COLOR_CYAN);
var exitcode:=0;
var testpkgs:={};
var restartpkg;
Expand All @@ -32,7 +32,7 @@ program tests()

var filter:=GetEnvironmentVariable("POLCORE_TEST_FILTER");
if (filter)
syslog("## using testfilter \""+filter+"\"\n",0);
syslog("## using testfilter \""+filter+"\"\n",0,CONSOLE_COLOR_CYAN);
endif

foreach pkg in testpkgs
Expand All @@ -44,16 +44,16 @@ program tests()
continue;
endif

syslog(pkg[2],0);
syslog(pkg[2],0,"\x1B[1;32m");
var scripts:=listdirectory(":{}:".format(pkg[1]),"ecl");
scripts.sort();
if ("setup.ecl" in scripts)
syslog(" Calling setup.ecl..",0);
syslog(" Calling setup.ecl..",0,CONSOLE_COLOR_GREEN);
var res:=run_script(":{}:setup.ecl".format(pkg[1]));
if (res == IGNORE_TEST)
continue;
elseif (res != 1)
syslog(" failed: "+res.errortext,0);
syslog(" failed: "+res.errortext,0,CONSOLE_COLOR_RED);
exitcode:=1;
continue;
endif
Expand All @@ -62,10 +62,10 @@ program tests()
if (file.find("test") != 1)
continue;
endif
syslog(" Calling {}..".format(file),0);
syslog(" Calling {}..".format(file),0,CONSOLE_COLOR_GREEN);
var script:=LoadExportedScript(":{}:{}".format(pkg[1], file));
if (!script[2])
syslog(" failed: "+script[2],0);
syslog(" failed: "+script[2],0,CONSOLE_COLOR_RED);
exitcode:=1;
continue;
endif
Expand All @@ -74,28 +74,28 @@ program tests()
endif

foreach func in (script[1].exported_functions)
syslog(" Calling {}..".format(func),0);
syslog(" Calling {}..".format(func),0,CONSOLE_COLOR_GREEN);
var res:=script[1].call(func);
if (res != 1)
syslog(" failed: "+res.errortext,0);
syslog(" failed: "+res.errortext,0,CONSOLE_COLOR_RED);
exitcode:=1;
continue;
endif
endforeach
endforeach
if ("cleanup.ecl" in scripts)
syslog(" Calling cleanup.ecl..",0);
syslog(" Calling cleanup.ecl..",0,CONSOLE_COLOR_GREEN);
run_script(":{}:cleanup.ecl".format(pkg[1]));
endif
endforeach

var result:="success";
var result:=$"{CONSOLE_COLOR_GREEN}success";
if (exitcode)
result:="failed ";
result:=$"{CONSOLE_COLOR_RED}failed ";
endif
syslog(
"\n###########################\n"
+ "# finished tests: {} #\n".format(result)
+ "###########################\n",0);
$"\n{CONSOLE_COLOR_CYAN}###########################\n"
+ $"{CONSOLE_COLOR_CYAN}# finished tests: {result} {CONSOLE_COLOR_CYAN}#\n"
+ $"{CONSOLE_COLOR_CYAN}###########################\n",0,CONSOLE_COLOR_CYAN);
shutdown(exitcode);
endprogram

0 comments on commit 2440d90

Please sign in to comment.