Delphi中如何通过跨平台手段确保Writeln输出至Output的安全性?
- 内容介绍
- 文章标签
- 相关推荐
本文共计833个文字,预计阅读时间需要4分钟。
对于支持OSX和Android的最新Delphi版本,是否有独立于平台的方法来检测Writeln到Output是否可以安全使用?文档中提到大多数进程没有标准输出文件,并且wri...。
答案:是的,Delphi提供了独立于平台的方法来检测是否可以安全使用Writeln到Output。
详细说明:在Delphi中,可以使用`Output`对象来输出信息到标准输出。然而,并非所有进程都配置了标准输出文件。为了检测是否可以安全使用`Writeln`到`Output`,可以使用以下步骤:
1. 尝试将信息输出到`Output`。
2.捕获任何可能发生的异常。
以下是一个简单的示例代码:
delphi
try Writeln(Output, 'This is a test message');except on E: EOutputNotAssigned do begin // 标准输出未分配,不能安全使用Writeln Writeln('Error: ', E.Message); end;end;如果捕获到`EOutputNotAssigned`异常,说明标准输出未分配,此时不能安全使用`Writeln`。否则,可以安全地使用`Writeln`输出信息。
文档中提到的Most processes do not have a standard output file, and wri...表明,在大多数情况下,标准输出文件并未分配,因此需要检测后再进行输出。
对于具有OSX和 Android支持的较新的Delphi版本,是否有一种独立于平台的方法来检测 Writeln到 Output是否可以安全使用?输出文档包含一个说明
Most processes do not have a standard output file, and writing to
Output raises an error. Delphi programs have a standard output file if
they are linked as console applications.
我的主要目标是为日志记录提供独立于平台的回退,但要避免在没有控制台(stdout)时可能出现的操作系统错误.
例如:如此检查IsConsole就足够了:
procedure Log(const Msg: string); begin if LoggingFrameworkAvailable then begin // use the logging framework to output the log message end if System.IsConsole then begin // fallback to stdout logging WriteLn(Msg); end; end;
所以问题可以改写:“如果IsConsole是真的,Delphi应用程序是否可以安全地使用Output?”.
因为它是一个后备日志方法,如果日志消息是“不可见的”(重定向到/ dev / null),只要代码保证跨平台运行而没有错误,那对我来说就没问题了.
如果是,此代码是否也可以安全地使用Free Pascal? (见Can a Windows GUI program written in Lazarus create a console and write to it at runtime?)
不是最终答案,而是写入{$IFDEF}平台相关调用平台独立POSIX C API functionint fileno(FILE * stream)
..This function returns the file descriptor associated with the stream stream. If an error is detected (for example, if the stream is not valid) or if stream does not do I/O to a file, fileno returns -1
…
There are also symbolic constants defined in unistd.h for the file descriptors belonging to the standard streams stdin, stdout, and stderr…
STDOUT_FILENO .. This macro has value 1, which is the file descriptor for standard output.
STDERR_FILENO .. This macro has value 2, which is the file descriptor for standard error output.
因此,如果对应于Console输出的流的fileno的平台独立请求返回2或1,那么您不会被重定向,如果它返回-1,那么您的输出没有结束
对于Delphi和Free Pascal以及Virtual Pascal和GNU Pascal,确切的代码可能会有所不同.
去看看您感兴趣的目标平台的运行时库,例如:
> svn.freepascal.org/svn/fpc/trunk/rtl/win32/system.pp
> svn.freepascal.org/svn/fpc/trunk/rtl/linux/system.pp
本文共计833个文字,预计阅读时间需要4分钟。
对于支持OSX和Android的最新Delphi版本,是否有独立于平台的方法来检测Writeln到Output是否可以安全使用?文档中提到大多数进程没有标准输出文件,并且wri...。
答案:是的,Delphi提供了独立于平台的方法来检测是否可以安全使用Writeln到Output。
详细说明:在Delphi中,可以使用`Output`对象来输出信息到标准输出。然而,并非所有进程都配置了标准输出文件。为了检测是否可以安全使用`Writeln`到`Output`,可以使用以下步骤:
1. 尝试将信息输出到`Output`。
2.捕获任何可能发生的异常。
以下是一个简单的示例代码:
delphi
try Writeln(Output, 'This is a test message');except on E: EOutputNotAssigned do begin // 标准输出未分配,不能安全使用Writeln Writeln('Error: ', E.Message); end;end;如果捕获到`EOutputNotAssigned`异常,说明标准输出未分配,此时不能安全使用`Writeln`。否则,可以安全地使用`Writeln`输出信息。
文档中提到的Most processes do not have a standard output file, and wri...表明,在大多数情况下,标准输出文件并未分配,因此需要检测后再进行输出。
对于具有OSX和 Android支持的较新的Delphi版本,是否有一种独立于平台的方法来检测 Writeln到 Output是否可以安全使用?输出文档包含一个说明
Most processes do not have a standard output file, and writing to
Output raises an error. Delphi programs have a standard output file if
they are linked as console applications.
我的主要目标是为日志记录提供独立于平台的回退,但要避免在没有控制台(stdout)时可能出现的操作系统错误.
例如:如此检查IsConsole就足够了:
procedure Log(const Msg: string); begin if LoggingFrameworkAvailable then begin // use the logging framework to output the log message end if System.IsConsole then begin // fallback to stdout logging WriteLn(Msg); end; end;
所以问题可以改写:“如果IsConsole是真的,Delphi应用程序是否可以安全地使用Output?”.
因为它是一个后备日志方法,如果日志消息是“不可见的”(重定向到/ dev / null),只要代码保证跨平台运行而没有错误,那对我来说就没问题了.
如果是,此代码是否也可以安全地使用Free Pascal? (见Can a Windows GUI program written in Lazarus create a console and write to it at runtime?)
不是最终答案,而是写入{$IFDEF}平台相关调用平台独立POSIX C API functionint fileno(FILE * stream)
..This function returns the file descriptor associated with the stream stream. If an error is detected (for example, if the stream is not valid) or if stream does not do I/O to a file, fileno returns -1
…
There are also symbolic constants defined in unistd.h for the file descriptors belonging to the standard streams stdin, stdout, and stderr…
STDOUT_FILENO .. This macro has value 1, which is the file descriptor for standard output.
STDERR_FILENO .. This macro has value 2, which is the file descriptor for standard error output.
因此,如果对应于Console输出的流的fileno的平台独立请求返回2或1,那么您不会被重定向,如果它返回-1,那么您的输出没有结束
对于Delphi和Free Pascal以及Virtual Pascal和GNU Pascal,确切的代码可能会有所不同.
去看看您感兴趣的目标平台的运行时库,例如:
> svn.freepascal.org/svn/fpc/trunk/rtl/win32/system.pp
> svn.freepascal.org/svn/fpc/trunk/rtl/linux/system.pp

