Skip to content

Commit

Permalink
Logging API (#1714)
Browse files Browse the repository at this point in the history
Co-authored-by: Steve Boytsun <steve@clockwokrlabs.io>
  • Loading branch information
SteveBoytsun and Steve Boytsun authored Sep 23, 2024
1 parent 9f62399 commit 2b09c8d
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 72 deletions.
4 changes: 2 additions & 2 deletions crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public static partial class Reducers
public static void InsertData(PublicTable data)
{
data.Insert();
Runtime.Log("New list");
Log.Info("New list");
foreach (var item in PublicTable.Iter())
{
Runtime.Log($"Item: {item.StringField}");
Log.Info($"Item: {item.StringField}");
}
}

Expand Down
18 changes: 11 additions & 7 deletions crates/bindings-csharp/Runtime/Internal/FFI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ public readonly struct IndexType
private readonly byte index_type;
}

[StructLayout(LayoutKind.Sequential)]
public readonly struct LogLevel(byte log_level)
{
private readonly byte log_level = log_level;
}

[StructLayout(LayoutKind.Sequential)]
public readonly record struct RowIter(uint Handle)
{
Expand Down Expand Up @@ -198,9 +192,19 @@ public static partial void _volatile_nonatomic_schedule_immediate(
uint args_len
);

public enum LogLevel : byte
{
Error = 0,
Warn = 1,
Info = 2,
Debug = 3,
Trace = 4,
Panic = 5,
}

[LibraryImport(StdbNamespace)]
public static partial void _console_log(
byte level,
LogLevel level,
[In] byte[] target,
uint target_len,
[In] byte[] filename,
Expand Down
2 changes: 1 addition & 1 deletion crates/bindings-csharp/Runtime/Internal/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static void __describe_module__(BytesSink description)
}
catch (Exception e)
{
Runtime.Log($"Error while describing the module: {e}", Runtime.LogLevel.Error);
Log.Error($"Error while describing the module: {e}");
}
}

Expand Down
179 changes: 179 additions & 0 deletions crates/bindings-csharp/Runtime/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
namespace SpacetimeDB;

using System.Runtime.CompilerServices;
using System.Text;
using SpacetimeDB.Internal;

public static class Log
{
/// <summary>
/// Write an error message to module log
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="RESERVED_target"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_filename"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_lineNumber"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
public static void Debug(
string message,
[CallerMemberName] string RESERVED_target = "",
[CallerFilePath] string RESERVED_filename = "",
[CallerLineNumber] uint RESERVED_lineNumber = 0
) =>
LogInternal(
message,
FFI.LogLevel.Debug,
RESERVED_target,
RESERVED_filename,
RESERVED_lineNumber
);

/// <summary>
/// Write a trace message to module log
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="RESERVED_target"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_filename"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_lineNumber"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
public static void Trace(
string message,
[CallerMemberName] string RESERVED_target = "",
[CallerFilePath] string RESERVED_filename = "",
[CallerLineNumber] uint RESERVED_lineNumber = 0
) =>
LogInternal(
message,
FFI.LogLevel.Trace,
RESERVED_target,
RESERVED_filename,
RESERVED_lineNumber
);

/// <summary>
/// Write an info message to module log
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="RESERVED_target"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_filename"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_lineNumber"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
public static void Info(
string message,
[CallerMemberName] string RESERVED_target = "",
[CallerFilePath] string RESERVED_filename = "",
[CallerLineNumber] uint RESERVED_lineNumber = 0
) =>
LogInternal(
message,
FFI.LogLevel.Info,
RESERVED_target,
RESERVED_filename,
RESERVED_lineNumber
);

/// <summary>
/// Write a warning message to module log
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="RESERVED_target"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_filename"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_lineNumber"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
public static void Warn(
string message,
[CallerMemberName] string RESERVED_target = "",
[CallerFilePath] string RESERVED_filename = "",
[CallerLineNumber] uint RESERVED_lineNumber = 0
) =>
LogInternal(
message,
FFI.LogLevel.Warn,
RESERVED_target,
RESERVED_filename,
RESERVED_lineNumber
);

/// <summary>
/// Write an error message to module log
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="RESERVED_target"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_filename"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_lineNumber"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
public static void Error(
string message,
[CallerMemberName] string RESERVED_target = "",
[CallerFilePath] string RESERVED_filename = "",
[CallerLineNumber] uint RESERVED_lineNumber = 0
) =>
LogInternal(
message,
FFI.LogLevel.Error,
RESERVED_target,
RESERVED_filename,
RESERVED_lineNumber
);

/// <summary>
/// Write an exception message to module log
/// </summary>
/// <param name="message">Message to log</param>
/// <param name="RESERVED_target"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_filename"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_lineNumber"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
public static void Exception(
string message,
[CallerMemberName] string RESERVED_target = "",
[CallerFilePath] string RESERVED_filename = "",
[CallerLineNumber] uint RESERVED_lineNumber = 0
) =>
LogInternal(
message,
FFI.LogLevel.Error,
RESERVED_target,
RESERVED_filename,
RESERVED_lineNumber
);

/// <summary>
/// Write an exception message and stacktrace to module log
/// </summary>
/// <param name="exception">Exception to log</param>
/// <param name="RESERVED_target"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_filename"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
/// <param name="RESERVED_lineNumber"><b>!!! DO NOT USE !!!</b> Value for this parameter will be automatically generated at compile time. Providing this parameter could lead to undefined behavior</param>
public static void Exception(
Exception exception,
[CallerMemberName] string RESERVED_target = "",
[CallerFilePath] string RESERVED_filename = "",
[CallerLineNumber] uint RESERVED_lineNumber = 0
) =>
LogInternal(
exception.ToString(),
FFI.LogLevel.Error,
RESERVED_target,
RESERVED_filename,
RESERVED_lineNumber
);

private static void LogInternal(
string text,
FFI.LogLevel level,
string target,
string filename,
uint lineNumber
)
{
var target_bytes = Encoding.UTF8.GetBytes(target);
var filename_bytes = Encoding.UTF8.GetBytes(filename);
var text_bytes = Encoding.UTF8.GetBytes(text);

FFI._console_log(
level,
target_bytes,
(uint)target_bytes.Length,
filename_bytes,
(uint)filename_bytes.Length,
lineNumber,
text_bytes,
(uint)text_bytes.Length
);
}
}
38 changes: 0 additions & 38 deletions crates/bindings-csharp/Runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace SpacetimeDB;
using System.Runtime.CompilerServices;
using SpacetimeDB.BSATN;
using SpacetimeDB.Internal;
using static System.Text.Encoding;

