public void ReportLogToConsole_Does_Nothing_If_Logging_Event_Handled() { using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); string testMessage = "Test message"; LoggerReporter reporter = new LoggerReporter(); LoggerListener.SubscribeListener(reporter); //Check for event LoggerEventArgs arg = new LoggerEventArgs(new MessengerEventArgs(testMessage)); reporter.ThrowLoggerEvent(arg); string expected = string.Format(testMessage + Environment.NewLine); Assert.AreEqual(expected, sw.ToString()); // Clear Console output StringBuilder sb = sw.GetStringBuilder(); sb.Remove(0, sb.Length); // Check that event does not trigger again arg.Handled = true; reporter.ThrowLoggerEvent(arg); expected = string.Empty; Assert.AreEqual(expected, sw.ToString()); } }
public void ReportLogToConsole_Logs_Parameters() { using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); string testMessage = "Test message"; string key1 = "Key1:"; string value1 = "Value1"; string key2 = "Key2:"; string value2 = "Value2"; LoggerReporter reporter = new LoggerReporter(); LoggerListener.SubscribeListener(reporter); //Check for event LoggerEventArgs arg = new LoggerEventArgs(new MessengerEventArgs(testMessage), key1, value1, key2, value2); reporter.ThrowLoggerEvent(arg); string expected = string.Format(testMessage + Environment.NewLine + key1 + " " + value1 + Environment.NewLine + key2 + " " + value2 + Environment.NewLine); Assert.AreEqual(expected, sw.ToString()); } }
public void ReportLogToConsole_Logs_Exception_With_Stacktrace() { using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); string testMessage = "Test message"; string exceptionMessage = "Exception Message"; LoggerReporter reporter = new LoggerReporter(); LoggerListener.SubscribeListener(reporter); //Check for event try { throwException(exceptionMessage); } catch (Exception expectedException) { LoggerEventArgs arg = new LoggerEventArgs(expectedException, new MessengerEventArgs(testMessage)); reporter.ThrowLoggerEvent(arg); } string expectedWithoutStackTrace = string.Format(exceptionMessage + Environment.NewLine + testMessage + Environment.NewLine); Assert.IsTrue(sw.ToString().Contains(exceptionMessage)); Assert.IsTrue(sw.ToString().Contains(testMessage)); Assert.IsTrue(sw.ToString().Length > expectedWithoutStackTrace.Length); } }
public void SubscribeListener_Subsribes_Listener() { using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); string testMessage = "Test message"; LoggerReporter reporter = new LoggerReporter(); LoggerListener.SubscribeListener(reporter); //Check for event LoggerEventArgs arg = new LoggerEventArgs(new MessengerEventArgs(testMessage)); reporter.ThrowLoggerEvent(arg); string expected = string.Format(testMessage + Environment.NewLine); Assert.AreEqual(expected, sw.ToString()); } }
public void ReportLogToConsole_Logs_Exception_With_No_Stacktrace() { using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); string testMessage = "Test message"; string exceptionMessage = "Exception Message"; LoggerReporter reporter = new LoggerReporter(); LoggerListener.SubscribeListener(reporter); //Check for event Exception expectedException = new Exception(exceptionMessage); LoggerEventArgs arg = new LoggerEventArgs(expectedException, new MessengerEventArgs(testMessage)); reporter.ThrowLoggerEvent(arg); string expected = string.Format(exceptionMessage + Environment.NewLine + testMessage + Environment.NewLine); Assert.AreEqual(expected, sw.ToString()); } }
public static void StartInterface() { Application.Init(); Catalog.Init("monovs", "./locale"); logger = new LoggerListener(); Debug.Listeners.Add(logger); RunInTerminal = MonoToolsConfigurationManager.PauseTerminal; // Creation of the status icon string icon_resource = "monotools.png"; if (Platform.GetPlatform() == OS.Mac) { icon_resource = "mactools.png"; } status_icon = new StatusIcon(new Pixbuf(Assembly.GetExecutingAssembly(), icon_resource)); status_icon.PopupMenu += OnStatusIconPopup; status_icon.Visible = true; // Creation of status menu status_menu = new Menu(); // IP Address / Port label StringBuilder sb = new StringBuilder(); sb.Append("Listening on:"); int port = MonoToolsConfigurationManager.ServerPort; foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName())) { if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { sb.AppendFormat("\n{0}:{1}", ip, port); } } LabelMenuItem ip_label = new LabelMenuItem(sb.ToString()); ip_label.Activated += HandleIPLabelActivated; status_menu.Add(ip_label); // Separator status_menu.Add(new SeparatorMenuItem()); // Run in Terminal CheckMenuItem terminal = new CheckMenuItem("Pause Terminal"); terminal.Active = RunInTerminal; terminal.Activated += HandleTerminalActivated; status_menu.Add(terminal); // Log viewer ImageMenuItem menu_log_viewer = new ImageMenuItem(Catalog.GetString("Log _Viewer")); menu_log_viewer.Image = new Gtk.Image(Stock.Info, IconSize.Menu); menu_log_viewer.Activated += OnLogViewerMenuActivated; status_menu.Add(menu_log_viewer); // About ImageMenuItem menu_about = new ImageMenuItem(Catalog.GetString("_About")); menu_about.Image = new Gtk.Image(Stock.About, IconSize.Menu); menu_about.Activated += OnAboutMenuActivated; status_menu.Add(menu_about); // Separator status_menu.Add(new SeparatorMenuItem()); // Quit ImageMenuItem menu_quit = new ImageMenuItem(Catalog.GetString("_Quit")); menu_quit.Image = new Gtk.Image(Stock.Quit, IconSize.Menu); menu_quit.Activated += OnQuitMenuActivated; status_menu.Add(menu_quit); status_menu.ShowAll(); if (Environment.OSVersion.Platform == PlatformID.Unix) { Thread signal_thread = new Thread(delegate() { UnixSignal[] signals = new UnixSignal[] { new UnixSignal(Mono.Unix.Native.Signum.SIGINT), new UnixSignal(Mono.Unix.Native.Signum.SIGTERM), }; while (!exit_application && (UnixSignal.WaitAny(signals, 1000) == 1000)) { } Application.Quit(); }); signal_thread.Start(); } Application.Run(); //StopServer (); }
public static void subscribe(LoggerListener listener) { Logger.listeners.Add(listener); }
/// <summary> /// Runs simulation. /// </summary> /// <param name="runListener">Simulation listener.</param> /// <param name="runInSameThread">If <c>true</c>, simulation is run in same thread like caller, /// ie. method blocks until simulation don't finish. If <c>false</c>, simulation is /// run in separate thread and method returns immediately.</param> /// <remarks> /// Simulation is run the way that trigger invokes <see cref="ILinkableComponent.GetValues">ILinkableComponent.GetValues</see> /// method of the model it's connected to /// at the time specified by <see cref="TriggerInvokeTime">TriggerInvokeTime</see> property. /// If you need to use more than one listener you can use <see cref="ProxyListener">ProxyListener</see> /// class or <see cref="ProxyMultiThreadListener">ProxyMultiThreadListener</see> if <c>runInSameThread</c> is <c>false</c>. /// </remarks> public void Run(Logger logger, bool runInSameThread) { LoggerListener loggerListener = new LoggerListener(logger); ProgressListener progressListener = new ProgressListener(); progressListener.ModelProgressChangedHandler += new ModelProgressChangedDelegate(delegate(ILinkableComponent linkableComponent, ITimeStamp simTime) { string guid = cmGuidMapping[linkableComponent]; string progress = CalendarConverter.ModifiedJulian2Gregorian(simTime.ModifiedJulianDay).ToString(); CompositionModelProgressChangedHandler(this, guid, progress); }); ArrayList listeners = new ArrayList(); listeners.Add(loggerListener); listeners.Add(progressListener); ProxyListener proxyListener = new ProxyListener(); proxyListener.Initialize(listeners); startTime = DateTime.Now; if (_running) throw (new Exception("Simulation is already running.")); _running = true; _runListener = proxyListener; try { TimeStamp runToTime = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(_triggerInvokeTime)); // Create informative message if (_runListener != null) { StringBuilder description = new StringBuilder(); description.Append("Starting simulation at "); description.Append(DateTime.Now.ToString()); description.Append(","); description.Append(" composition consists from following models:\n"); foreach (Model model in _models) { description.Append(model.ModelID); description.Append(", "); } // todo: add more info? Event theEvent = new Event(EventType.Informative); theEvent.Description = description.ToString(); _runListener.OnEvent(theEvent); } _runPrepareForComputationStarted = true; // prepare for computation if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Preparing for computation...."; _runListener.OnEvent(theEvent); } // subscribing event listener to all models if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Subscribing proxy event listener...."; _runListener.OnEvent(theEvent); for (int i = 0; i < _runListener.GetAcceptedEventTypeCount(); i++) foreach (Model uimodel in _models) { theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Subscribe() method with EventType." + ((EventType)i).ToString() + " of model " + uimodel.ModelID; _runListener.OnEvent(theEvent); for (int j = 0; j < uimodel.LinkableComponent.GetPublishedEventTypeCount(); j++) if (uimodel.LinkableComponent.GetPublishedEventType(j) == _runListener.GetAcceptedEventType(i)) { uimodel.LinkableComponent.Subscribe(_runListener, _runListener.GetAcceptedEventType(i)); break; } } } if (!runInSameThread) { // creating run thread if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Creating run thread...."; _runListener.OnEvent(theEvent); } _runThread = new Thread(RunThreadFunction) { Priority = ThreadPriority.Normal }; // starting thread... if (_runListener != null) { Event theEvent = new Event(EventType.GlobalProgress); theEvent.Description = "Starting run thread...."; _runListener.OnEvent(theEvent); } _runThread.Start(); } else { // run simulation in same thread (for example when running from console) if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Running simulation in same thread...."; _runListener.OnEvent(theEvent); } RunThreadFunction(); } } catch (System.Exception e) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Exception occured while initiating simulation run: " + e.ToString(); _runListener.OnEvent(theEvent); _runListener.OnEvent(SimulationFailedEvent); // todo: add info about time to this event endTime = DateTime.Now; var ev = new Event { Description = "Elapsed time: " + (endTime - startTime), Type = EventType.GlobalProgress }; _runListener.OnEvent(ev); } } finally { } }