Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging API #132

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/check-pr-base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Git tree checks

on:
pull_request:
types: [opened, edited, reopened, synchronize]
merge_group:
permissions: read-all

jobs:
check_base_ref:
name: Only release branches may merge into master
runs-on: ubuntu-latest
steps:
- id: not_based_on_master
if: |
github.event_name == 'pull_request' &&
github.event.pull_request.base.ref == 'master' &&
! startsWith(github.event.pull_request.head.ref, 'release/')
run: |
echo 'Only `release/*` branches are allowed to merge into `master`.'
echo 'Maybe your PR should be merging into `staging`?'
exit 1
4 changes: 2 additions & 2 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

## `SpacetimeDB.ClientApi`

To regenerate this namespace, run the `tools/gen-client-api.sh` or the
`tools/gen-client-api.bat` script.
To regenerate this namespace, run the `tools~/gen-client-api.sh` or the
`tools~/gen-client-api.bat` script.
6 changes: 3 additions & 3 deletions src/ClientCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public bool DeleteEntry(byte[] rowBytes)
return true;
}

Logger.LogWarning("Deleting value that we don't have (no cached value available)");
Log.Warn("Deleting value that we don't have (no cached value available)");
return false;
}

Expand All @@ -65,7 +65,7 @@ public void AddTable<T>()

if (!tables.TryAdd(name, new TableCache<T>()))
{
Logger.LogError($"Table with name already exists: {name}");
Log.Error($"Table with name already exists: {name}");
}
}

Expand All @@ -76,7 +76,7 @@ public void AddTable<T>()
return table;
}

Logger.LogError($"We don't know that this table is: {name}");
Log.Error($"We don't know that this table is: {name}");
return null;
}

Expand Down
52 changes: 39 additions & 13 deletions src/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ public enum LogLevel
{
None = 0,
Debug = 1,
Warning = 2,
Error = 4,
Exception = 8,
All = Debug | Warning | Error | Exception
Trace = 2,
Info = 4,
Warning = 8,
Error = 16,
Exception = 32,
All = ~0
}
LogLevel _logLevel;

Expand All @@ -21,35 +23,59 @@ public ConsoleLogger(LogLevel logLevel = LogLevel.All)
_logLevel = logLevel;
}

public void Log(string message)
public void Debug(string message)
{
if (_logLevel.HasFlag(LogLevel.Debug))
{
Console.WriteLine(message);
Console.WriteLine($"[D] {message}");
}
}

public void LogError(string message)
public void Trace(string message)
{
if (_logLevel.HasFlag(LogLevel.Trace))
{
Console.WriteLine($"[T] {message}");
}
}

public void Info(string message)
{
if (_logLevel.HasFlag(LogLevel.Info))
{
Console.WriteLine($"[I] {message}");
}
}

public void Warn(string message)
{
if (_logLevel.HasFlag(LogLevel.Warning))
{
Console.WriteLine($"[W] {message}");
}
}

public void Error(string message)
{
if (_logLevel.HasFlag(LogLevel.Error))
{
Console.WriteLine($"Error: {message}");
Console.WriteLine($"[E] {message}");
}
}

public void LogException(Exception e)
public void Exception(string message)
{
if (_logLevel.HasFlag(LogLevel.Exception))
{
Console.WriteLine($"Exception: {e.Message}");
Console.WriteLine($"[X] {message}");
}
}

public void LogWarning(string message)
public void Exception(Exception exception)
{
if (_logLevel.HasFlag(LogLevel.Warning))
if (_logLevel.HasFlag(LogLevel.Exception))
{
Console.WriteLine($"Warning: {message}");
Console.WriteLine($"[X] {exception}");
}
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/ISpacetimeDBLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ namespace SpacetimeDB
{
public interface ISpacetimeDBLogger
{
void Log(string message);
void LogError(string message);
void LogWarning(string message);
void LogException(Exception e);
void Debug(string message);
void Trace(string message);
void Info(string message);
void Warn(string message);
void Error(string message);
void Exception(string message);
void Exception(Exception e);
}

public static class Logger
public static class Log
{
public static ISpacetimeDBLogger Current =

Expand All @@ -20,9 +23,12 @@ public static class Logger
new ConsoleLogger();
#endif

public static void Log(string message) => Current.Log(message);
public static void LogError(string message) => Current.LogError(message);
public static void LogWarning(string message) => Current.LogWarning(message);
public static void LogException(Exception e) => Current.LogException(e);
public static void Debug(string message) => Current.Debug(message);
public static void Trace(string message) => Current.Trace(message);
public static void Info(string message) => Current.Info(message);
public static void Warn(string message) => Current.Warn(message);
public static void Error(string message) => Current.Error(message);
public static void Exception(string message) => Current.Exception(message);
public static void Exception(Exception exception) => Current.Exception(exception);
}
}
50 changes: 25 additions & 25 deletions src/SpacetimeDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
var table = clientDB.GetTable(tableName);
if (table == null)
{
Logger.LogError($"Unknown table name: {tableName}");
Log.Error($"Unknown table name: {tableName}");
continue;
}

if (update.Deletes.Count != 0)
{
Logger.LogWarning("Non-insert during a subscription update!");
Log.Warn("Non-insert during a subscription update!");
}

var hashSet = GetInsertHashSet(table.ClientTableType, initialSubscription.DatabaseUpdate.Tables.Count);
Expand All @@ -240,7 +240,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
break;

case EncodedValue.Text(var txt):
Logger.LogWarning("JavaScript messages are unsupported.");
Log.Warn("JavaScript messages are unsupported.");
break;
}
}
Expand All @@ -261,7 +261,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
var table = clientDB.GetTable(tableName);
if (table == null)
{
Logger.LogError($"Unknown table name: {tableName}");
Log.Error($"Unknown table name: {tableName}");
continue;
}

