diff --git a/include/eepp/system/log.hpp b/include/eepp/system/log.hpp index 737df276c..1629fcf5f 100644 --- a/include/eepp/system/log.hpp +++ b/include/eepp/system/log.hpp @@ -83,14 +83,14 @@ class EE_API Log : protected Mutex { /** @brief Writes a formated string to the log */ void writef( const char* format, ... ); - /** @returns A copy of the current writed log. */ - std::string getBuffer() const; + /** @returns A reference of the current writed log. */ + const std::string& getBuffer() const; - /** @returns If the log Writes are outputed to the terminal. */ - const bool& isConsoleOutput() const; + /** @returns If the log Writes are outputed to stdout. */ + const bool& isLoggingToStdOut() const; - /** @brief Enabled or disables to output the Writes to the terminal. */ - void setConsoleOutput( const bool& output ); + /** @brief Enabled or disables to output the Writes to stdout. */ + void setLogToStdOut( const bool& output ); /** @returns If the file is forced to flush the data on every Write call. */ const bool& isLiveWrite() const; @@ -119,13 +119,17 @@ class EE_API Log : protected Mutex { /** Sets the file path of the log file. */ void setFilePath( const std::string& filePath ); + /** @return True if the logs are being buffered in memory */ + bool getKeepLog() const; + + /** Enable/Disable to keep a copy of the logs into memory (disabled by default) */ + void setKeepLog( bool keepLog ); + static void debug( const std::string& text ) { Log::instance()->writel( LogLevel::Debug, text ); } - static void info( const std::string& text ) { - Log::instance()->writel( LogLevel::Info, text ); - } + static void info( const std::string& text ) { Log::instance()->writel( LogLevel::Info, text ); } static void notice( const std::string& text ) { Log::instance()->writel( LogLevel::Notice, text ); @@ -185,6 +189,7 @@ class EE_API Log : protected Mutex { bool mSave; bool mConsoleOutput; bool mLiveWrite; + bool mKeepLog{ false }; LogLevel mLogLevelThreshold{ getDefaultLogLevel() }; IOStreamFile* mFS; std::list mReaders; diff --git a/src/eepp/core/debug.cpp b/src/eepp/core/debug.cpp index 8e0440e13..bc58ac6e9 100644 --- a/src/eepp/core/debug.cpp +++ b/src/eepp/core/debug.cpp @@ -27,7 +27,7 @@ void eeREPORT_ASSERT( const char* File, int Line, const char* Exp ) { if ( PrintDebugInLog ) { Log::instance()->writef( LogLevel::Assert, "%s file:%s line:%d", Exp, File, Line ); - if ( !Log::instance()->isConsoleOutput() ) + if ( !Log::instance()->isLoggingToStdOut() ) printf( "ASSERT: %s file:%s line:%d", Exp, File, Line ); } else { printf( "ASSERT: %s file:%s line:%d", Exp, File, Line ); @@ -52,7 +52,7 @@ static void printBuffer( std::string& buf, bool newLine ) { #ifdef EE_COMPILER_MSVC OutputDebugStringA( buf.c_str() ); #else - if ( PrintDebugInLog && Log::instance()->isConsoleOutput() ) { + if ( PrintDebugInLog && Log::instance()->isLoggingToStdOut() ) { Log::instance()->write( buf ); return; } else { diff --git a/src/eepp/system/log.cpp b/src/eepp/system/log.cpp index 01bd4bfa3..f7bd8cde6 100644 --- a/src/eepp/system/log.cpp +++ b/src/eepp/system/log.cpp @@ -33,7 +33,7 @@ Log* Log::create( const std::string& logPath, const LogLevel& level, bool consol ms_singleton = eeNew( Log, ( logPath, level, consoleOutput, liveWrite ) ); } else { ms_singleton->setLogLevelThreshold( level ); - ms_singleton->setConsoleOutput( consoleOutput ); + ms_singleton->setLogToStdOut( consoleOutput ); ms_singleton->setLiveWrite( liveWrite ); } return ms_singleton; @@ -44,7 +44,7 @@ Log* Log::create( const LogLevel& level, bool consoleOutput, bool liveWrite ) { ms_singleton = eeNew( Log, ( "", level, consoleOutput, liveWrite ) ); } else { ms_singleton->setLogLevelThreshold( level ); - ms_singleton->setConsoleOutput( consoleOutput ); + ms_singleton->setLogToStdOut( consoleOutput ); ms_singleton->setLiveWrite( liveWrite ); } return ms_singleton; @@ -64,6 +64,14 @@ Log::Log( const std::string& logPath, const LogLevel& level, bool consoleOutput, writel( LogLevel::Info, "eepp initialized" ); } +bool Log::getKeepLog() const { + return mKeepLog; +} + +void Log::setKeepLog( bool keepLog ) { + mKeepLog = keepLog; +} + const std::string& Log::getFilePath() const { return mFilePath; } @@ -78,7 +86,7 @@ void Log::setFilePath( const std::string& filePath ) { Log::~Log() { writel( LogLevel::Info, "eepp stoped\n" ); - if ( mSave && !mLiveWrite ) { + if ( mSave && !mLiveWrite && mKeepLog ) { openFS(); mFS->write( mData.c_str(), mData.size() ); @@ -106,9 +114,11 @@ void Log::save( const std::string& filepath ) { } void Log::write( const std::string& text ) { - lock(); - mData += text; - unlock(); + if ( mKeepLog ) { + lock(); + mData += text; + unlock(); + } writeToReaders( text ); @@ -168,10 +178,12 @@ void Log::write( const LogLevel& level, const std::string& text ) { } void Log::writel( const std::string& text ) { - lock(); - mData += text; - mData += "\n"; - unlock(); + if ( mKeepLog ) { + lock(); + mData += text; + mData += "\n"; + unlock(); + } writeToReaders( text ); writeToReaders( "\n" ); @@ -236,9 +248,11 @@ void Log::writef( const char* format, ... ) { tstr.resize( n ); tstr += '\n'; - lock(); - mData += tstr; - unlock(); + if ( mKeepLog ) { + lock(); + mData += tstr; + unlock(); + } writeToReaders( tstr ); @@ -301,9 +315,11 @@ void Log::writef( const LogLevel& level, const char* format, ... ) { first = false; } - lock(); - mData += tstr; - unlock(); + if ( mKeepLog ) { + lock(); + mData += tstr; + unlock(); + } writeToReaders( tstr ); @@ -343,22 +359,22 @@ void Log::writef( const LogLevel& level, const char* format, ... ) { } } -std::string Log::getBuffer() const { +const std::string& Log::getBuffer() const { return mData; } -const bool& Log::isConsoleOutput() const { +const bool& Log::isLoggingToStdOut() const { return mConsoleOutput; } -void Log::setConsoleOutput( const bool& output ) { +void Log::setLogToStdOut( const bool& output ) { bool OldOutput = mConsoleOutput; mConsoleOutput = output; - if ( !OldOutput && output ) { + if ( !OldOutput && output && !mData.empty() ) { lock(); - std::string data( mData ); + std::string data( std::move( mData ) ); mData = ""; unlock(); write( data ); @@ -382,10 +398,8 @@ void Log::removeLogReader( LogReaderInterface* reader ) { } void Log::writeToReaders( const std::string& text ) { - for ( std::list::iterator it = mReaders.begin(); it != mReaders.end(); - ++it ) { - ( *it )->writeLog( text ); - } + for ( const auto& reader : mReaders ) + reader->writeLog( text ); } }} // namespace EE::System diff --git a/src/tests/test_all/test.cpp b/src/tests/test_all/test.cpp index f73426f84..5eea6827e 100644 --- a/src/tests/test_all/test.cpp +++ b/src/tests/test_all/test.cpp @@ -78,7 +78,7 @@ void EETest::init() { EE = Engine::instance(); Log::instance()->setLiveWrite( true ); - Log::instance()->setConsoleOutput( true ); + Log::instance()->setLogToStdOut( true ); mTranslator.loadFromString( "" " eepp" diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 0c9a82525..c1c1ed996 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -442,7 +442,8 @@ void App::initPluginManager() { mPluginManager->registerPlugin( LSPClientPlugin::Definition() ); } -void App::loadConfig( const LogLevel& logLevel, const Sizeu& displaySize, bool sync ) { +void App::loadConfig( const LogLevel& logLevel, const Sizeu& displaySize, bool sync, + bool stdOutLogs, bool disableFileLogs ) { mConfigPath = Sys::getConfigPath( "ecode" ); if ( !FileSystem::fileExists( mConfigPath ) ) FileSystem::makeDir( mConfigPath ); @@ -471,11 +472,14 @@ void App::loadConfig( const LogLevel& logLevel, const Sizeu& displaySize, bool s FileSystem::dirAddSlashAtEnd( mThemesPath ); #ifndef EE_DEBUG - Log::create( mConfigPath + "ecode.log", logLevel, false, true ); + Log::create( mConfigPath + "ecode.log", logLevel, stdOutLogs, true ); #else - Log::create( mConfigPath + "ecode.log", logLevel, true, true ); + Log::create( mConfigPath + "ecode.log", logLevel, stdOutLogs, true ); #endif + if ( disableFileLogs ) + Log::instance()->setLiveWrite( false ); + if ( !mArgs.empty() ) { std::string strargs( String::join( mArgs ) ); Log::info( "ecode starting with these command line arguments: %s", strargs.c_str() ); @@ -2843,7 +2847,8 @@ FontTrueType* App::loadFont( const std::string& name, std::string fontPath, void App::init( const LogLevel& logLevel, std::string file, const Float& pidelDensity, const std::string& colorScheme, bool terminal, bool frameBuffer, bool benchmarkMode, const std::string& css, bool health, const std::string& healthLang, - FeaturesHealth::OutputFormat healthFormat, const std::string& fileToOpen ) { + FeaturesHealth::OutputFormat healthFormat, const std::string& fileToOpen, + bool stdOutLogs, bool disableFileLogs ) { DisplayManager* displayManager = Engine::instance()->getDisplayManager(); Display* currentDisplay = displayManager->getDisplayIndex( 0 ); mDisplayDPI = currentDisplay->getDPI(); @@ -2860,7 +2865,7 @@ void App::init( const LogLevel& logLevel, std::string file, const Float& pidelDe mResPath += "assets"; FileSystem::dirAddSlashAtEnd( mResPath ); - loadConfig( logLevel, currentDisplay->getSize(), health ); + loadConfig( logLevel, currentDisplay->getSize(), health, stdOutLogs, disableFileLogs ); if ( health ) { Sys::windowAttachConsole(); @@ -3639,6 +3644,10 @@ EE_MAIN_FUNC int main( int argc, char* argv[] ) { parser, "convert-lang-output", "Sets the directory output path. If not set it will be printed to stdout", { "convert-lang-output" }, "" ); + args::Flag stdOutLogs( parser, "stdout-logs", "Redirects all logs to stdout", + { "stdout-logs" } ); + args::Flag disableFileLogs( parser, "disable-file-logs", "Disables writing logs to a log file", + { "disable-file-logs" } ); std::vector args; try { @@ -3702,14 +3711,14 @@ EE_MAIN_FUNC int main( int argc, char* argv[] ) { } if ( verbose.Get() ) - Log::instance()->setConsoleOutput( true ); + Log::instance()->setLogToStdOut( true ); appInstance = eeNew( App, ( jobs, args ) ); appInstance->init( logLevel.Get(), folder ? folder.Get() : fileOrFolderPos.Get(), pixelDenstiyConf ? pixelDenstiyConf.Get() : 0.f, prefersColorScheme ? prefersColorScheme.Get() : "", terminal.Get(), fb.Get(), benchmarkMode.Get(), css.Get(), health || healthLang, healthLang.Get(), - healthFormat.Get(), file.Get() ); + healthFormat.Get(), file.Get(), stdOutLogs.Get(), disableFileLogs.Get() ); eeSAFE_DELETE( appInstance ); Engine::destroySingleton(); diff --git a/src/tools/ecode/ecode.hpp b/src/tools/ecode/ecode.hpp index 1ce0c2f80..543faedf9 100644 --- a/src/tools/ecode/ecode.hpp +++ b/src/tools/ecode/ecode.hpp @@ -34,7 +34,8 @@ class App : public UICodeEditorSplitter::Client { void init( const LogLevel& logLevel, std::string file, const Float& pidelDensity, const std::string& colorScheme, bool terminal, bool frameBuffer, bool benchmarkMode, const std::string& css, bool health, const std::string& healthLang, - ecode::FeaturesHealth::OutputFormat healthFormat, const std::string& fileToOpen ); + ecode::FeaturesHealth::OutputFormat healthFormat, const std::string& fileToOpen, + bool stdOutLogs, bool disableFileLogs ); void createWidgetInspector(); @@ -56,7 +57,8 @@ class App : public UICodeEditorSplitter::Client { void runCommand( const std::string& command ); - void loadConfig( const LogLevel& logLevel, const Sizeu& displaySize, bool sync ); + void loadConfig( const LogLevel& logLevel, const Sizeu& displaySize, bool sync, + bool stdOutLogs, bool disableFileLogs ); void saveConfig(); diff --git a/src/tools/eterm/eterm.cpp b/src/tools/eterm/eterm.cpp index 57f7a7d0f..64fc39462 100644 --- a/src/tools/eterm/eterm.cpp +++ b/src/tools/eterm/eterm.cpp @@ -124,7 +124,7 @@ void mainLoop() { EE_MAIN_FUNC int main( int argc, char* argv[] ) { #ifdef EE_DEBUG - Log::instance()->setConsoleOutput( true ); + Log::instance()->setLogToStdOut( true ); Log::instance()->setLiveWrite( true ); #endif args::ArgumentParser parser( "eterm" ); diff --git a/src/tools/uieditor/uieditor.cpp b/src/tools/uieditor/uieditor.cpp index 39a5fcccc..262b0e394 100644 --- a/src/tools/uieditor/uieditor.cpp +++ b/src/tools/uieditor/uieditor.cpp @@ -1194,7 +1194,7 @@ void App::init( const Float& pixelDensityConf, const bool& useAppTheme, const st pixelDensity = pixelDensityConf; Log::instance()->setLiveWrite( true ); - Log::instance()->setConsoleOutput( true ); + Log::instance()->setLogToStdOut( true ); mResPath = Sys::getProcessPath();