Skip to content

Commit

Permalink
Merge pull request systemd#10850 from poettering/log-setup
Browse files Browse the repository at this point in the history
reduce some logging boilerplate
  • Loading branch information
keszybz authored Nov 20, 2018
2 parents ad0cdb6 + 6bf3c61 commit 7fa0269
Show file tree
Hide file tree
Showing 56 changed files with 77 additions and 167 deletions.
4 changes: 1 addition & 3 deletions src/backlight/backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
10 changes: 10 additions & 0 deletions src/basic/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -1337,3 +1337,13 @@ int log_dup_console(void) {
console_fd = copy;
return 0;
}

void log_setup_service(void) {
/* Sets up logging the way it is most appropriate for running a program as a service. Note that using this
* doesn't make the binary unsuitable for invocation on the command line, as log output will still go to the
* terminal if invoked interactively. */

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
}
2 changes: 2 additions & 0 deletions src/basic/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,5 @@ int log_syntax_invalid_utf8_internal(
})

#define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)

void log_setup_service(void);
4 changes: 1 addition & 3 deletions src/binfmt/binfmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ int main(int argc, char *argv[]) {
if (r <= 0)
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
6 changes: 2 additions & 4 deletions src/boot/bless-boot-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unistd.h>

#include "efivars.h"
#include "generator.h"
#include "log.h"
#include "mkdir.h"
#include "special.h"
Expand All @@ -21,10 +22,7 @@ static const char *arg_dest = "/tmp";
int main(int argc, char *argv[]) {
const char *p;

log_set_prohibit_ipc(true);
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_generator();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/cgroups-agent/cgroups-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
if (fd < 0) {
Expand Down
4 changes: 1 addition & 3 deletions src/coredump/coredump.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,7 @@ static int process_socket(int fd) {

assert(fd >= 0);

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

log_debug("Processing coredump received on stdin...");

Expand Down
5 changes: 1 addition & 4 deletions src/cryptsetup/cryptsetup-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,7 @@ int main(int argc, char *argv[]) {
if (argc > 1)
arg_dest = argv[1];

log_set_prohibit_ipc(true);
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_generator();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/cryptsetup/cryptsetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,7 @@ int main(int argc, char *argv[]) {
goto finish;
}

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
6 changes: 2 additions & 4 deletions src/debug-generator/debug-generator.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */

#include "alloc-util.h"
#include "generator.h"
#include "main-func.h"
#include "mkdir.h"
#include "parse-util.h"
Expand Down Expand Up @@ -152,10 +153,7 @@ static int run(int argc, char *argv[]) {
if (argc > 1)
arg_dest = argv[2];

log_set_prohibit_ipc(true);
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_generator();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/firstboot/firstboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -949,9 +949,7 @@ int main(int argc, char *argv[]) {
if (r <= 0)
goto finish;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/fsck/fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ static int run(int argc, char *argv[]) {
int r, exit_status;
pid_t pid;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

if (argc > 2) {
log_error("This program expects one or no arguments.");
Expand Down
5 changes: 1 addition & 4 deletions src/fstab-generator/fstab-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -870,10 +870,7 @@ int main(int argc, char *argv[]) {
if (argc > 3)
arg_dest_late = argv[3];

log_set_prohibit_ipc(true);
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_generator();

umask(0022);

Expand Down
6 changes: 2 additions & 4 deletions src/getty-generator/getty-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "generator.h"
#include "log.h"
#include "mkdir.h"
#include "path-util.h"
Expand Down Expand Up @@ -120,10 +121,7 @@ int main(int argc, char *argv[]) {
if (argc > 1)
arg_dest = argv[1];

log_set_prohibit_ipc(true);
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_generator();

umask(0022);

Expand Down
5 changes: 1 addition & 4 deletions src/gpt-auto-generator/gpt-auto-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,7 @@ int main(int argc, char *argv[]) {
if (argc > 1)
arg_dest = argv[3];

log_set_prohibit_ipc(true);
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_generator();

umask(0022);

Expand Down
6 changes: 2 additions & 4 deletions src/hibernate-resume/hibernate-resume-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "alloc-util.h"
#include "fstab-util.h"
#include "generator.h"
#include "log.h"
#include "mkdir.h"
#include "proc-cmdline.h"
Expand Down Expand Up @@ -68,10 +69,7 @@ static int process_resume(void) {
int main(int argc, char *argv[]) {
int r = 0;

log_set_prohibit_ipc(true);
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_generator();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/hibernate-resume/hibernate-resume.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/hostname/hostnamed.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,7 @@ int main(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);
mac_selinux_init();
Expand Down
4 changes: 1 addition & 3 deletions src/import/importd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(manager_unrefp) Manager *m = NULL;
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/initctl/initctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/journal-remote/journal-gatewayd.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,9 +990,7 @@ int main(int argc, char *argv[]) {
struct MHD_Daemon *d = NULL;
int r, n;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

r = parse_argv(argc, argv);
if (r < 0)
Expand Down
4 changes: 1 addition & 3 deletions src/locale/localed.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,7 @@ int main(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);
mac_selinux_init();
Expand Down
4 changes: 1 addition & 3 deletions src/login/logind.c
Original file line number Diff line number Diff line change
Expand Up @@ -1171,10 +1171,8 @@ static int run(int argc, char *argv[]) {
_cleanup_(manager_unrefp) Manager *m = NULL;
int r;

log_set_target(LOG_TARGET_AUTO);
log_set_facility(LOG_AUTH);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/machine/machined.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,8 @@ static int run(int argc, char *argv[]) {
_cleanup_(manager_unrefp) Manager *m = NULL;
int r;

log_set_target(LOG_TARGET_AUTO);
log_set_facility(LOG_AUTH);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/modules-load/modules-load.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ int main(int argc, char *argv[]) {
if (r <= 0)
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/network/networkd.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ int main(int argc, char *argv[]) {
gid_t gid;
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/network/wait-online/wait-online.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ int main(int argc, char *argv[]) {
_cleanup_(manager_freep) Manager *m = NULL;
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/partition/growfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,7 @@ int main(int argc, char *argv[]) {
struct statfs sfs;
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

r = parse_argv(argc, argv);
if (r < 0)
Expand Down
4 changes: 1 addition & 3 deletions src/partition/makefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ static int run(int argc, char *argv[]) {
struct stat st;
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

if (argc != 3) {
log_error("This program expects two arguments.");
Expand Down
4 changes: 1 addition & 3 deletions src/portable/portabled.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ static int run(int argc, char *argv[]) {
_cleanup_(manager_unrefp) Manager *m = NULL;
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

umask(0022);

Expand Down
4 changes: 1 addition & 3 deletions src/quotacheck/quotacheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ static void test_files(void) {
static int run(int argc, char *argv[]) {
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

if (argc > 1) {
log_error("This program takes no arguments.");
Expand Down
4 changes: 1 addition & 3 deletions src/random-seed/random-seed.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ static int run(int argc, char *argv[]) {
FILE *f;
int r;

log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
log_setup_service();

if (argc != 2) {
log_error("This program requires one argument.");
Expand Down
Loading

0 comments on commit 7fa0269

Please sign in to comment.