tracerVerbose.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from sccd.runtime.tracers.tracerBase import BaseTracer
  2. import sys
  3. class TracerVerbose(BaseTracer):
  4. """
  5. A tracer for simple verbose output
  6. """
  7. def __init__(self, filename):
  8. """
  9. Constructor
  10. :param uid: the UID of this tracer
  11. :param server: the server to make remote calls on
  12. :param filename: file to save the trace to, can be None for output to stdout
  13. """
  14. super(TracerVerbose, self).__init__()
  15. self.filename = filename
  16. self.prevtime = -1
  17. self.text = ""
  18. def startTracer(self, recover):
  19. """
  20. Starts up the tracer
  21. :param recover: whether or not this is a recovery call (so whether or not the file should be appended to)
  22. """
  23. if self.filename is None:
  24. self.verb_file = sys.stdout
  25. elif recover:
  26. self.verb_file = open(self.filename, 'a+')
  27. else:
  28. self.verb_file = open(self.filename, 'w')
  29. def stopTracer(self):
  30. """
  31. Stop the tracer
  32. """
  33. self.verb_file.flush()
  34. def trace(self, time):
  35. """
  36. Actual tracing function
  37. :param time: time at which this trace happened
  38. :param text: the text that was traced
  39. """
  40. string = ""
  41. if time > self.prevtime:
  42. string = ("__ Current Time: %10.6f " + "_"*42 + " \n") % (time / 1000)
  43. self.prevtime = time
  44. string += self.text + "\n"
  45. try:
  46. self.verb_file.write(string)
  47. except TypeError:
  48. self.verb_file.write(string.encode())
  49. self.text = ""
  50. def traceExitState(self, StateChart, State):
  51. self.text += "\n"
  52. self.text += "EXIT STATE in model <%s>\n" % StateChart
  53. self.text += "\t\tState: %s\n" % str(State)
  54. # TODO: This should be different because can also be written to a file, the DEVS implementation uses a server which i'm not going to do
  55. #print(text)
  56. def traceEnterState(self, StateChart, State):
  57. self.text += "\n"
  58. self.text += "ENTER STATE in model <%s>\n" % StateChart
  59. self.text += "\t\tState: %s\n" % str(State)
  60. #print(text)
  61. def traceOutput(self, event):
  62. self.text += "\n"
  63. self.text += "OUTPUT EVENT to port <%s>\n" % event.port
  64. self.text += "\t\Event: %s\n" % str(event)
  65. #print(text)
  66. def traceInput(self, listener, event):
  67. self.text += "\n"
  68. self.text += "INPUT EVENT from port <%s>\n" % event.port
  69. self.text += "\t\Type: %s\n" % listener.virtual_name
  70. self.text += "\t\Event: %s\n" % str(event)
  71. #print(text)
  72. def traceTransition(self, aStateChart):
  73. """
  74. Tracing done for the internal transition function
  75. :param aDEVS: the model that transitioned
  76. """
  77. pass
  78. def traceExternal(self, aClass):
  79. """
  80. Tracing done for the external transition function
  81. :param aDEVS: the model that transitioned
  82. """
  83. pass
  84. def traceInit(self, aDEVS, t):
  85. """
  86. Tracing done for the initialisation
  87. :param aDEVS: the model that was initialised
  88. :param t: time at which it should be traced
  89. """
  90. pass