Skip to content

Commit

Permalink
added logging feature (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fw2568 authored Nov 30, 2018
1 parent 68a6c9a commit dfa110b
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/Contiva.SAP.NWRfc.Abstractions/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace Contiva.SAP.NWRfc
{
public interface ILogger
{
void LogException(Exception exception, string message);
void LogException(Exception exception);
void LogTrace(string message, object data);
void LogError(string message, object data);

void LogDebug(string message, object data);

void LogTrace(string message);
void LogDebug(string message);
void LogError(string message);


}
}
1 change: 1 addition & 0 deletions src/Contiva.SAP.NWRfc.Abstractions/IRfcRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ public interface IRfcRuntime
Either<RfcErrorInfo, Unit> SetTimeString(IDataContainerHandle containerHandle, string name, string value);
Either<RfcErrorInfo, string> GetTimeString(IDataContainerHandle containerHandle, string name);

Option<ILogger> Logger { get; }
}
}
5 changes: 2 additions & 3 deletions src/Contiva.SAP.NWRfc.Core/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public Connection(
_stateAgent = Agent.Start<IConnectionHandle, AgentMessage, Either<RfcErrorInfo, object>>(
connectionHandle, (handle, msg) =>
{
Console.WriteLine($"Agent: {System.Threading.Thread.CurrentThread.ManagedThreadId}");

if (handle == null)
return (null,
new RfcErrorInfo(RfcRc.RFC_INVALID_HANDLE, RfcErrorGroup.EXTERNAL_RUNTIME_FAILURE,
Expand Down Expand Up @@ -64,7 +62,7 @@ public Connection(
}
catch (Exception ex)
{
Console.WriteLine(ex);
rfcRuntime.Logger.IfSome(l => l.LogException(ex));
}

throw new InvalidOperationException();
Expand All @@ -76,6 +74,7 @@ public static Task<Either<RfcErrorInfo,IConnection>> Create(IDictionary<string,
return runtime.OpenConnection(connectionParams).Map(handle => (IConnection) new Connection(handle, runtime)).AsTask();
}


public Task<Either<RfcErrorInfo, Unit>> CommitAndWait()
{
return CreateFunction("BAPI_TRANSACTION_COMMIT")
Expand Down
Binary file modified src/Contiva.SAP.NWRfc.Native.clr/AssemblyInfo.cpp
Binary file not shown.
14 changes: 14 additions & 0 deletions src/Contiva.SAP.NWRfc.Native.clr/HandleWrappers.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#pragma once
#include <sstream> //for std::stringstream
#include <string> //for std::string


using namespace System;

Expand Down Expand Up @@ -48,6 +51,17 @@ namespace Contiva {

protected:
virtual void DestroyHandle(T handle) abstract;

public:
virtual String^ ToString() override
{
const void * address = static_cast<const void*>(Handle);
std::stringstream ss;
ss << address;
std::string name = ss.str();

return gcnew String(name.c_str());
}
};

template<typename T>
Expand Down
70 changes: 65 additions & 5 deletions src/Contiva.SAP.NwRfc.Runtime/RfcRuntime.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/SAPSystemTests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static async Task Main(string[] args)

};

var runtime = new RfcRuntime();
var runtime = new RfcRuntime(new SimpleConsoleLogger());

Task<Either<RfcErrorInfo, IConnection>> ConnFunc() => Connection.Create(settings, runtime);

Expand Down
97 changes: 97 additions & 0 deletions test/SAPSystemTests/SimpleConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Text;
using Contiva.SAP.NWRfc;
using Newtonsoft.Json;

namespace SAPSystemTests
{
public class SimpleConsoleLogger : ILogger
{
public void LogException(Exception exception, string message)
{
Console.WriteLine($"{message} - Exception: {exception}");
}

public void LogException(Exception exception)
{
LogException(exception, "");
}

public void LogTrace(string message, object data)
{
Console.WriteLine($"TRACE\t{message}{ObjectToString(data)}");
}

public void LogError(string message, object data)
{
Console.WriteLine($"ERROR\t{message}{ObjectToString(data)}");
}

public void LogDebug(string message, object data)
{
Console.WriteLine($"DEBUG\t{message}{ObjectToString(data)}");
}

public void LogTrace(string message)
{
LogTrace(message, null);
}

public void LogDebug(string message)
{
LogDebug(message, null);
}

public void LogError(string message)
{
LogError(message, null);
}

public static string ObjectToString(object valueObject)
{
var dataString = new StringBuilder();
if (valueObject == null)
return "";

dataString.Append(", Data: ");

dataString.Append(JsonConvert.SerializeObject(valueObject, new JsonSerializerSettings
{
Converters = new List<JsonConverter>(new[]{new HandleToStringJsonConverter() })
}));

return dataString.ToString();

}

class HandleToStringJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
if(objectType.BaseType != null
&& objectType.BaseType.FullName != null
&& (objectType.BaseType.FullName.StartsWith("Contiva.SAP.NWRfc.Native.HandleBase")
|| objectType.BaseType.FullName.StartsWith("Contiva.SAP.NWRfc.Native.DataContainerBase")))
return true;
return false;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var typeName = value.GetType().Name;
writer.WriteValue($"{typeName}<{value}>");
}

public override bool CanRead
{
get { return false; }
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}
}

0 comments on commit dfa110b

Please sign in to comment.