Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make standard output/error redirection optional. #141

Merged
merged 1 commit into from

4 participants

@dragos
Owner

By default, std out and error are redirected to the log file. This makes it
difficult to do println debugging, and often each printed character ends up
on a separate line in the log file. This PR makes it an option.

Fixes #1001133

@misto
Owner

Thanks!

...core/src/scala/tools/eclipse/logging/LogManager.scala
((39 lines not shown))
+ if (event.getProperty == RedirectStdErrOut) {
+ val enable = event.getNewValue.asInstanceOf[Boolean]
+ if (enable) redirectStdOutAndStdErr()
+ else disableRedirectStdOutAndStdErr()
+
+ // we need to restart the presentation compilers so that
+ // the std out/err streams are refreshed by Console.in/out
+ if (enable != event.getOldValue.asInstanceOf[Boolean])
+ restartPCs()
+ }
+ }
+
+ /** Restart all presentation compilers in the workspace. Need to do it in order
+ * for them to pick up the new std out/err streams.
+ */
+ private def restartPCs() {
@dotta Owner
dotta added a note

I don't think this belongs here. Maybe ScalaProject ScalaPlugin object?

@dragos Owner
dragos added a note

It's for all Scala projects, so ti can't be in ScalaProject. But ScalaPlugin is a better place.

@dotta Owner
dotta added a note

Taking this further, it would be nice if you could register a listener in ScalaPlugin that listen for changes for event.getProperty == RedirectStdErrOut and restarts the PCs (this way the method restartPCs could be private to ScalaPlugin).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dragos dragos Make standard output/error redirection optional.
By default, std out and error are redirected to the log file. This makes it
difficult to do println debugging, and often each printed character ends up
on a separate line in the log file. This PR makes it an option.

Fixes #1001133
a8e7fb2
@dragos dragos merged commit c61528c into scala-ide:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 5, 2012
  1. @dragos

    Make standard output/error redirection optional.

    dragos authored
    By default, std out and error are redirected to the log file. This makes it
    difficult to do println debugging, and often each printed character ends up
    on a separate line in the log file. This PR makes it an option.
    
    Fixes #1001133
This page is out of date. Refresh to see the latest.
View
11 org.scala-ide.sdt.core/src/scala/tools/eclipse/ScalaPlugin.scala
@@ -218,6 +218,17 @@ class ScalaPlugin extends AbstractUIPlugin with PluginLogConfigurator with IReso
}
}
+ /** Restart all presentation compilers in the workspace. Need to do it in order
+ * for them to pick up the new std out/err streams.
+ */
+ def resetAllPresentationCompilers() {
+ for {
+ iProject <- ResourcesPlugin.getWorkspace.getRoot.getProjects
+ if iProject.isOpen
+ scalaProject <- asScalaProject(iProject)
+ } scalaProject.resetPresentationCompiler()
+ }
+
/**
* Return Some(ScalaProject) if the project has the Scala nature, None otherwise.
*/
View
52 org.scala-ide.sdt.core/src/scala/tools/eclipse/logging/LogManager.scala
@@ -8,38 +8,56 @@ import org.eclipse.jface.util.PropertyChangeEvent
import org.eclipse.jdt.ui.PreferenceConstants
import scala.tools.eclipse.util.SWTUtils
import java.io.File
+import org.eclipse.core.resources.ResourcesPlugin
object LogManager extends Log4JFacade with HasLogger {
import ui.properties.LoggingPreferenceConstants._
- private def updateLogLevel: IPropertyChangeListener = {
- SWTUtils.fnToPropertyChangeListener { event =>
- if (event.getProperty == LogLevel) {
- val level = event.getNewValue.asInstanceOf[String]
- setLogLevel(Level.withName(level))
- }
+ private def updateLogLevel(event: PropertyChangeEvent): Unit = {
+ if (event.getProperty == LogLevel) {
+ val level = event.getNewValue.asInstanceOf[String]
+ setLogLevel(Level.withName(level))
}
}
- private def updateConsoleAppenderStatus: IPropertyChangeListener = {
- SWTUtils.fnToPropertyChangeListener { event =>
- if (event.getProperty == IsConsoleAppenderEnabled) {
- val enable = event.getNewValue.asInstanceOf[Boolean]
- withoutConsoleRedirects {
- updateConsoleAppender(enable)
- }
+ private def updateConsoleAppenderStatus(event: PropertyChangeEvent): Unit = {
+ if (event.getProperty == IsConsoleAppenderEnabled) {
+ val enable = event.getNewValue.asInstanceOf[Boolean]
+ withoutConsoleRedirects {
+ updateConsoleAppender(enable)
}
}
}
+ private def updateStdRedirectStatus(event: PropertyChangeEvent): Unit = {
+ if (event.getProperty == RedirectStdErrOut) {
+ val enable = event.getNewValue.asInstanceOf[Boolean]
+ if (enable) redirectStdOutAndStdErr()
+ else disableRedirectStdOutAndStdErr()
+
+ // we need to restart the presentation compilers so that
+ // the std out/err streams are refreshed by Console.in/out
+ if (enable != event.getOldValue.asInstanceOf[Boolean])
+ ScalaPlugin.plugin.resetAllPresentationCompilers()
+ }
+ }
+
override protected def logFileName = "scala-ide.log"
override def configure(logOutputLocation: String, preferredLogLevel: Level.Value) {
+ import SWTUtils.fnToPropertyChangeListener
+
super.configure(logOutputLocation, preferredLogLevel)
- ScalaPlugin.plugin.getPreferenceStore.addPropertyChangeListener(updateLogLevel)
- ScalaPlugin.plugin.getPreferenceStore.addPropertyChangeListener(updateConsoleAppenderStatus)
-
- redirectStdOutAndStdErr()
+
+ val prefStore = ScalaPlugin.plugin.getPreferenceStore
+ prefStore.addPropertyChangeListener(updateLogLevel _)
+ prefStore.addPropertyChangeListener(updateConsoleAppenderStatus _)
+ prefStore.addPropertyChangeListener(updateStdRedirectStatus _)
+
+ if (prefStore.getBoolean(RedirectStdErrOut)) {
+ redirectStdOutAndStdErr()
+ ScalaPlugin.plugin.resetAllPresentationCompilers()
+ }
}
override protected def setLogLevel(level: Level.Value) {
View
4 org.scala-ide.sdt.core/src/scala/tools/eclipse/logging/StreamRedirect.scala
@@ -16,6 +16,7 @@ private[logging] object StreamRedirect {
val logger = LogManager.getLogger("System.out")
val outStream = redirect(msg => logger.debug(msg))
System.setOut(outStream)
+ Console.setOut(outStream)
isStdOutRedirected = true
}
}
@@ -23,6 +24,7 @@ private[logging] object StreamRedirect {
def disableRedirectStdOutput(): Unit = synchronized {
if(isStdOutRedirected) {
System.setOut(defaultStdOut)
+ Console.setOut(defaultStdOut)
isStdOutRedirected = false
}
}
@@ -32,6 +34,7 @@ private[logging] object StreamRedirect {
val logger = LogManager.getLogger("System.err")
val errStream = redirect(msg => logger.error(msg))
System.setErr(errStream)
+ Console.setErr(errStream)
isStdErrRedirected = true
}
}
@@ -39,6 +42,7 @@ private[logging] object StreamRedirect {
def disableRedirectStdError(): Unit = synchronized {
if(isStdErrRedirected) {
System.setErr(defaultStdErr)
+ Console.setErr(defaultStdErr)
isStdErrRedirected = false
}
}
View
1  org.scala-ide.sdt.core/src/scala/tools/eclipse/logging/ui/properties/LoggingPreferenceConstants.scala
@@ -4,4 +4,5 @@ private[logging] object LoggingPreferenceConstants {
private final val Prefix = "scala.tools.eclipse.logging.ui.properties."
final val LogLevel = Prefix + "LogLevel"
final val IsConsoleAppenderEnabled = Prefix + "ConsoleAppenderEnabled"
+ final val RedirectStdErrOut = Prefix + "RedirectSdtErrOut"
}
View
3  org.scala-ide.sdt.core/src/scala/tools/eclipse/logging/ui/properties/LoggingPreferencePage.scala
@@ -28,6 +28,7 @@ class LoggingPreferencePage extends FieldEditorPreferencePage with IWorkbenchPre
addField(new ComboFieldEditor(LoggingPreferenceConstants.LogLevel, "Log Level", namesAndValues, getFieldEditorParent))
addField(new BooleanFieldEditor(LoggingPreferenceConstants.IsConsoleAppenderEnabled, "Output log in terminal", getFieldEditorParent))
+ addField(new BooleanFieldEditor(LoggingPreferenceConstants.RedirectStdErrOut, "Redirect standard out/err to log file", getFieldEditorParent))
}
override def createContents(parent: Composite): Control = {
@@ -49,10 +50,12 @@ class LoggingPreferencePageInitializer extends AbstractPreferenceInitializer {
if(ScalaPlugin.plugin.headlessMode) {
store.setDefault(LoggingPreferenceConstants.LogLevel, Level.DEBUG.toString)
store.setDefault(LoggingPreferenceConstants.IsConsoleAppenderEnabled, true)
+ store.setDefault(LoggingPreferenceConstants.RedirectStdErrOut, false)
}
else {
store.setDefault(LoggingPreferenceConstants.LogLevel, LogManager.defaultLogLevel.toString)
store.setDefault(LoggingPreferenceConstants.IsConsoleAppenderEnabled, false)
+ store.setDefault(LoggingPreferenceConstants.RedirectStdErrOut, true)
}
}
}
Something went wrong with that request. Please try again.