Skip to content

Commit

Permalink
tracing slim console i/o
Browse files Browse the repository at this point in the history
  • Loading branch information
mikestockdale committed Dec 12, 2016
1 parent 1957a1b commit 6596080
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 12 deletions.
Binary file added binary/release.2.5.1.net.40.zip
Binary file not shown.
15 changes: 15 additions & 0 deletions source/Samples/SlimSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,20 @@ public string FirstHash(Dictionary<string, string> hash) {
public Dictionary<string, string> SomeDictionary() {
return new Dictionary<string, string>{{"name", "bob"}, {"address", "here"}};
}

public string StringOf(int count) {
//var thread = new Thread(WriteStuff);
//thread.Start();
var result = new string('*', count);
Console.WriteLine(result);
return result;
}

static void WriteStuff() {
for (var i = 0; i < 1000; i++) {
Thread.Sleep(1);
Console.WriteLine("hey");
}
}
}
}
33 changes: 33 additions & 0 deletions source/fitSharp/Machine/Engine/Trace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright © 2016 Syterra Software Inc. All rights reserved.
// The use and distribution terms for this software are covered by the Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file license.txt at the root of this distribution. By using this software in any fashion, you are agreeing
// to be bound by the terms of this license. You must not remove this notice, or any other, from this software.

using System;
using System.IO;
using fitSharp.Machine.Model;

namespace fitSharp.Machine.Engine {
public class Trace: Copyable {

public Copyable Copy() {
return new Trace {writer = writer};
}

public void File(string fileName) {
writer = new StreamWriter(fileName);
}

public void Stop() {
writer.Close();
writer = StreamWriter.Null;
}

public void Write(string label, string message) {
writer.WriteLine("{0:yyyy-MM-dd HH:mm:ss.fffff},{1},{2}", DateTime.Now, label, message);
writer.Flush();
}

StreamWriter writer = StreamWriter.Null;
}
}
18 changes: 14 additions & 4 deletions source/fitSharp/Slim/Service/ConsoleSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
using System.IO;
using System.Text;
using fitSharp.IO;
using fitSharp.Machine.Engine;