Expand All @@ -279,7 +279,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
{
if ((op.insert is not null && oldOp.insert is not null) || (op.delete is not null && oldOp.delete is not null))
{
Logger.LogWarning($"Update with the same primary key was applied multiple times! tableName={tableName}");
Log.Warn($"Update with the same primary key was applied multiple times! tableName={tableName}");
// TODO(jdetter): Is this a correctable error? This would be a major error on the
// SpacetimeDB side.
continue;
Expand Down Expand Up @@ -315,7 +315,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
{
if ((op.insert is not null && oldOp.insert is not null) || (op.delete is not null && oldOp.delete is not null))
{
Logger.LogWarning($"Update with the same primary key was applied multiple times! tableName={tableName}");
Log.Warn($"Update with the same primary key was applied multiple times! tableName={tableName}");
// TODO(jdetter): Is this a correctable error? This would be a major error on the
// SpacetimeDB side.
continue;
Expand Down Expand Up @@ -348,13 +348,13 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
}
catch (Exception e)
{
Logger.LogException(e);
Log.Exception(e);
}
break;
case UpdateStatus.Failed(var failed):
break;
case UpdateStatus.OutOfEnergy(var outOfEnergy):
Logger.LogWarning("Failed to execute reducer: out of energy.");
Log.Warn("Failed to execute reducer: out of energy.");
break;
default:
throw new InvalidOperationException();
Expand All @@ -368,7 +368,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)

if (!waitingOneOffQueries.Remove(messageId, out var resultSource))
{
Logger.LogError($"Response to unknown one-off-query: {messageId}");
Log.Error($"Response to unknown one-off-query: {messageId}");
break;
}

Expand Down Expand Up @@ -442,7 +442,7 @@ public void Connect(string? token, string uri, string addressOrName)
uri = $"ws://{uri}";
}

Logger.Log($"SpacetimeDBClient: Connecting to {uri} {addressOrName}");
Log.Info($"SpacetimeDBClient: Connecting to {uri} {addressOrName}");
Task.Run(async () =>
{
try
Expand All @@ -453,11 +453,11 @@ public void Connect(string? token, string uri, string addressOrName)
{
if (connectionClosed)
{
Logger.Log("Connection closed gracefully.");
Log.Info("Connection closed gracefully.");
return;
}

Logger.LogException(e);
Log.Exception(e);
}
});
}
Expand All @@ -476,7 +476,7 @@ private void OnMessageProcessCompleteUpdate(ReducerEvent? dbEvent, List<DbOp> db
}
catch (Exception e)
{
Logger.LogException(e);
Log.Exception(e);
}
}
}
Expand Down Expand Up @@ -541,7 +541,7 @@ private void OnMessageProcessCompleteUpdate(ReducerEvent? dbEvent, List<DbOp> db
}
catch (Exception e)
{
Logger.LogException(e);
Log.Exception(e);
}
}
}
Expand All @@ -566,7 +566,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
}
catch (Exception e)
{
Logger.LogException(e);
Log.Exception(e);
}
break;
case ServerMessage.TransactionUpdate(var transactionUpdate):
Expand All @@ -581,7 +581,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
var requestId = transactionUpdate.ReducerCall.RequestId;
if (!stats.ReducerRequestTracker.FinishTrackingRequest(requestId))
{
Logger.LogWarning($"Failed to finish tracking reducer request: {requestId}");
Log.Warn($"Failed to finish tracking reducer request: {requestId}");
}
}
OnMessageProcessCompleteUpdate(processed.reducerEvent, dbOps);
Expand All @@ -591,7 +591,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
}
catch (Exception e)
{
Logger.LogException(e);
Log.Exception(e);
}

if (processed.reducerEvent is not { } reducerEvent)
Expand All @@ -607,7 +607,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
}
catch (Exception e)
{
Logger.LogException(e);
Log.Exception(e);
}

if (!reducerFound && transactionUpdate.Status is UpdateStatus.Failed(var failed))
Expand All @@ -618,7 +618,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
}
catch (Exception e)
{
Logger.LogException(e);
Log.Exception(e);
}
}
break;
Expand All @@ -631,7 +631,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
}
catch (Exception e)
{
Logger.LogException(e);
Log.Exception(e);
}
break;
case ServerMessage.OneOffQueryResponse:
Expand All @@ -641,7 +641,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
}
catch (Exception e)
{
Logger.LogException(e);
Log.Exception(e);
}

break;
Expand All @@ -659,7 +659,7 @@ public void InternalCallReducer<T>(T args)
{
if (!webSocket.IsConnected)
{
Logger.LogError("Cannot call reducer, not connected to server!");
Log.Error("Cannot call reducer, not connected to server!");
return;
}

Expand All @@ -677,7 +677,7 @@ public void Subscribe(List<string> queries)
{
if (!webSocket.IsConnected)
{
Logger.LogError("Cannot subscribe, not connected to server!");
Log.Error("Cannot subscribe, not connected to server!");
return;
}

Expand Down Expand Up @@ -714,13 +714,13 @@ public async Task<T[]> OneOffQuery<T>(string query)

if (!stats.OneOffRequestTracker.FinishTrackingRequest(requestId))
{
Logger.LogWarning($"Failed to finish tracking one off request: {requestId}");
Log.Warn($"Failed to finish tracking one off request: {requestId}");
}

T[] LogAndThrow(string error)
{
error = $"While processing one-off-query `{queryString}`, ID {messageId}: {error}";
Logger.LogError(error);
Log.Error(error);
throw new Exception(error);
}

Expand Down
Loading