import sys
import sg_script_api

old_stdout = None
old_stderr = None

destOutputObj = None

class RedirectStdout:
	def __init__(self, stdout):
		self.stdout = stdout

	def write(self, s):
		#old_stdout.write(s)
		destOutputObj.scriptStdoutMsg(s)

class RedirectStderr:
	def __init__(self, stderr):
		self.stderr = stderr

	def write(self, s):
		#old_stderr.write(s)
		#return
		destOutputObj.scriptStderrMsg(s)


# redirect standard output (including the print statement)
old_stdout = sys.stdout
old_stderr = sys.stderr

def redirect(dest):
	global destOutputObj
	
	destOutputObj = dest
	sys.stdout = RedirectStdout(sys.stdout)
	sys.stderr = RedirectStderr(sys.stderr)

import msg

def setInterceptor(interceptor):
	g = msg.PyMsgGlobal.instance()
	g.setInterceptor(interceptor)

def simulationTime():
	g = msg.PyMsgGlobal.instance()
	return g.getClock()
	
def startSimulation():
	return

def stopSimulation():
	g = msg.PyMsgGlobal.instance()
	g.killAllProcesses(True)
	
def setDebugMode(mode):
	g = msg.PyMsgGlobal.instance()
	if mode:
		g.setReportMode(msg.PyMsgGlobal._report_debug)
	else:
		g.setReportMode(msg.PyMsgGlobal._report_key_functions)
