Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #141 from dragos/make-stdout-redirect-optional-100…

…1133

Make standard output/error redirection optional.
  • Loading branch information...
commit c61528c30d80ef3076d723d359ad9509f78adfc2 2 parents e45cd67 + a8e7fb2
@dragos dragos authored
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  ...e.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  ...la-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)
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.