File: log-example.c

package info (click to toggle)
rpma 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,040 kB
  • sloc: ansic: 27,313; sh: 1,805; perl: 1,148; makefile: 8
file content (72 lines) | stat: -rw-r--r-- 1,735 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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020, Intel Corporation */

/*
 * log-example.c -- an example of how to use and control behavior of the log.
 */

#include <stdarg.h>
#include <stdio.h>

#include "librpma.h"

extern void log_worker_is_doing_something(void);

static void
user_log_function(int level, const char *file_name, const int line_no,
		const char *func_name, const char *message_format, ...)
{
	if (((NULL != file_name) && (NULL == func_name)) ||
			(NULL == message_format)) {
		return;
	}
	if (fprintf(stderr, "Custom log handling: \n") < 0)
		return;
	if (NULL != file_name) {
		if (fprintf(stderr, "%s %4d %s:\n", file_name, line_no,
				func_name) < 0) {
			return;
		}
	}

	if (fprintf(stderr, "level: %d ", level) < 0)
		return;

	va_list args;
	va_start(args, message_format);
	if (vfprintf(stderr, message_format, args) < 0) {
		va_end(args);
		return;
	}
	va_end(args);

	if (NULL != file_name)
		fprintf(stderr, "\n");
}

int
main(int argc, char *argv[])
{
	/*
	 * log messages to be produced to syslog as well as stderr
	 */
	printf("Let's write messages to stderr and syslog\n");
	rpma_log_set_threshold(RPMA_LOG_THRESHOLD,
			RPMA_LOG_LEVEL_DEBUG);
	rpma_log_set_threshold(RPMA_LOG_THRESHOLD_AUX,
			RPMA_LOG_LEVEL_DEBUG);
	log_worker_is_doing_something();
	printf(
		"Use: \n$ sudo tail -n 60 /var/log/syslog | grep rpma\nto see messages in the syslog.");

	/*
	 * log messages to be transferred only to custom user function
	 */
	rpma_log_set_function(user_log_function);
	printf(
		"Let's use custom log function to write messages to stderr\nNo message should be written to syslog\n");
	log_worker_is_doing_something();
	rpma_log_set_function(RPMA_LOG_USE_DEFAULT_FUNCTION);

	return 0;
}