File: util_log_example.c

package info (click to toggle)
s390-tools 2.35.0-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 12,248 kB
  • sloc: ansic: 184,236; sh: 12,152; cpp: 4,954; makefile: 2,763; perl: 2,519; asm: 1,085; python: 697; xml: 29
file content (81 lines) | stat: -rw-r--r-- 1,733 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * util_log_example - Example program for util_log
 *
 * Copyright IBM Corp. 2021
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

//! [code]
#include "lib/util_opt.h"
#include "lib/util_prg.h"
#include "lib/util_log.h"

static const struct util_prg prg = {
	.desc = "Example for util_log.",
	.copyright_vec = {
		{
			.owner = "IBM Corp.",
			.pub_first = 2021,
			.pub_last = 2021,
		},
		UTIL_PRG_COPYRIGHT_END
	}
};

static struct util_opt opt_vec[] = {
	{
		.option = { "verbose", no_argument, NULL, 'V' },
		.desc = "Print verbose messages to stderr. "
			"This option may be given multiple times and "
			"each time this option is given the verbosity level is "
			"increased.",
	},
	UTIL_OPT_HELP,
	UTIL_OPT_VERSION,
	UTIL_OPT_END
};

int main(int argc, char *argv[])
{
	int verbose = -1;

	util_prg_init(&prg);
	util_opt_init(opt_vec, NULL);

	while (1) {
		int opt = util_opt_getopt_long(argc, argv);

		if (opt == -1)
			break;

		switch (opt) {
		case 'h':
			util_prg_print_help();
			util_opt_print_help();
			exit(EXIT_SUCCESS);
		case 'v':
			util_prg_print_version();
			exit(EXIT_SUCCESS);
		case 'V':
			verbose++;
			break;
		case '?':
		default:
			fprintf(stderr, "Try '--help' for more information.\n");
			exit(EXIT_FAILURE);
		}
	}

	util_log_set_level(verbose);

	util_log_print(UTIL_LOG_ERROR, "This is an ERROR message\n");
	util_log_print(UTIL_LOG_WARN, "This is a WARN message\n");
	util_log_print(UTIL_LOG_INFO, "This is an INFO message\n");
	util_log_print(UTIL_LOG_DEBUG, "This is a DEBUG message\n");
	util_log_print(UTIL_LOG_TRACE, "This is a TRACE message\n");

	return EXIT_SUCCESS;
}
//! [code]