From e49ea868e39afd8b5b3d74708997f2b3c705b0b7 Mon Sep 17 00:00:00 2001 From: vsr Date: Mon, 22 Sep 2014 17:59:44 +0400 Subject: [PATCH] 0022652: [CEA 1194] Redirect the traces from embedded Python console in a log file Additional changes: - Implement Start/Stop Log commands to save run-time log instead of saving static snapshot of the log. --- src/PyConsole/PyConsole_Console.cxx | 24 +++-- src/PyConsole/PyConsole_Console.h | 15 +-- src/PyConsole/PyConsole_Editor.cxx | 110 ++++++++++++++------ src/PyConsole/PyConsole_Editor.h | 13 +-- src/PyConsole/resources/PyConsole_msg_en.ts | 14 ++- src/PyConsole/resources/PyConsole_msg_fr.ts | 12 ++- src/PyConsole/resources/PyConsole_msg_ja.ts | 12 ++- 7 files changed, 139 insertions(+), 61 deletions(-) diff --git a/src/PyConsole/PyConsole_Console.cxx b/src/PyConsole/PyConsole_Console.cxx index c613866d1..f386ebfdb 100644 --- a/src/PyConsole/PyConsole_Console.cxx +++ b/src/PyConsole/PyConsole_Console.cxx @@ -248,7 +248,10 @@ void PyConsole_Console::contextMenuPopup( QMenu* menu ) menu->addAction( myActions[SelectAllId] ); menu->addSeparator(); menu->addAction( myActions[DumpCommandsId] ); - menu->addAction( myActions[SaveLogId] ); + if ( !myEditor->isLogging() ) + menu->addAction( myActions[StartLogId] ); + else + menu->addAction( myActions[StopLogId] ); Qtx::simplifySeparators( menu ); @@ -270,7 +273,8 @@ void PyConsole_Console::setMenuActions( const int flags ) myActions[ClearId]->setVisible( flags & ClearId ); myActions[SelectAllId]->setVisible( flags & SelectAllId ); myActions[DumpCommandsId]->setVisible( flags & DumpCommandsId ); - myActions[SaveLogId]->setVisible( flags & SaveLogId ); + myActions[StartLogId]->setVisible( flags & StartLogId ); + myActions[StopLogId]->setVisible( flags & StopLogId ); } /*! @@ -286,7 +290,8 @@ int PyConsole_Console::menuActions() const ret = ret | ( myActions[ClearId]->isVisible() ? ClearId : 0 ); ret = ret | ( myActions[SelectAllId]->isVisible() ? SelectAllId : 0 ); ret = ret | ( myActions[DumpCommandsId]->isVisible() ? DumpCommandsId : 0 ); - ret = ret | ( myActions[SaveLogId]->isVisible() ? SaveLogId : 0 ); + ret = ret | ( myActions[StartLogId]->isVisible() ? StartLogId : 0 ); + ret = ret | ( myActions[StopLogId]->isVisible() ? StopLogId : 0 ); return ret; } @@ -322,10 +327,15 @@ void PyConsole_Console::createActions() connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( dump() ) ); myActions.insert( DumpCommandsId, a ); - a = new QAction( tr( "EDIT_SAVELOG_CMD" ), this ); - a->setStatusTip( tr( "EDIT_SAVELOG_CMD" ) ); - connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( saveLog() ) ); - myActions.insert( SaveLogId, a ); + a = new QAction( tr( "EDIT_STARTLOG_CMD" ), this ); + a->setStatusTip( tr( "EDIT_STARTLOG_CMD" ) ); + connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( startLog() ) ); + myActions.insert( StartLogId, a ); + + a = new QAction( tr( "EDIT_STOPLOG_CMD" ), this ); + a->setStatusTip( tr( "EDIT_STOPLOG_CMD" ) ); + connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( stopLog() ) ); + myActions.insert( StopLogId, a ); } /*! diff --git a/src/PyConsole/PyConsole_Console.h b/src/PyConsole/PyConsole_Console.h index 783c6567a..de909960d 100644 --- a/src/PyConsole/PyConsole_Console.h +++ b/src/PyConsole/PyConsole_Console.h @@ -44,13 +44,14 @@ public: //! Context popup menu actions flags enum { - CopyId = 0x01, //!< "Copy" menu action - PasteId = 0x02, //!< "Paste" menu action - ClearId = 0x04, //!< "Clear" menu action - SelectAllId = 0x08, //!< "Select All" menu action - DumpCommandsId = 0x10, //!< "DumpCommands" menu action - SaveLogId = 0x20, //!< "Save log" menu action - All = CopyId | PasteId | ClearId | SelectAllId | DumpCommandsId | SaveLogId //!< all menu actions + CopyId = 0x01, //!< "Copy" menu action + PasteId = 0x02, //!< "Paste" menu action + ClearId = 0x04, //!< "Clear" menu action + SelectAllId = 0x08, //!< "Select All" menu action + DumpCommandsId = 0x10, //!< "DumpCommands" menu action + StartLogId = 0x20, //!< "Start log" menu action + StopLogId = 0x40, //!< "Stop log" menu action + All = 0xFF, //!< all menu actions }; public: diff --git a/src/PyConsole/PyConsole_Editor.cxx b/src/PyConsole/PyConsole_Editor.cxx index c9542464a..be9ba6b98 100644 --- a/src/PyConsole/PyConsole_Editor.cxx +++ b/src/PyConsole/PyConsole_Editor.cxx @@ -138,14 +138,20 @@ bool DumpCommandsFileValidator::canSave(const QString& file, bool permissions) void staticCallbackStdout( void* data, char* c ) { - if(!((PyConsole_Editor*)data)->isSuppressOutput()) - QApplication::postEvent( (PyConsole_Editor*)data, new PrintEvent( QString::fromUtf8(c), false ) ); + if(!((PyConsole_Editor*)data)->isSuppressOutput()) { + PyConsole_Editor* e = (PyConsole_Editor*)data; + e->putLog( QString::fromUtf8(c) ); + QApplication::postEvent( e, new PrintEvent( QString::fromUtf8(c), false ) ); + } } void staticCallbackStderr( void* data, char* c ) { - if(!((PyConsole_Editor*)data)->isSuppressOutput()) - QApplication::postEvent( (PyConsole_Editor*)data, new PrintEvent( QString::fromUtf8(c), true ) ); + if(!((PyConsole_Editor*)data)->isSuppressOutput()) { + PyConsole_Editor* e = (PyConsole_Editor*)data; + e->putLog( QString::fromUtf8(c) ); + QApplication::postEvent( e, new PrintEvent( QString::fromUtf8(c), true ) ); + } } @@ -272,6 +278,17 @@ void PyConsole_Editor::setIsShowBanner( const bool on ) } } +/*! + \brief Check if trace logging is switched on. + + \sa startLog(), stopLog() + \return \c true if trace logging is switched on +*/ +bool PyConsole_Editor::isLogging() const +{ + return !myLogFile.isEmpty(); +} + /*! \brief Get size hint for the Python console window \return size hint value @@ -339,13 +356,10 @@ void PyConsole_Editor::exec( const QString& command ) if ( !cmd.endsWith( "\n" ) ) cmd += "\n"; QStringList lines = command.split( "\n" ); for ( int i = 0; i < lines.size(); i++ ) { - if ( !lines[i].trimmed().isEmpty() ) { - PyCommand aCommand; - aCommand.command = lines[i]; - aCommand.prompt = i == 0 ? READY_PROMPT : DOTS_PROMPT; - myHistory.append( aCommand ); - } + if ( !lines[i].trimmed().isEmpty() ) + myHistory.push_back( lines[i] ); addText( ( i == 0 ? READY_PROMPT : DOTS_PROMPT ) + lines[i], i != 0 ); + putLog( QString( "%1%2\n" ).arg( i == 0 ? READY_PROMPT : DOTS_PROMPT ).arg( lines[i] ) ); } // IPAL20182 addText( "", true ); @@ -413,12 +427,9 @@ void PyConsole_Editor::handleReturn() // extend the command buffer with the current command myCommandBuffer.append( cmd ); // add command to the history - if ( !cmd.trimmed().isEmpty() ) { - PyCommand aCommand; - aCommand.command = cmd; - aCommand.prompt = myPrompt; - myHistory.append( aCommand ); - } + if ( !cmd.trimmed().isEmpty() ) + myHistory.push_back( cmd ); + putLog( QString( "%1%2\n" ).arg( myPrompt ).arg( cmd ) ); // IPAL19397 addText( "", true ); @@ -604,7 +615,7 @@ void PyConsole_Editor::keyPressEvent( QKeyEvent* event ) if ( myCmdInHistory > 0 ) { myCmdInHistory--; // get previous command in the history - QString previousCommand = myHistory.at( myCmdInHistory ).command; + QString previousCommand = myHistory.at( myCmdInHistory ); // print previous command moveCursor( QTextCursor::End ); moveCursor( QTextCursor::StartOfBlock, QTextCursor::KeepAnchor ); @@ -641,7 +652,7 @@ void PyConsole_Editor::keyPressEvent( QKeyEvent* event ) QString nextCommand; if ( myCmdInHistory < myHistory.count() ) { // next command in history - nextCommand = myHistory.at( myCmdInHistory ).command; + nextCommand = myHistory.at( myCmdInHistory ); } else { // end of history is reached @@ -748,7 +759,7 @@ void PyConsole_Editor::keyPressEvent( QKeyEvent* event ) if ( myCmdInHistory > 0 ) { myCmdInHistory = 0; // get very first command in the history - QString firstCommand = myHistory.at( myCmdInHistory ).command; + QString firstCommand = myHistory.at( myCmdInHistory ); // print first command moveCursor( QTextCursor::End ); moveCursor( QTextCursor::StartOfBlock, QTextCursor::KeepAnchor ); @@ -943,7 +954,6 @@ void PyConsole_Editor::customEvent( QEvent* event ) { PrintEvent* pe=(PrintEvent*)event; addText( pe->text(), false, pe->isError()); - myHistory.last().output = myHistory.last().output + pe->text(); return; } case PyInterp_Event::ES_OK: @@ -1135,7 +1145,7 @@ void PyConsole_Editor::dump() QTextStream out (&file); for ( int i=0; i myHistory; //!< commands history buffer + QString myLogFile; //!< current output log + QStringList myHistory; //!< commands history buffer QEventLoop* myEventLoop; //!< internal event loop QString myBanner; //!< current banner bool myShowBanner; //!< 'show banner' flag diff --git a/src/PyConsole/resources/PyConsole_msg_en.ts b/src/PyConsole/resources/PyConsole_msg_en.ts index a4818b95b..3a3b89bd9 100644 --- a/src/PyConsole/resources/PyConsole_msg_en.ts +++ b/src/PyConsole/resources/PyConsole_msg_en.ts @@ -25,11 +25,15 @@ EDIT_DUMPCOMMANDS_CMD - D&ump commands + D&ump Commands - EDIT_SAVELOG_CMD - &Save log + EDIT_STARTLOG_CMD + Start &Log + + + EDIT_STOPLOG_CMD + Stop &Log @@ -50,5 +54,9 @@ LOG_FILES_FILTER Log files (*.log *.txt) + + ERR_FILE_NOT_WRITABLE + File is not writable! + diff --git a/src/PyConsole/resources/PyConsole_msg_fr.ts b/src/PyConsole/resources/PyConsole_msg_fr.ts index 92dd76dde..4446aa9bd 100755 --- a/src/PyConsole/resources/PyConsole_msg_fr.ts +++ b/src/PyConsole/resources/PyConsole_msg_fr.ts @@ -28,8 +28,12 @@ &Générer le script des commandes - EDIT_SAVELOG_CMD - &Save log + EDIT_STARTLOG_CMD + Start &Log + + + EDIT_STOPLOG_CMD + Stop &Log @@ -50,5 +54,9 @@ LOG_FILES_FILTER Log files (*.log *.txt) + + ERR_FILE_NOT_WRITABLE + File is not writable! + diff --git a/src/PyConsole/resources/PyConsole_msg_ja.ts b/src/PyConsole/resources/PyConsole_msg_ja.ts index 23de7907e..c993c7cc5 100644 --- a/src/PyConsole/resources/PyConsole_msg_ja.ts +++ b/src/PyConsole/resources/PyConsole_msg_ja.ts @@ -28,8 +28,12 @@ スクリプト コマンドを生成します。(&u) - EDIT_SAVELOG_CMD - &Save log + EDIT_STARTLOG_CMD + Start &Log + + + EDIT_STOPLOG_CMD + Stop &Log @@ -50,5 +54,9 @@ LOG_FILES_FILTER Log files (*.log *.txt) + + ERR_FILE_NOT_WRITABLE + File is not writable! + -- 2.39.2