Skip to content

Commit

Permalink
[.NET] Use separate function in CLib for external logging
Browse files Browse the repository at this point in the history
Restores original definition of ct_setLogWriter by introducing ct_setLogCallback.
  • Loading branch information
burkenyo authored and speth committed Aug 17, 2022
1 parent 5cc7680 commit 09017bb
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions include/cantera/base/ExternalLogger.h
Expand Up @@ -13,7 +13,7 @@ namespace Cantera {
class ExternalLogger : public Logger
{
public:
explicit ExternalLogger(Writer writer) {
explicit ExternalLogger(LogCallback writer) {
if (writer == nullptr) {
throw CanteraError("ExternalLogger::ExternalLogger",
"Argument “writer” must not be null!");
Expand Down Expand Up @@ -55,7 +55,7 @@ class ExternalLogger : public Logger
private:
std::string m_writeBuffer;

Writer m_writer = nullptr;
LogCallback m_writer = nullptr;
};

}
Expand Down
4 changes: 3 additions & 1 deletion include/cantera/clib/clib_defs.h
Expand Up @@ -38,7 +38,9 @@

// Used by external logger
enum LogLevel { INFO, WARN , ERROR };

//! Represents a callback that is invoked to produce log output.
typedef void
(*Writer)(enum LogLevel logLevel, const char* category, const char* message);
(*LogCallback)(enum LogLevel logLevel, const char* category, const char* message);

#endif
3 changes: 2 additions & 1 deletion include/cantera/clib/ct.h
Expand Up @@ -155,7 +155,8 @@ extern "C" {
const double* state2, double delta, double* fluxes);

CANTERA_CAPI int ct_getCanteraError(int buflen, char* buf);
CANTERA_CAPI int ct_setLogWriter(Writer writer);
CANTERA_CAPI int ct_setLogWriter(void* logger);
CANTERA_CAPI int ct_setLogCallback(LogCallback writer);
CANTERA_CAPI int ct_addCanteraDirectory(size_t buflen, const char* buf);
CANTERA_CAPI int ct_getDataDirectories(int buflen, char* buf, const char* sep);
CANTERA_CAPI int ct_getCanteraVersion(int buflen, char* buf);
Expand Down
2 changes: 1 addition & 1 deletion interfaces/dotnet/Cantera.Tests/src/ApplicationTest.cs
Expand Up @@ -155,7 +155,7 @@ static void ProduceMockLogOutput()

Assert.NotNull(eventField);

var del = (LibCantera.Writer) eventField!.GetValue(null)!;
var del = (LibCantera.LogCallback) eventField!.GetValue(null)!;

Assert.NotNull(del);

Expand Down
4 changes: 2 additions & 2 deletions interfaces/dotnet/Cantera/src/Application.cs
Expand Up @@ -50,7 +50,7 @@ static Application()
};

InteropUtil.CheckReturn(
LibCantera.ct_setLogWriter(s_invokeMessageLoggedDelegate));
LibCantera.ct_setLogCallback(s_invokeMessageLoggedDelegate));
}

/// <summary>
Expand All @@ -64,7 +64,7 @@ static Application()
/// storing it as
/// a class member, we ensure it is not collected until the class is.
/// </remarks>
static readonly LibCantera.Writer? s_invokeMessageLoggedDelegate;
static readonly LibCantera.LogCallback s_invokeMessageLoggedDelegate;

unsafe static readonly Lazy<string> s_version =
new(() => InteropUtil.GetString(10, LibCantera.ct_getCanteraVersion));
Expand Down
7 changes: 1 addition & 6 deletions interfaces/dotnet/Cantera/src/Interop/LibCantera.cs
@@ -1,8 +1,6 @@
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

using System.Runtime.InteropServices;

namespace Cantera.Interop;

static partial class LibCantera
Expand All @@ -15,8 +13,5 @@ static partial class LibCantera
const string LibFile = "cantera.3.0.0.so";
#endif

public delegate void Writer(LogLevel logLevel, string category, string message);

[DllImport(LibFile)]
public static extern int ct_setLogWriter(Writer writer);
public delegate void LogCallback(LogLevel logLevel, string category, string message);
}
13 changes: 12 additions & 1 deletion src/clib/ct.cpp
Expand Up @@ -1470,7 +1470,18 @@ extern "C" {
}
}

int ct_setLogWriter(Writer writer)
int ct_setLogWriter(void* logger)
{
try {
Logger* logwriter = (Logger*)logger;
setLogger(logwriter);
return 0;
} catch (...) {
return handleAllExceptions(-1, ERR);
}
}

int ct_setLogCallback(LogCallback writer)
{
static unique_ptr<Logger> logwriter;
try {
Expand Down

0 comments on commit 09017bb

Please sign in to comment.