Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
committed Jan 15, 2025
1 parent 4d34796 commit bdde057
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
27 changes: 27 additions & 0 deletions dev/log.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:general_lib/log/log.dart';
import 'package:io_universe/io_universe.dart';

void main(List<String> args) async {
final GeneralLibraryLog generalLibraryLog = GeneralLibraryLog(
logOptions: GeneralLibraryLogOptions(
textTitle: "General Lib",
textContext: "dev log",
),
);

{
for (var i = 0; i < 2; i++) {
await generalLibraryLog.printToTerminal(
logMessage: GeneralLibraryLogMessage(
value: "index: ${i + 1}",
isForcePrint: false,
stackTrace: StackTrace.current,
isFullDetail: false,
logMessageType: GeneralLibraryLogMessageType.info,
logOptions: null,
),
);
}
}
exit(0);
}
76 changes: 73 additions & 3 deletions lib/log/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class GeneralLibraryLogMessage {
/// GeneralLibraryLog
final bool isFullDetail;

/// GeneralLibraryLog
final GeneralLibraryLogOptions? logOptions;

/// GeneralLibraryLog
const GeneralLibraryLogMessage({
/// GeneralLibraryLog
Expand All @@ -53,6 +56,9 @@ class GeneralLibraryLogMessage {

/// GeneralLibraryLog
required this.logMessageType,

/// GeneralLibraryLog
required this.logOptions,
});

/// GeneralLib
Expand All @@ -62,25 +68,81 @@ class GeneralLibraryLogMessage {
StackTrace? stackTrace,
bool? isFullDetail,
GeneralLibraryLogMessageType? logMessageType,
GeneralLibraryLogOptions? logOptions,
}) {
return GeneralLibraryLogMessage(
value: value ?? this.value,
isForcePrint: isForcePrint ?? this.isForcePrint,
stackTrace: stackTrace ?? this.stackTrace,
isFullDetail: isFullDetail ?? this.isFullDetail,
logMessageType: logMessageType ?? this.logMessageType,
logOptions: logOptions ?? this.logOptions,
);
}
}

/// GeneralLibraryLog
class GeneralLibraryLogOptions {
/// GeneralLibraryLog
final String textTitle;

/// GeneralLibraryLog
final String textContext;

/// GeneralLibraryLog
const GeneralLibraryLogOptions({
required this.textTitle,
required this.textContext,
});

/// GeneralLibraryLog
GeneralLibraryLogOptions patchWith({
GeneralLibraryLogOptions? logOptions,
}) {
if (logOptions != null) {
return copyWith(
textTitle: valueStringSetIfEmptReturnNull(value: logOptions.textTitle),
textContext: valueStringSetIfEmptReturnNull(value: logOptions.textContext),
);
}
return this;
}

/// GeneralLibraryLog
String? valueStringSetIfEmptReturnNull({
required String? value,
}) {
final String vl = (value ?? "").trim();
if (vl.isEmpty) {
return null;
}
return vl;
}

/// GeneralLibraryLog
GeneralLibraryLogOptions copyWith({
final String? textTitle,
final String? textContext,
}) {
return GeneralLibraryLogOptions(
textTitle: textTitle ?? this.textTitle,
textContext: textContext ?? this.textContext,
);
}
}

/// GeneralLibraryLog
class GeneralLibraryLog {
/// GeneralLibraryLog
const GeneralLibraryLog();
final GeneralLibraryLogOptions logOptions;

/// GeneralLibraryLog
const GeneralLibraryLog({
required this.logOptions,
});

/// GeneralLibraryLog
static final bool _isCanPrintToTerminal =
Dart.executable_type == ExecutableType.cli;
static final bool _isCanPrintToTerminal = Dart.executable_type == ExecutableType.cli;

/// GeneralLibraryLog
FutureOr<void> printToTerminal({
Expand All @@ -94,6 +156,14 @@ class GeneralLibraryLog {
} else {
print(Trace.from(logMessage.stackTrace).terse.toString());
}
final GeneralLibraryLogOptions logOptions = this.logOptions.patchWith(
logOptions: logMessage.logOptions,
);
print("""
• 🆔 Title: ${logOptions.textTitle}
• 📍 Context: ${logOptions.textContext}
• 📄 Message:
""");
if (logMessage.value is Map || logMessage.value is List) {
(logMessage.value ?? {}).printPretty();
} else {
Expand Down

0 comments on commit bdde057

Please sign in to comment.