Skip to content

Conversation

@rainyl
Copy link
Owner

@rainyl rainyl commented Aug 28, 2025

fix: #173

Recently, opencv 4.12.0 adds replaceWriteLogMessage abd replaceWriteLogMessageEx in opencv/opencv#27154

Which makes logging to a file possible, now we support user-defined logger

import 'dart:io';
import 'dart:isolate';

import 'package:dartcv4/dartcv.dart' as cv;
import 'package:logging/logging.dart';

void main(List<String> args) async {
  final logFile = File("log.txt");
  final logger = Logger("")
    ..level = Level.ALL
    ..onRecord.listen((record) {
      logFile.writeAsStringSync("${record.message}\n", mode: FileMode.append);
    });

  cv.setLogLevel(cv.LogLevel.DEBUG);
  cv.replaceWriteLogMessage(
    callback: (level, message) {
      logger.warning(message);
    },
  );

  await Isolate.run<void>(
    () {
      for (var i = 0; i < 100; i++) {
        cv.writeLogMessage(cv.LogLevel.ERROR, "$i hello world");
      }
    },
  );

  print("isolate done");

  cv.replaceWriteLogMessage(callback: null);
}

Currently, this is only supported in 2.x branch, will support 1.x in the future if I have some time.

@rainyl rainyl linked an issue Aug 28, 2025 that may be closed by this pull request
@rainyl rainyl merged commit f20902c into 2.x Aug 28, 2025
4 checks passed
@rainyl rainyl deleted the custom-logger branch August 28, 2025 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

logging

2 participants