mirror of
https://github.com/systemd/systemd.git
synced 2024-11-27 04:03:36 +08:00
man: recommend %m over strerror()
The need to set errno is very very ugly, but at least it is thread-safe and works correctly. Using strerror() is likely to be wrong, so let's not recommend that. People who do a lot of logging would provide use some wrapper that sets errno like we do, so nudge people towards %m. I tested that all the separate .c files compile cleanly.
This commit is contained in:
parent
29c45dc434
commit
b4096cecff
@ -1,7 +1,7 @@
|
||||
/* SPDX-License-Identifier: CC0-1.0 */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <systemd/sd-journal.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@ -11,7 +11,8 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to open journal: %m\n");
|
||||
return 1;
|
||||
}
|
||||
SD_JOURNAL_FOREACH_FIELD(j, field)
|
||||
|
@ -1,7 +1,7 @@
|
||||
/* SPDX-License-Identifier: CC0-1.0 */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <systemd/sd-journal.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@ -9,7 +9,8 @@ int main(int argc, char *argv[]) {
|
||||
sd_journal *j;
|
||||
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to open journal: %m\n");
|
||||
return 1;
|
||||
}
|
||||
SD_JOURNAL_FOREACH(j) {
|
||||
@ -18,7 +19,8 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l);
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to read message field: %m\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
/* SPDX-License-Identifier: CC0-1.0 */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <systemd/sd-journal.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@ -12,12 +12,14 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to open journal: %m\n");
|
||||
return 1;
|
||||
}
|
||||
r = sd_journal_query_unique(j, "_SYSTEMD_UNIT");
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Failed to query journal: %s\n", strerror(-r));
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to query journal: %m\n");
|
||||
return 1;
|
||||
}
|
||||
SD_JOURNAL_FOREACH_UNIQUE(j, d, l)
|
||||
|
@ -1,7 +1,7 @@
|
||||
/* SPDX-License-Identifier: CC0-1.0 */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <systemd/sd-journal.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@ -9,7 +9,8 @@ int main(int argc, char *argv[]) {
|
||||
sd_journal *j;
|
||||
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to open journal: %m\n");
|
||||
return 1;
|
||||
}
|
||||
for (;;) {
|
||||
@ -17,21 +18,24 @@ int main(int argc, char *argv[]) {
|
||||
size_t l;
|
||||
r = sd_journal_next(j);
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Failed to iterate to next entry: %s\n", strerror(-r));
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to iterate to next entry: %m\n");
|
||||
break;
|
||||
}
|
||||
if (r == 0) {
|
||||
/* Reached the end, let's wait for changes, and try again */
|
||||
r = sd_journal_wait(j, (uint64_t) -1);
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Failed to wait for changes: %s\n", strerror(-r));
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to wait for changes: %m\n");
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
r = sd_journal_get_data(j, "MESSAGE", &d, &l);
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to read message field: %m\n");
|
||||
continue;
|
||||
}
|
||||
printf("%.*s\n", (int) l, (const char*) d);
|
||||
|
@ -1,8 +1,8 @@
|
||||
/* SPDX-License-Identifier: CC0-1.0 */
|
||||
|
||||
#include <errno.h>
|
||||
#include <syslog.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <systemd/sd-journal.h>
|
||||
#include <systemd/sd-daemon.h>
|
||||
@ -12,7 +12,8 @@ int main(int argc, char *argv[]) {
|
||||
FILE *log;
|
||||
fd = sd_journal_stream_fd("test", LOG_INFO, 1);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "Failed to create stream fd: %s\n", strerror(-fd));
|
||||
errno = -fd;
|
||||
fprintf(stderr, "Failed to create stream fd: %m\n");
|
||||
return 1;
|
||||
}
|
||||
log = fdopen(fd, "w");
|
||||
|
@ -1,7 +1,7 @@
|
||||
/* SPDX-License-Identifier: CC0-1.0 */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
@ -21,7 +21,8 @@
|
||||
#define MEMBER "GetUnitByPID"
|
||||
|
||||
static int log_error(int error, const char *message) {
|
||||
fprintf(stderr, "%s: %s\n", message, strerror(-error));
|
||||
errno = -error;
|
||||
fprintf(stderr, "%s: %m\n", message);
|
||||
return error;
|
||||
}
|
||||
|
||||
|
@ -119,8 +119,10 @@
|
||||
int r;
|
||||
…
|
||||
r = sd_bus_default(&bus);
|
||||
if (r < 0)
|
||||
fprintf(stderr, "Failed to allocate bus: %s\n", strerror(-r));
|
||||
if (r < 0) {
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to allocate bus: %m\n");
|
||||
}
|
||||
…
|
||||
}</programlisting>
|
||||
|
||||
|
@ -62,8 +62,10 @@
|
||||
int r;
|
||||
…
|
||||
r = sd_device_new_from_syspath(&device, "…");
|
||||
if (r < 0)
|
||||
fprintf(stderr, "Failed to allocate device: %s\n", strerror(-r));
|
||||
if (r < 0) {
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to allocate device: %m\n");
|
||||
}
|
||||
…
|
||||
}</programlisting>
|
||||
|
||||
|
@ -135,8 +135,10 @@
|
||||
int r;
|
||||
…
|
||||
r = sd_event_default(&event);
|
||||
if (r < 0)
|
||||
fprintf(stderr, "Failed to allocate event loop: %s\n", strerror(-r));
|
||||
if (r < 0) {
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to allocate event loop: %m\n");
|
||||
}
|
||||
…
|
||||
}</programlisting>
|
||||
|
||||
|
@ -116,8 +116,10 @@
|
||||
int r;
|
||||
…
|
||||
r = sd_login_monitor_new(NULL, &m);
|
||||
if (r < 0)
|
||||
fprintf(stderr, "Failed to allocate login monitor object: %s\n", strerror(-r));
|
||||
if (r < 0) {
|
||||
errno = -r;
|
||||
fprintf(stderr, "Failed to allocate login monitor object: %m\n");
|
||||
}
|
||||
…
|
||||
}</programlisting>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user