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

Serialize LogLevel using constants #244

Merged
merged 5 commits into from Nov 29, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions android/src/main/java/io/ably/flutter/plugin/AblyMessageCodec.java
@@ -1,7 +1,5 @@
package io.ably.flutter.plugin;

import android.util.Log;

import androidx.annotation.Nullable;

import com.google.firebase.messaging.RemoteMessage;
Expand Down Expand Up @@ -47,6 +45,7 @@
import io.ably.lib.types.Param;
import io.ably.lib.types.PresenceMessage;
import io.ably.lib.util.Crypto;
import io.ably.lib.util.Log;
import io.flutter.plugin.common.StandardMessageCodec;

public class AblyMessageCodec extends StandardMessageCodec {
Expand Down Expand Up @@ -302,7 +301,7 @@ private PlatformClientOptions decodeClientOptions(Map<String, Object> jsonMap) {

// ClientOptions
readValueFromJson(jsonMap, PlatformConstants.TxClientOptions.clientId, v -> o.clientId = (String) v);
readValueFromJson(jsonMap, PlatformConstants.TxClientOptions.logLevel, v -> o.logLevel = (Integer) v);
readValueFromJson(jsonMap, PlatformConstants.TxClientOptions.logLevel, v -> o.logLevel = decodeLogLevel((String) v));
readValueFromJson(jsonMap, PlatformConstants.TxClientOptions.tls, v -> o.tls = (Boolean) v);
readValueFromJson(jsonMap, PlatformConstants.TxClientOptions.restHost, v -> o.restHost = (String) v);
readValueFromJson(jsonMap, PlatformConstants.TxClientOptions.realtimeHost, v -> o.realtimeHost = (String) v);
Expand Down Expand Up @@ -334,6 +333,24 @@ private PlatformClientOptions decodeClientOptions(Map<String, Object> jsonMap) {
return new PlatformClientOptions(o, jsonMap.containsKey(PlatformConstants.TxClientOptions.hasAuthCallback) ? ((boolean) jsonMap.get(PlatformConstants.TxClientOptions.hasAuthCallback)) : false);
}

private int decodeLogLevel(String logLevelString) {
if (logLevelString == null) return Log.WARN;
switch (logLevelString) {
case PlatformConstants.TxLogLevelEnum.none:
return Log.NONE;
case PlatformConstants.TxLogLevelEnum.verbose:
return Log.VERBOSE;
case PlatformConstants.TxLogLevelEnum.debug:
return Log.DEBUG;
case PlatformConstants.TxLogLevelEnum.info:
return Log.INFO;
case PlatformConstants.TxLogLevelEnum.error:
return Log.ERROR;
default:
return Log.WARN;
}
}

private TokenDetails decodeTokenDetails(Map<String, Object> jsonMap) {
if (jsonMap == null) return null;
final TokenDetails o = new TokenDetails();
Expand Down
Expand Up @@ -32,9 +32,10 @@ static final public class CodecTypes {
public static final byte unNotificationSettings = (byte) 148;
public static final byte remoteMessage = (byte) 149;
public static final byte errorInfo = (byte) 150;
public static final byte connectionStateChange = (byte) 151;
public static final byte channelStateChange = (byte) 152;
public static final byte cipherParams = (byte) 153;
public static final byte logLevel = (byte) 151;
public static final byte connectionStateChange = (byte) 152;
public static final byte channelStateChange = (byte) 153;
public static final byte cipherParams = (byte) 154;
}

static final public class PlatformMethod {
Expand Down Expand Up @@ -252,6 +253,15 @@ static final public class TxFormFactorEnum {
public static final String other = "other";
}

static final public class TxLogLevelEnum {
public static final String none = "none";
public static final String verbose = "verbose";
public static final String debug = "debug";
public static final String info = "info";
public static final String warn = "warn";
public static final String error = "error";
}

static final public class TxDevicePlatformEnum {
public static final String ios = "ios";
public static final String android = "android";
Expand Down
5 changes: 5 additions & 0 deletions bin/codegen_context.dart
Expand Up @@ -30,6 +30,7 @@ Iterable<Map<String, dynamic>> get _types sync* {
'remoteMessage',

'errorInfo',
'logLevel',

// Events
'connectionStateChange',
Expand Down Expand Up @@ -326,6 +327,10 @@ const List<Map<String, dynamic>> _objects = [
'other'
]
},
{
'name': 'LogLevelEnum',
'properties': ['none', 'verbose', 'debug', 'info', 'warn', 'error'],
},
{
'name': 'DevicePlatformEnum',
'properties': <String>['ios', 'android', 'browser']
Expand Down
16 changes: 7 additions & 9 deletions ios/Classes/codec/AblyFlutterReader.m
Expand Up @@ -5,15 +5,13 @@
#import "AblyPlatformConstants.h"
#import <ably_flutter/ably_flutter-Swift.h>

static ARTLogLevel _logLevel(NSNumber *const number) {
switch (number.unsignedIntegerValue) {
case 99: return ARTLogLevelNone;
case 2: return ARTLogLevelVerbose;
case 3: return ARTLogLevelDebug;
case 4: return ARTLogLevelInfo;
case 5: return ARTLogLevelWarn;
case 6: return ARTLogLevelError;
}
static ARTLogLevel _logLevel(NSString *const logLevelString) {
QuintinWillison marked this conversation as resolved.
Show resolved Hide resolved
if ([logLevelString isEqualToString:TxLogLevelEnum_none]) return ARTLogLevelNone;
if ([logLevelString isEqualToString:TxLogLevelEnum_verbose]) return ARTLogLevelVerbose;
if ([logLevelString isEqualToString:TxLogLevelEnum_debug]) return ARTLogLevelDebug;
if ([logLevelString isEqualToString:TxLogLevelEnum_info]) return ARTLogLevelInfo;
if ([logLevelString isEqualToString:TxLogLevelEnum_warn]) return ARTLogLevelWarn;
if ([logLevelString isEqualToString:TxLogLevelEnum_error]) return ARTLogLevelError;
return ARTLogLevelWarn;
}

Expand Down
15 changes: 12 additions & 3 deletions ios/Classes/codec/AblyPlatformConstants.h
Expand Up @@ -29,9 +29,10 @@ typedef NS_ENUM(UInt8, _Value) {
unNotificationSettingsCodecType = 148,
remoteMessageCodecType = 149,
errorInfoCodecType = 150,
connectionStateChangeCodecType = 151,
channelStateChangeCodecType = 152,
cipherParamsCodecType = 153,
logLevelCodecType = 151,
connectionStateChangeCodecType = 152,
channelStateChangeCodecType = 153,
cipherParamsCodecType = 154,
};


Expand Down Expand Up @@ -233,6 +234,14 @@ extern NSString *const TxFormFactorEnum_car;
extern NSString *const TxFormFactorEnum_embedded;
extern NSString *const TxFormFactorEnum_other;

// key constants for LogLevelEnum
extern NSString *const TxLogLevelEnum_none;
extern NSString *const TxLogLevelEnum_verbose;
extern NSString *const TxLogLevelEnum_debug;
extern NSString *const TxLogLevelEnum_info;
extern NSString *const TxLogLevelEnum_warn;
extern NSString *const TxLogLevelEnum_error;

// key constants for DevicePlatformEnum
extern NSString *const TxDevicePlatformEnum_ios;
extern NSString *const TxDevicePlatformEnum_android;
Expand Down
8 changes: 8 additions & 0 deletions ios/Classes/codec/AblyPlatformConstants.m
Expand Up @@ -204,6 +204,14 @@
NSString *const TxFormFactorEnum_embedded = @"embedded";
NSString *const TxFormFactorEnum_other = @"other";

// key constants for LogLevelEnum
NSString *const TxLogLevelEnum_none = @"none";
NSString *const TxLogLevelEnum_verbose = @"verbose";
NSString *const TxLogLevelEnum_debug = @"debug";
NSString *const TxLogLevelEnum_info = @"info";
NSString *const TxLogLevelEnum_warn = @"warn";
NSString *const TxLogLevelEnum_error = @"error";

// key constants for DevicePlatformEnum
NSString *const TxDevicePlatformEnum_ios = @"ios";
NSString *const TxDevicePlatformEnum_android = @"android";
Expand Down
2 changes: 1 addition & 1 deletion lib/src/authentication/src/client_options.dart
Expand Up @@ -28,7 +28,7 @@ class ClientOptions extends AuthOptions {
///
/// Use constants from [LogLevel] to pass arguments
/// https://docs.ably.com/client-lib-development-guide/features/#TO3b
int logLevel = LogLevel.info;
LogLevel logLevel = LogLevel.info;

/// for development environments only
///
Expand Down
16 changes: 13 additions & 3 deletions lib/src/generated/platform_constants.dart
Expand Up @@ -29,9 +29,10 @@ class CodecTypes {
static const int unNotificationSettings = 148;
static const int remoteMessage = 149;
static const int errorInfo = 150;
static const int connectionStateChange = 151;
static const int channelStateChange = 152;
static const int cipherParams = 153;
static const int logLevel = 151;
static const int connectionStateChange = 152;
static const int channelStateChange = 153;
static const int cipherParams = 154;
}

class PlatformMethod {
Expand Down Expand Up @@ -256,6 +257,15 @@ class TxFormFactorEnum {
static const String other = 'other';
}

class TxLogLevelEnum {
QuintinWillison marked this conversation as resolved.
Show resolved Hide resolved
static const String none = 'none';
static const String verbose = 'verbose';
static const String debug = 'debug';
static const String info = 'info';
static const String warn = 'warn';
static const String error = 'error';
}

class TxDevicePlatformEnum {
static const String ios = 'ios';
static const String android = 'android';
Expand Down
23 changes: 1 addition & 22 deletions lib/src/logging/src/log_level.dart
Expand Up @@ -5,25 +5,4 @@ import 'package:ably_flutter/ably_flutter.dart';
/// Can be used for [ClientOptions.logLevel]
///
/// https://docs.ably.com/client-lib-development-guide/features/#TO3b
///
/// TODO(tiholic) convert [LogLevel] to enum and update encoder to pass
/// right numeric values to platform methods
class LogLevel {
/// No logging
static const int none = 99;

/// Verbose logs
static const int verbose = 2;

/// debug logs
static const int debug = 3;

/// info logs
static const int info = 4;

/// warning logs
static const int warn = 5;

/// error logs
static const int error = 6;
}
enum LogLevel { none, verbose, debug, info, warn, error }
43 changes: 41 additions & 2 deletions lib/src/platform/src/codec.dart
Expand Up @@ -235,7 +235,8 @@ class Codec extends StandardMessageCodec {

// ClientOptions
_writeToJson(jsonMap, TxClientOptions.clientId, v.clientId);
_writeToJson(jsonMap, TxClientOptions.logLevel, v.logLevel);
_writeToJson(
jsonMap, TxClientOptions.logLevel, _encodeLogLevel(v.logLevel));
//TODO handle logHandler
_writeToJson(jsonMap, TxClientOptions.tls, v.tls);
_writeToJson(jsonMap, TxClientOptions.restHost, v.restHost);
Expand Down Expand Up @@ -573,7 +574,7 @@ class Codec extends StandardMessageCodec {
jsonMap,
TxClientOptions.clientId,
)
..logLevel = jsonMap[TxClientOptions.logLevel] as int
..logLevel = _decodeLogLevel(jsonMap[TxClientOptions.logLevel] as String?)
//TODO handle logHandler
..tls = jsonMap[TxClientOptions.tls] as bool
..restHost = _readFromJson<String>(
Expand Down Expand Up @@ -1112,4 +1113,42 @@ class Codec extends StandardMessageCodec {
}
return PaginatedResult(items, hasNext: hasNext);
}

String? _encodeLogLevel(final LogLevel? level) {
if (level == null) return null;
switch (level) {
case LogLevel.none:
return TxLogLevelEnum.none;
case LogLevel.verbose:
return TxLogLevelEnum.verbose;
case LogLevel.debug:
return TxLogLevelEnum.debug;
case LogLevel.info:
return TxLogLevelEnum.info;
case LogLevel.warn:
return TxLogLevelEnum.warn;
case LogLevel.error:
return TxLogLevelEnum.error;
}
}

LogLevel _decodeLogLevel(String? logLevelString) {
switch (logLevelString) {
case TxLogLevelEnum.none:
return LogLevel.none;
case TxLogLevelEnum.verbose:
return LogLevel.verbose;
case TxLogLevelEnum.debug:
return LogLevel.debug;
case TxLogLevelEnum.info:
return LogLevel.info;
case TxLogLevelEnum.warn:
return LogLevel.warn;
case TxLogLevelEnum.error:
return LogLevel.error;
}
throw AblyException(
'Error decoding LogLevel from platform string: $logLevelString',
);
}
}