veilidchat/packages/veilid_support/lib/src/veilid_log.dart

87 lines
2.3 KiB
Dart
Raw Normal View History

2023-01-08 22:27:33 -05:00
import 'package:loggy/loggy.dart';
2024-03-01 16:25:56 -05:00
import 'package:meta/meta.dart';
2023-07-26 14:20:29 -04:00
import 'package:veilid/veilid.dart';
2023-09-26 18:46:02 -04:00
// Loggy tools
const LogLevel traceLevel = LogLevel('Trace', 1);
extension TraceLoggy on Loggy {
void trace(dynamic message, [Object? error, StackTrace? stackTrace]) =>
log(traceLevel, message, error, stackTrace);
}
2023-01-08 22:27:33 -05:00
VeilidConfigLogLevel convertToVeilidConfigLogLevel(LogLevel? level) {
if (level == null) {
return VeilidConfigLogLevel.off;
}
switch (level) {
case LogLevel.error:
return VeilidConfigLogLevel.error;
case LogLevel.warning:
return VeilidConfigLogLevel.warn;
case LogLevel.info:
return VeilidConfigLogLevel.info;
case LogLevel.debug:
return VeilidConfigLogLevel.debug;
case traceLevel:
return VeilidConfigLogLevel.trace;
}
return VeilidConfigLogLevel.off;
}
void setVeilidLogLevel(LogLevel? level) {
2023-07-26 14:20:29 -04:00
Veilid.instance.changeLogLevel('all', convertToVeilidConfigLogLevel(level));
2023-01-08 22:27:33 -05:00
}
2024-03-24 12:13:27 -04:00
void changeVeilidLogIgnore(String change) {
Veilid.instance.changeLogIgnore('all', change.split(','));
}
2023-01-08 22:27:33 -05:00
class VeilidLoggy implements LoggyType {
@override
Loggy<VeilidLoggy> get loggy => Loggy<VeilidLoggy>('Veilid');
}
2024-03-01 16:25:56 -05:00
@internal
Loggy get veilidLoggy => Loggy<VeilidLoggy>('Veilid');
2023-01-08 22:27:33 -05:00
2024-01-04 22:29:43 -05:00
void processLog(VeilidLog log) {
2023-01-08 22:27:33 -05:00
StackTrace? stackTrace;
Object? error;
final backtrace = log.backtrace;
if (backtrace != null) {
2023-07-26 17:42:11 -04:00
stackTrace = StackTrace.fromString('$backtrace\n${StackTrace.current}');
2023-01-08 22:27:33 -05:00
error = 'embedded stack trace for ${log.logLevel} ${log.message}';
}
switch (log.logLevel) {
case VeilidLogLevel.error:
2024-03-01 16:25:56 -05:00
veilidLoggy.error(log.message, error, stackTrace);
2023-01-08 22:27:33 -05:00
break;
case VeilidLogLevel.warn:
2024-03-01 16:25:56 -05:00
veilidLoggy.warning(log.message, error, stackTrace);
2023-01-08 22:27:33 -05:00
break;
case VeilidLogLevel.info:
2024-03-01 16:25:56 -05:00
veilidLoggy.info(log.message, error, stackTrace);
2023-01-08 22:27:33 -05:00
break;
case VeilidLogLevel.debug:
2024-03-01 16:25:56 -05:00
veilidLoggy.debug(log.message, error, stackTrace);
2023-01-08 22:27:33 -05:00
break;
case VeilidLogLevel.trace:
2024-03-01 16:25:56 -05:00
veilidLoggy.trace(log.message, error, stackTrace);
2023-01-08 22:27:33 -05:00
break;
}
}
2023-12-27 22:56:24 -05:00
void initVeilidLog(bool debugMode) {
2023-07-26 17:42:11 -04:00
// ignore: do_not_use_environment
const isTrace = String.fromEnvironment('LOG_TRACE') != '';
2023-01-08 22:27:33 -05:00
LogLevel logLevel;
if (isTrace) {
logLevel = traceLevel;
} else {
2023-12-27 22:56:24 -05:00
logLevel = debugMode ? LogLevel.debug : LogLevel.info;
2023-01-08 22:27:33 -05:00
}
setVeilidLogLevel(logLevel);
}