简体   繁体   English

是否有任何不终止/退出程序的 err() 替代方法?

[英]Are there any alternatives to err() that do not terminate/exit program?

Is there an alternative to the err() family of functions found in <err.h> that displays the program name, a semicolon, a space, and the error message... without exiting the program? <err.h> 中的 err() 系列函数是否有替代方法,它显示程序名称、分号、空格和错误消息......而不退出程序?

Pseudocode伪代码

I want我想

print "<program name>: <error message>"

and not并不是

print "<program name>: <error message>" exit program return <status>

EDIT: I cannot use argv[0] (as the program name) to write out the message myself, as I am writing a library编辑:我不能使用 argv[0] (作为程序名称)自己写出消息,因为我正在编写一个库

See man error .man error

error is the same as err but will return if status == 0 , exit otherwise. errorerr相同,但如果status == 0将返回,否则退出。

CONFORMING TO符合

  • These functions and variables are GNU extensions, and should not be used in programs intended to be portable.这些函数和变量是 GNU 扩展,不应在旨在可移植的程序中使用。

program_invocation_name works just as well for getting the name of the program, as Erdal Küçük mentioned.正如 Erdal Küçük 所提到的, program_invocation_name也可以用于获取程序的名称。 From this, we can print the full error message, program name included.从中,我们可以打印完整的错误消息,包括程序名称。

The variable is part of glibc and can be retrieved using:该变量是 glibc 的一部分,可以使用以下方法检索:

extern char *program_invocation_name;

Because it is not a part of the C standard library, programs using this variable should not be expected to be portable.因为它不是 C 标准库的一部分,所以不应期望使用此变量的程序具有可移植性。

The warn / warnx functions from err.h will do the same thing as err , but will return. err.h中的warn / warnx函数将执行与err相同的操作,但会返回。

For example:例如:

warnx("message here");
puts("I'm still running!");

Output: Output:

a.out: message here
I'm still running!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM