/// <summary> /// The function will write the provided 'output' to the /// specified console stream using the provided 'foregroundColor' and /// 'backgroundColor'. /// After printing the original console colors get restored. /// <para> /// The default colors are: /// </para> /// <para> /// The output stream to use. Default is the /// standard output. /// </para> /// <para> /// The backgroundColor default is ConsoleColor.Black. /// </para> /// <para> /// The foregroundColor default is ConsoleColor.White. /// </para> /// </summary> /// <param name="output"></param> /// <param name="consoleOutputStream">The output stream to use. Default is the ConsoleOutputStream.STANDARD.</param> /// <param name="foregroundColor"></param> /// <param name="backgroundColor"></param> public static void PrintToConsole(string output, ConsoleOutputStream consoleOutputStream = ConsoleOutputStream.STANDARD, ConsoleColor foregroundColor = ConsoleColor.White, ConsoleColor backgroundColor = ConsoleColor.Black) { ConsoleColor savedBackgroundColor; ConsoleColor savedForegroundColor; if (!String.IsNullOrWhiteSpace(output)) { savedBackgroundColor = Console.BackgroundColor; savedForegroundColor = Console.ForegroundColor; Console.ForegroundColor = foregroundColor; Console.BackgroundColor = backgroundColor; if(consoleOutputStream == ConsoleOutputStream.STANDARD) { Console.Write(output); } else { Console.Error.Write(output); Console.Error.Flush(); } Console.BackgroundColor = savedBackgroundColor; Console.ForegroundColor = savedForegroundColor; } }
private void DisposeOldConsoleMessages() { // clean up if (ConsoleOutputStream.Count > 1100) { for (int i = 1000; i < ConsoleOutputStream.Count; i++) { ConsoleOutputStream.RemoveAt(i); } } }
private void FilterConsoleOutput(int?value) { if (value == null) { ConsoleOutputStream.ToList().ForEach(x => x.Visibility = Visibility.Visible); } else { var en = (TextTypeEnum)value; foreach (OutputText item in ConsoleOutputStream.ToList()) { SetOutputTextVisibility(en, item); } } }
public void DisposeCore() { { ConsoleOutputStream consoleOutputStream = new ConsoleOutputStream(); Core core = new Core( new CoreDescription(), consoleOutputStream ); Scene scene = core.CreateScene(); Actor actorA, actorB; { ActorDescription actorDesc = new ActorDescription(); actorDesc.Shapes.Add( new BoxShapeDescription( 5, 6, 7 ) ); actorA = scene.CreateActor( actorDesc ); } { ActorDescription actorDesc = new ActorDescription(); actorDesc.Shapes.Add( new BoxShapeDescription( 2, 5, 7 ) ); actorB = scene.CreateActor( actorDesc ); } core.Dispose(); } { ConsoleOutputStream consoleOutputStream = new ConsoleOutputStream(); Core core = new Core( new CoreDescription(), consoleOutputStream ); Scene scene = core.CreateScene(); Actor actorA, actorB; { ActorDescription actorDesc = new ActorDescription(); actorDesc.Shapes.Add( new BoxShapeDescription( 5, 6, 7 ) ); actorA = scene.CreateActor( actorDesc ); } core.Dispose(); } }
public void DisposeCore() { { ConsoleOutputStream consoleOutputStream = new ConsoleOutputStream(); Core core = new Core(new CoreDescription(), consoleOutputStream); Scene scene = core.CreateScene(); Actor actorA, actorB; { ActorDescription actorDesc = new ActorDescription(); actorDesc.Shapes.Add(new BoxShapeDescription(5, 6, 7)); actorA = scene.CreateActor(actorDesc); } { ActorDescription actorDesc = new ActorDescription(); actorDesc.Shapes.Add(new BoxShapeDescription(2, 5, 7)); actorB = scene.CreateActor(actorDesc); } core.Dispose(); } { ConsoleOutputStream consoleOutputStream = new ConsoleOutputStream(); Core core = new Core(new CoreDescription(), consoleOutputStream); Scene scene = core.CreateScene(); Actor actorA, actorB; { ActorDescription actorDesc = new ActorDescription(); actorDesc.Shapes.Add(new BoxShapeDescription(5, 6, 7)); actorA = scene.CreateActor(actorDesc); } core.Dispose(); } }
private void PublishMessageToConsole(string rowData) { DisposeOldConsoleMessages(); ConsoleOutputStream.Insert(0, CreateOutputText(rowData)); if (IsCountingNotVisitedOutputLinesOn) { if (_messageCounter > _maxMessages) { CountOfNotVisitedOutputLines = $"New (>{_maxMessages})"; } else { _messageCounter++; CountOfNotVisitedOutputLines = $"New ({_messageCounter})"; } } }
static void Main(string[] args) { //Havent used any library for Dependecy Injection. Wanted to keep it simple and small IReader reader = new CSVFileReader(); IOutputStream output = new ConsoleOutputStream(); ICalculator calculator = new QuoteCalculator(); CalculateRateApp calc = new CalculateRateApp(reader, output, calculator, 36); try { calc.Run(args); } catch (Exception exception) { Console.WriteLine(exception.Message); } }
public void Process(ConsoleOutputStream defaultStream, List <Tuple <LogLevel, ConsoleOutputStream> > mappings, IEnumerable <LocalLogMessage> messages) { // create a new pipeline stage var formatter = new TestFormatter(); var stage = ProcessingPipelineStage.Create <ConsoleWriterPipelineStage>("Console", null); stage.DefaultStream = defaultStream; stage.Formatter = formatter; // replace i/o streams to avoid mixing up test output with regular console output var stdoutStream = Stream.Synchronized(new MemoryStream()); // the stage writes to the stream asynchronously, var stderrStream = Stream.Synchronized(new MemoryStream()); // so synchronize access to the stream var stdoutWriter = new StreamWriter(stdoutStream); var stderrWriter = new StreamWriter(stderrStream); stage.OutputStream = stdoutWriter; stage.ErrorStream = stderrWriter; // configure the stage // build a dictionary that contains the mappings from log level to the corresponding console stream var levelToStreamMap = new Dictionary <LogLevel, ConsoleOutputStream>(); foreach (var mapping in mappings) { levelToStreamMap[mapping.Item1] = mapping.Item2; stage.MapLogLevelToStream(mapping.Item1, mapping.Item2); } // initialize the pipeline stage stage.Initialize(); // process the message and determine the expected output in stdout/stderr var expectedStdout = new StringBuilder(); var expectedStderr = new StringBuilder(); foreach (var message in messages) { stage.ProcessMessage(message); if (!levelToStreamMap.TryGetValue(message.LogLevel, out var stream)) { stream = defaultStream; } // add formatted message to the expected output // (the stage automatically adds a newline after each message) string formattedOutput = formatter.Format(message); if (stream == ConsoleOutputStream.Stdout) { expectedStdout.AppendLine(formattedOutput); } else { expectedStderr.AppendLine(formattedOutput); } } // give the messages some time (500ms) to travel through the pipeline for (int i = 0; i < 10; i++) { Thread.Sleep(50); if (stdoutStream.Length >= expectedStdout.Length && stderrStream.Length >= expectedStderr.Length) { break; } } // the streams should contain the output now stdoutStream.Position = 0; stderrStream.Position = 0; byte[] stdoutData = new byte[stdoutStream.Length]; byte[] stderrData = new byte[stderrStream.Length]; int stdoutBytesReadCount = stdoutStream.Read(stdoutData, 0, stdoutData.Length); int stderrBytesReadCount = stderrStream.Read(stderrData, 0, stderrData.Length); Assert.Equal(stdoutData.Length, stdoutBytesReadCount); Assert.Equal(stderrData.Length, stderrBytesReadCount); var stdoutReader = new StreamReader(new MemoryStream(stdoutData)); var stderrReader = new StreamReader(new MemoryStream(stderrData)); string stdoutOutput = stdoutReader.ReadToEnd(); string stderrOutput = stderrReader.ReadToEnd(); Assert.Equal(expectedStdout.ToString(), stdoutOutput); Assert.Equal(expectedStderr.ToString(), stderrOutput); // shut the pipeline stage down stage.Shutdown(); Assert.False(stage.IsInitialized); }