Skip to content

Commit 3214026

Browse files
committed
[mixin_logger] ffi implement setLoggerFileLeading
1 parent dff8405 commit 3214026

File tree

8 files changed

+34
-3
lines changed

8 files changed

+34
-3
lines changed

packages/mixin_logger/ffigen.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ description: |
77
output: 'lib/src/mixin_logger_bindings_generated.dart'
88
headers:
99
entry-points:
10-
- 'native/build/mixin_logger.h'
10+
- 'macos/Header/mixin_logger.h'
1111
include-directives:
12-
- 'native/build/mixin_logger.h'
12+
- 'macos/Header/mixin_logger.h'
1313
preamble: |
1414
// ignore_for_file: always_specify_types
1515
// ignore_for_file: camel_case_types

packages/mixin_logger/lib/src/mixin_logger_bindings_generated.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ class MixinLoggerBindings {
6161
'MixinLoggerWriteLog');
6262
late final _MixinLoggerWriteLog = _MixinLoggerWriteLogPtr.asFunction<
6363
void Function(ffi.Pointer<ffi.Char>)>();
64+
65+
void MixinLoggerSetFileLeading(
66+
ffi.Pointer<ffi.Char> str,
67+
) {
68+
return _MixinLoggerSetFileLeading(
69+
str,
70+
);
71+
}
72+
73+
late final _MixinLoggerSetFileLeadingPtr =
74+
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Char>)>>(
75+
'MixinLoggerSetFileLeading');
76+
late final _MixinLoggerSetFileLeading = _MixinLoggerSetFileLeadingPtr
77+
.asFunction<void Function(ffi.Pointer<ffi.Char>)>();
6478
}
6579

6680
class _GoString_ extends ffi.Struct {

packages/mixin_logger/lib/src/write_log_to_file_ffi.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ Future<void> initLogger(
4848
}
4949

5050
void setLoggerFileLeading(String? fileLeading) {
51-
// TODO: implement setLoggerFileLeading
51+
final fileLeadingPtr = (fileLeading ?? "").toNativeUtf8();
52+
_bindings.MixinLoggerSetFileLeading(fileLeadingPtr.cast());
53+
malloc.free(fileLeadingPtr);
5254
}

packages/mixin_logger/lib/src/write_log_to_file_io.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Future<void> initLogger(
3232
}
3333

3434
void setLoggerFileLeading(String? fileLeading) {
35+
if (Platform.isMacOS) {
36+
ffi.setLoggerFileLeading(fileLeading);
37+
return;
38+
}
3539
assert(LogFileManager.instance != null, 'Logger is not initialized');
3640
LogFileManager.instance?.setLoggerFileLeading(fileLeading);
3741
}

packages/mixin_logger/macos/Header/mixin_logger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ extern "C" {
7676

7777
extern void MixinLoggerInit(char* dir, GoInt64 maxFileSize, GoInt maxFileCount, char* fileLeading);
7878
extern void MixinLoggerWriteLog(char* str);
79+
extern void MixinLoggerSetFileLeading(char* str);
7980

8081
#ifdef __cplusplus
8182
}
1.04 KB
Binary file not shown.

packages/mixin_logger/native/mixin_logger.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ func MixinLoggerWriteLog(str *C.char) {
226226
_mixinLoggerContext._WriteLogToContext(C.GoString(str))
227227
}
228228

229+
//export MixinLoggerSetFileLeading
230+
func MixinLoggerSetFileLeading(str *C.char) {
231+
if _mixinLoggerContext == nil {
232+
fmt.Println("mixin_logger is not initialized")
233+
return
234+
}
235+
_mixinLoggerContext.fileLeading = C.GoString(str)
236+
}
237+
229238
func main() {
230239

231240
}

packages/mixin_logger/native/mixin_logger_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestLogger(t *testing.T) {
1919
}
2020
for i := 0; i < 1000; i++ {
2121
context._WriteLogToContext(fmt.Sprintf("test %d", i))
22+
context.fileLeading = fmt.Sprintf("test_leading_%d", i)
2223
}
2324
}
2425

0 commit comments

Comments
 (0)