namespace fitSharp.Slim.Service {
public class ConsoleSession: Session {
public ConsoleSession() {

public ConsoleSession(Memory memory) {
this.memory = memory;
saveOut = Console.Out;
saveError = Console.Error;
CaptureConsole();
Expand Down Expand Up @@ -43,23 +46,29 @@ void WriteCaptured() {
}

void WriteEncoded(string prefix, StringWriter content) {
var encodedContent = prefix + content.ToString().Replace(Environment.NewLine, Environment.NewLine + prefix);
var contentString = content.ToString();
if (string.IsNullOrEmpty(contentString)) return;
var encodedContent = prefix + contentString.Replace(Environment.NewLine, Environment.NewLine + prefix);
if (encodedContent.EndsWith(prefix)) {
encodedContent = encodedContent.Substring(0, encodedContent.Length - prefix.Length);
}
memory.GetItem<Trace>().Write("ConsoleErr", encodedContent);
saveError.Write(encodedContent);
}

void WriteToConsole(string message) {
memory.GetItem<Trace>().Write("ConsoleOut", message);
saveOut.Write(message);
}

static string ReadFromConsole(int length) {
string ReadFromConsole(int length) {
var result = new StringBuilder(length);
for (var i = 0; i < length; i++) {
result.Append((char) Console.Read());
}
return result.ToString();
var message = result.ToString();
memory.GetItem<Trace>().Write("ConsoleIn", message);
return message;
}

void CaptureConsole() {
Expand All @@ -69,6 +78,7 @@ void CaptureConsole() {
Console.SetError(captureError);
}

readonly Memory memory;
readonly TextWriter saveOut;
readonly TextWriter saveError;
StringWriter captureOut;
Expand Down
5 changes: 3 additions & 2 deletions source/fitSharp/Slim/Service/Messenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
using System.Net;
using System.Net.Sockets;
using fitSharp.IO;
using fitSharp.Machine.Engine;
using fitSharp.Machine.Model;

namespace fitSharp.Slim.Service {
public class Messenger {
public bool IsEnd { get; private set; }

public static Messenger Make(int port) {
public static Messenger Make(int port, Memory memory) {
if (port == 1) {
return new Messenger(new ConsoleSession());
return new Messenger(new ConsoleSession(memory));
}
var listener = new TcpListener(IPAddress.Any, port);
listener.Start();
Expand Down
8 changes: 4 additions & 4 deletions source/fitSharp/Slim/Service/Runner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2011 Syterra Software Inc. All rights reserved.
// Copyright © 2016 Syterra Software Inc. All rights reserved.
// The use and distribution terms for this software are covered by the Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file license.txt at the root of this distribution. By using this software in any fashion, you are agreeing
// to be bound by the terms of this license. You must not remove this notice, or any other, from this software.
Expand All @@ -16,13 +16,13 @@ public class Runner: Runnable {

public int Run(IList<string> commandLineArguments, Memory memory, ProgressReporter reporter) {
service = new Service(memory);
ParseCommandLine(commandLineArguments);
ParseCommandLine(commandLineArguments, memory);
new Interpreter(messenger, assemblyPaths, service).ProcessInstructions();
return 0;
}

private void ParseCommandLine(IList<string> commandLineArguments) {
messenger = Messenger.Make(int.Parse(commandLineArguments[commandLineArguments.Count - 1]));
private void ParseCommandLine(IList<string> commandLineArguments, Memory memory) {
messenger = Messenger.Make(int.Parse(commandLineArguments[commandLineArguments.Count - 1]), memory);
if (commandLineArguments.Count > 1) {
assemblyPaths = commandLineArguments[0];
}
Expand Down
15 changes: 14 additions & 1 deletion source/fitSharp/Slim/Service/Service.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2011 Syterra Software Inc. All rights reserved.
// Copyright © 2016 Syterra Software Inc. All rights reserved.
// The use and distribution terms for this software are covered by the Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file license.txt at the root of this distribution. By using this software in any fashion, you are agreeing
// to be bound by the terms of this license. You must not remove this notice, or any other, from this software.
Expand All @@ -24,6 +24,7 @@ public Service(Memory memory) : base(memory) {
Memory.GetItem<Symbols>();

PushLibraryInstance(new TypedValue(new Actors(this)));
PushLibraryInstance(new TypedValue(new SlimFunctions(this)));
}

public void PushLibraryInstance(TypedValue instance) {
Expand Down Expand Up @@ -64,5 +65,17 @@ public void PopFixture() {
processor.Get<SavedInstances>().Save(actorInstanceName, actors.Pop());
}
}

class SlimFunctions {
public SlimFunctions(SlimProcessor processor) {
this.processor = processor;
}

public void _Configure(string feature, string item, string value) {
processor.Memory.GetItem<Trace>().File(value);
}

readonly SlimProcessor processor;
}
}
}
1 change: 1 addition & 0 deletions source/fitSharp/fitSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="Machine\Engine\MemberQuery.cs" />
<Compile Include="Machine\Engine\MemberSpecification.cs" />
<Compile Include="Machine\Engine\RuntimeMemberFactory.cs" />
<Compile Include="Machine\Engine\Trace.cs" />
<Compile Include="Machine\Exception\IgnoredException.cs" />
<Compile Include="Fit\Fixtures\Define.cs" />
<Compile Include="Fit\Operators\CheckDefault.cs" />
Expand Down
3 changes: 2 additions & 1 deletion source/fitSharpTest/NUnit/Slim/ConsoleSessionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.IO;
using fitSharp.Machine.Engine;
using fitSharp.Slim.Service;
using NUnit.Framework;

Expand All @@ -21,7 +22,7 @@ public void SetUp() {
Console.SetOut(testOut);
testError = new StringWriter();
Console.SetError(testError);
session = new ConsoleSession();
session = new ConsoleSession(new TypeDictionary());
}

[TearDown]
Expand Down

0 comments on commit 6596080

Please sign in to comment.