【问题标题】:Redirecting the assert output from a dll, to a file将断言输出从 dll 重定向到文件
【发布时间】:2023-03-03 00:17:01
【问题描述】:

我有一个由 C# 程序调用的 C DLL。 DLL 中充满了assert() 语句,没有一个足够自信,无法显示在我的屏幕上。我知道这是因为断言输出被写入 STDERR(或者可能是 STDOUT)。

有没有办法以某种方式插入断言宏,以便将输出重定向到文件?

【问题讨论】:

  • +1 添加了 C# 和 PInvoke 而不是 DLL,这样可以吸引更多 .Net 用户来查看问题。
  • 你确定 DLL 是为 DEBUG 编译的(实际上启用了断言)?
  • 是的,绝对启用。

标签: c# c pinvoke assert stderr


【解决方案1】:

也许断言驻留在 dll 中全部通过?

我在 Mono 上进行了以下测试

#include <assert.h>
void foo()
{
    assert(1==2);
}

//~~~~~

[DllImport("mylib.so")]
extern static void foo();

public static void Main (string[] args)
{
  foo();
}

断言失败后程序立即中止。

如果您只想重定向标准错误,Console.SetError(new StreamWriter("err.log"));...;Console.Error.Close() 就足够了。但是如果程序过早中止,Console.Error 将无法正确关闭,流将不会被刷新并且不会记录任何内容。

【讨论】:

  • 问题不在于程序没有中止。就是丢失了中止的原因。不确定您关于使用 Console.SetError 的想法是否适用于 C++ DLL,但这是一个 C DLL
猜你喜欢
  • 1970-01-01
  • 2015-08-31
  • 1970-01-01
  • 2016-08-09
  • 2013-10-11
  • 2023-03-19
  • 2014-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多