summaryrefslogtreecommitdiffstats
path: root/lib/util/tests/test_logging.c
blob: 9b13b052f25435914ea769e7db5d1da08b095c22 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
   Unix SMB/CIFS implementation.

   A test server that only does logging.

   Copyright (C) Andrew Tridgell		1992-2005
   Copyright (C) Martin Pool			2002
   Copyright (C) Jelmer Vernooij		2002
   Copyright (C) James J Myers 			2003 <myersjj@samba.org>
   Copyright (C) Douglas Bagnall		2022

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "lib/cmdline/cmdline.h"

#ifdef USING_CMDLINE_S3
#include "lib/util/debug_s3.h"
#endif

#define BINARY_NAME "test_s4_logging"

static int log_level = 1;


#include "lib/util/debug-classes/debug-classname-table.c"

static int log_all_classes(int level)
{
	size_t i;
	const char *name = NULL;
	for (i = 0; i < ARRAY_SIZE(default_classname_table); i++) {
		name = default_classname_table[i];
		DEBUGC(i, level,
		       ("logging for '%s' [%zu], at level %d\n",
			name, i, level));

		/*
		 * That's it for the tests *here*. The invoker of this
		 * process will have set up an smb.conf that directs the
		 * output in particular ways, and will be looking to see that
		 * happens correctly.
		 */
	}
	return 0;
}


static int init_daemon(TALLOC_CTX *mem_ctx,
		       int argc,
		       const char *argv[],
		       const char **error)
{
	poptContext pc;
	int opt;
	bool ok;
	struct poptOption long_options[] = {
		POPT_AUTOHELP
		{
			.longName   = "level",
			.shortName  = 'L',
			.argInfo    = POPT_ARG_INT,
			.arg        = &log_level,
			.descrip    = "log at this level",
			.argDescrip = "LEVEL",
		},
		POPT_COMMON_SAMBA
		POPT_COMMON_DAEMON
		POPT_COMMON_VERSION
		POPT_TABLEEND
	};

	setproctitle(BINARY_NAME);

	ok = samba_cmdline_init(mem_ctx,
				SAMBA_CMDLINE_CONFIG_SERVER,
				true /* require_smbconf */);
	if (!ok) {
		*error = "Failed to init cmdline parser!\n";
		return EINVAL;
	}

	pc = samba_popt_get_context(BINARY_NAME,
				    argc,
				    argv,
				    long_options,
				    0);
	if (pc == NULL) {
		*error = "Failed to setup popt context!\n";
		return ENOTRECOVERABLE;
	}

	while((opt = poptGetNextOpt(pc)) != -1) {
		fprintf(stderr, "\nInvalid option %s: %s\n\n",
			poptBadOption(pc, 0), poptStrerror(opt));
		poptPrintUsage(pc, stderr, 0);
		return 1;
	}

	poptFreeContext(pc);

#ifdef USING_CMDLINE_S3
	reopen_logs();
#endif
	return 0;
}


int main(int argc, const char *argv[])
{
	int rc;
	const char *error = NULL;
	TALLOC_CTX *mem_ctx = talloc_stackframe();
	if (mem_ctx == NULL) {
		exit(ENOMEM);
	}

	setproctitle_init(argc, discard_const(argv), environ);

	rc = init_daemon(mem_ctx, argc, argv, &error);
	if (rc != 0) {
		fprintf(stderr, "error [%d]: %s\n", rc, error);
		exit_daemon(error, rc);
	}

	rc = log_all_classes(log_level);
	if (rc != 0) {
		fprintf(stderr, "error in log_all_classes [%d]\n", rc);
		exit_daemon("logging error", rc);
	}

	TALLOC_FREE(mem_ctx);
	return rc;
}