Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion EScript/Compiler/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "AST/UserFunctionExpr.h"
#include "AST/ValueExpr.h"
#include "../Objects/Callables/UserFunction.h"
#include "../Utils/Logger.h"

#include <functional>
#include <iostream>
Expand All @@ -48,7 +49,7 @@ static bool _handlerInitialized UNUSED_ATTRIBUTE = initHandler(handlerRegistry);


//! (ctor)
Compiler::Compiler(Logger * _logger) : logger(_logger ? _logger : new StdLogger(std::cout)) {
Compiler::Compiler(Logger & _logger) : logger(_logger) {
}

std::pair<ERef<UserFunction>,_CountedRef<StaticData>>
Expand Down
7 changes: 3 additions & 4 deletions EScript/Compiler/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define ES_COMPILER_H

#include "../Utils/CodeFragment.h"
#include "../Utils/Logger.h"
#include "../Utils/StringId.h"
#include "../Utils/StringData.h"
#include "../Instructions/Instruction.h"
Expand All @@ -33,7 +32,7 @@ class ASTNode;

class Compiler {
public:
Compiler(Logger * _logger = nullptr);
Compiler(Logger & _logger);

std::pair<ERef<UserFunction>,_CountedRef<StaticData>> compile(const CodeFragment & code,const std::vector<StringId>& injectedStaticVarNames);

Expand All @@ -42,9 +41,9 @@ class Compiler {
//! @name Logging
// @{
public:
Logger * getLogger()const { return logger.get(); }
Logger & getLogger()const { return logger; }
private:
_CountedRef<Logger> logger;
Logger & logger;
// @}
// -------------

Expand Down
7 changes: 4 additions & 3 deletions EScript/Compiler/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "../Consts.h"

#include "../Utils/IO/IO.h"
#include "../Utils/Logger.h"
#include "../Objects/Callables/UserFunction.h"

#include <cstdio>
Expand Down Expand Up @@ -2150,8 +2151,8 @@ ASTNode::refArray_t readExpressionsInBrackets(ParsingContext & ctxt,int & cursor
// ---------------------------------

//! (ctor)
Parser::Parser(Logger * _logger) :
logger(_logger ? _logger : new StdLogger(std::cout)) {
Parser::Parser(Logger & _logger) :
logger(_logger) {
//ctor
}

Expand All @@ -2163,7 +2164,7 @@ ERef<AST::Block> Parser::parse(const CodeFragment & code) {
tokenizer.defineToken("__DIR__",new TValueString(IO::dirname(code.getFilename())));

Tokenizer::tokenList_t tokens;
ParsingContext ctxt(tokens,code,*logger.get());
ParsingContext ctxt(tokens,code,logger);
ctxt.rootBlock = rootBlock.get();

/// 1. Tokenize
Expand Down
6 changes: 3 additions & 3 deletions EScript/Compiler/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "Tokenizer.h"

#include "../Utils/CodeFragment.h"
#include "../Utils/Logger.h"

#include <vector>

Expand All @@ -22,17 +21,18 @@
#endif

namespace EScript {
class Logger;
namespace AST{
class Block;
}

//! [Parser]
class Parser {
public:
Parser(Logger * logger = nullptr);
Parser(Logger & _logger);
ERef<AST::Block> parse(const CodeFragment & code);
private:
_CountedRef<Logger> logger;
Logger & logger;

};
}
Expand Down
31 changes: 14 additions & 17 deletions EScript/Runtime/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "../Utils/Logger.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <sstream>
#include <stack>

Expand Down Expand Up @@ -135,12 +136,12 @@ Runtime::Runtime() :
internals(new RuntimeInternals(*this,
EScript::getSGlobals()->clone(),
std::make_shared<RuntimeInternals::SharedRuntimeContext>())),
logger(new LoggerGroup(Logger::LOG_WARNING)){
logger(Logger::LOG_WARNING){

declareConstant(internals->getGlobals(),"GLOBALS",internals->getGlobals());
declareConstant(internals->getGlobals(),"SGLOBALS",EScript::getSGlobals());

logger->addLogger("coutLogger",new StdLogger(std::cout));
logger.addLogger("coutLogger", std::make_shared<StdLogger>(std::cout));
//ctor
}

Expand All @@ -155,11 +156,7 @@ Runtime::Runtime(const Runtime& other) :
}

//! (dtor)
Runtime::~Runtime() {
// declareConstant(internals->getGlobals(), "GLOBALS",nullptr); //! \todo threading: Remove this and check the effect.
internals.reset(nullptr);
//dtor
}
Runtime::~Runtime() = default;

ERef<Runtime> Runtime::_fork()const{
return new Runtime(*this);
Expand Down Expand Up @@ -247,7 +244,7 @@ size_t Runtime::getStackSize()const { return internals->getStackSize(); }

size_t Runtime::_getStackSizeLimit()const { return internals->_getStackSizeLimit(); }

void Runtime::info(const std::string & s) { logger->info(s); }
void Runtime::info(const std::string & s) { logger.info(s); }

void Runtime::setAddStackInfoToExceptions(bool b) { internals->setAddStackInfoToExceptions(b); }

Expand All @@ -261,7 +258,7 @@ void Runtime::_setStackSizeLimit(const size_t s) { internals->_setStackSizeLimit

void Runtime::setTreatWarningsAsError(bool b){
if(b){ // --> disable coutLogger and add throwLogger
Logger * coutLogger = logger->getLogger("coutLogger");
Logger * coutLogger = logger.getLogger("coutLogger");
if(coutLogger!=nullptr)
coutLogger->setMinLevel(Logger::LOG_ERROR);

Expand All @@ -272,12 +269,12 @@ void Runtime::setTreatWarningsAsError(bool b){
public:
ThrowLogger(Runtime & _rt) : Logger(LOG_PEDANTIC_WARNING,LOG_WARNING), rt(_rt){}
};
logger->addLogger("throwLogger",new ThrowLogger(*this));
logger.addLogger("throwLogger", std::make_shared<ThrowLogger>(*this));
}else{
Logger * coutLogger = logger->getLogger("coutLogger");
Logger * coutLogger = logger.getLogger("coutLogger");
if(coutLogger!=nullptr)
coutLogger->setMinLevel(Logger::LOG_ALL);
logger->removeLogger("throwLogger");
logger.removeLogger("throwLogger");
}
}
void Runtime::throwException(const std::string & s,Object * obj){ internals->throwException(s,obj); }
Expand Down Expand Up @@ -330,22 +327,22 @@ class CountingLogger : public Logger{
};

void Runtime::enableLogCounting(){
if(logger->getLogger("countingLogger")==nullptr)
logger->addLogger("countingLogger",new CountingLogger);
if(logger.getLogger("countingLogger")==nullptr)
logger.addLogger("countingLogger", std::make_shared<CountingLogger>());
}

void Runtime::disableLogCounting(){
logger->removeLogger("countingLogger");
logger.removeLogger("countingLogger");
}

void Runtime::resetLogCounter(Logger::level_t level){
CountingLogger * l = dynamic_cast<CountingLogger*>(logger->getLogger("countingLogger"));
CountingLogger * l = dynamic_cast<CountingLogger*>(logger.getLogger("countingLogger"));
if(l!=nullptr)
l->reset(level);
}

uint32_t Runtime::getLogCounter(Logger::level_t level)const{
CountingLogger * l = dynamic_cast<CountingLogger*>(logger->getLogger("countingLogger"));
auto l = dynamic_cast<const CountingLogger*>(logger.getLogger("countingLogger"));
return l==nullptr ? 0 : l->get(level);
}

Expand Down
11 changes: 6 additions & 5 deletions EScript/Runtime/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,21 @@ class Runtime : public ExtObject {
std::string getCurrentFile()const;
int getCurrentLine()const;
uint32_t getLogCounter(Logger::level_t level)const;
LoggerGroup * getLogger()const { return logger.get(); }
Logger::level_t getLoggingLevel() { return logger->getMinLevel(); }
LoggerGroup & getLogger() { return logger; }
const LoggerGroup & getLogger() const { return logger; }
Logger::level_t getLoggingLevel() { return logger.getMinLevel(); }
std::string getStackInfo();
std::string getLocalStackInfo();

void log(Logger::level_t l,const std::string & s) { logger->log(l,s); }
void log(Logger::level_t l,const std::string & s) { logger.log(l,s); }
void resetLogCounter(Logger::level_t level);

void setAddStackInfoToExceptions(bool b);
void setLoggingLevel(Logger::level_t level) { logger->setMinLevel(level); }
void setLoggingLevel(Logger::level_t level) { logger.setMinLevel(level); }
void setTreatWarningsAsError(bool b);

private:
_CountedRef<LoggerGroup> logger;
LoggerGroup logger;
// @}

};
Expand Down
2 changes: 1 addition & 1 deletion EScript/Runtime/RuntimeInternals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ void RuntimeInternals::warn(const std::string & s)const {
if(getActiveFCC()){
os<<" ('" << getActiveFCC()->getUserFunction()->getCode().getFilename() << "':~"<<getCurrentLine()<<")";
}
runtime.getLogger()->warn(os.str());
runtime.getLogger().warn(os.str());
}

// -------------------------------------------------------------
Expand Down
15 changes: 12 additions & 3 deletions EScript/Utils/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
// Licensed under the MIT License. See LICENSE file for details.
// ---------------------------------------------------------------------------------
#include "Logger.h"
#include <stdexcept>
#include <iostream>
#include <memory>
#include <stdexcept>

namespace EScript{



// ------------------------------------------------
// LoggerGroup
void LoggerGroup::addLogger(const std::string & name,Logger * logger){
if(logger==nullptr)
void LoggerGroup::addLogger(const std::string & name, const std::shared_ptr<Logger> & logger){
if (!logger) {
throw std::invalid_argument("addLogger(nullptr)");
}
loggerRegistry[name] = logger;
}

Expand All @@ -35,6 +37,13 @@ Logger * LoggerGroup::getLogger(const std::string & name){
}
return nullptr;
}
const Logger * LoggerGroup::getLogger(const std::string & name) const{
const auto lbIt = loggerRegistry.lower_bound(name);
if(lbIt!=loggerRegistry.cend() && !(loggerRegistry.key_comp()(name, lbIt->first)) ){
return lbIt->second.get();
}
return nullptr;
}

//! ---|> Logger
void LoggerGroup::doLog(level_t l,const std::string & msg){
Expand Down
12 changes: 7 additions & 5 deletions EScript/Utils/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
#include "ObjRef.h"

#include <map>
#include <string>
#include <memory>
#include <ostream>
#include <string>

namespace EScript {

//! [Logger]
class Logger : public EReferenceCounter<Logger> {
class Logger {
public:
enum level_t{
LOG_ALL = 0,
Expand Down Expand Up @@ -65,16 +66,17 @@ class Logger : public EReferenceCounter<Logger> {
class LoggerGroup : public Logger {
public:
LoggerGroup(level_t _minLevel = LOG_ALL,level_t _maxLevel = LOG_NONE) : Logger(_minLevel,_maxLevel){}
virtual ~LoggerGroup(){}
virtual ~LoggerGroup() = default;

void addLogger(const std::string & name,Logger * logger);
void addLogger(const std::string & name, const std::shared_ptr<Logger> & logger);
bool removeLogger(const std::string & name);
void clearLoggers();
Logger * getLogger(const std::string & name);
const Logger * getLogger(const std::string & name) const;
private:
//! ---|> Logger
void doLog(level_t l,const std::string & msg) override;
typedef std::map<std::string, _CountedRef<Logger> > loggerRegistry_t;
using loggerRegistry_t = std::map<std::string, std::shared_ptr<Logger>>;
loggerRegistry_t loggerRegistry;
};

Expand Down