public class ReducerContext
{
Expand Down Expand Up @@ -82,40 +81,3 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
);
}
}

public static class Runtime
{
public enum LogLevel : byte
{
Error,
Warn,
Info,
Debug,
Trace,
Panic,
}

public static void Log(
string text,
LogLevel level = LogLevel.Info,
[CallerMemberName] string target = "",
[CallerFilePath] string filename = "",
[CallerLineNumber] uint lineNumber = 0
)
{
var target_bytes = UTF8.GetBytes(target);
var filename_bytes = UTF8.GetBytes(filename);
var text_bytes = UTF8.GetBytes(text);

FFI._console_log(
(byte)level,
target_bytes,
(uint)target_bytes.Length,
filename_bytes,
(uint)filename_bytes.Length,
lineNumber,
text_bytes,
(uint)text_bytes.Length
);
}
}
6 changes: 3 additions & 3 deletions crates/cli/src/subcommands/project/csharp/Lib._cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ static partial class Module
{
var person = new Person { Name = name, Age = age };
person.Insert();
Runtime.Log($"Inserted {person.Name} under #{person.Id}");
Log.Info($"Inserted {person.Name} under #{person.Id}");
}

[SpacetimeDB.Reducer]
public static void SayHello()
{
foreach (var person in Person.Iter())
{
Runtime.Log($"Hello, {person.Name}!");
Log.Info($"Hello, {person.Name}!");
}
Runtime.Log("Hello, World!");
Log.Info("Hello, World!");
}
}
10 changes: 5 additions & 5 deletions modules/benchmarks-cs/circles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void insert_bulk_entity(uint count)
{
new Entity(0, id, id + 5, id * 5).Insert();
}
Runtime.Log($"INSERT ENTITY: {count}");
Log.Info($"INSERT ENTITY: {count}");
}

[SpacetimeDB.Reducer]
Expand All @@ -75,7 +75,7 @@ public static void insert_bulk_circle(uint count)
{
new Circle(id, id, id, id + 5, id * 5).Insert();
}
Runtime.Log($"INSERT CIRCLE: {count}");
Log.Info($"INSERT CIRCLE: {count}");
}

[SpacetimeDB.Reducer]
Expand All @@ -85,7 +85,7 @@ public static void insert_bulk_food(uint count)
{
new Food(id).Insert();
}
Runtime.Log($"INSERT FOOD: {count}");
Log.Info($"INSERT FOOD: {count}");
}

[SpacetimeDB.Reducer]
Expand All @@ -103,7 +103,7 @@ public static void cross_join_all(uint expected)
}
}

Runtime.Log($"CROSS JOIN ALL: {expected}, processed: {count}");
Log.Info($"CROSS JOIN ALL: {expected}, processed: {count}");
}

[SpacetimeDB.Reducer]
Expand All @@ -127,7 +127,7 @@ public static void cross_join_circle_food(uint expected)
}
}

Runtime.Log($"CROSS JOIN CIRCLE FOOD: {expected}, processed: {count}");
Log.Info($"CROSS JOIN CIRCLE FOOD: {expected}, processed: {count}");
}

[SpacetimeDB.Reducer]
Expand Down
Loading

2 comments on commit 2b09c8d

@github-actions
Copy link

@github-actions github-actions bot commented on 2b09c8d Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

@github-actions
Copy link

@github-actions github-actions bot commented on 2b09c8d Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

Please sign in to comment.