File: testerrno.c

package info (click to toggle)
mmlib 1.4.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: ansic: 18,071; makefile: 431; sh: 135; python: 63
file content (94 lines) | stat: -rw-r--r-- 2,035 bytes parent folder | download
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
/*
   @mindmaze_header@
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <mmerrno.h>
#include <mmlog.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <mmthread.h>
#include <string.h>

#define print_errno_info(errnum)	\
	printf("%s (%i) : %s\n", #errnum , errnum, mm_strerror(errnum))

struct mm_error_state state_in_thread3;

static
int bar(const char* name)
{
	if (!strcmp(name, "thread3"))
		return mm_raise_error_with_extid(MM_ENOTFOUND, "mmscam-calib-out", "Calibration of %s is outdated", name);

	return 0;
}

static
int foo(const char* name)
{
	if (!strcmp(name, "thread2"))
		return mm_raise_error(EINVAL, "Wrong param: bad luck");

	if (bar(name)) {
		mm_log_error("something wrong in callee of foo() in %s", name);
		return -1;
	}

	return 0;
}

static
void* thread_func(void* data)
{
	const char* thread_name = data;

	foo(thread_name);
	mm_print_lasterror("error state in %s", thread_name);

	if (!strcmp(thread_name, "thread3"))
		mm_save_errorstate(&state_in_thread3);

	return NULL;
}

int main(void)
{
	int rv;
	char buffer[512];

	setlocale(LC_ALL, "");

	print_errno_info(MM_EDISCONNECTED);
	print_errno_info(MM_EBADFMT);
	print_errno_info(MM_ENOTFOUND);
	print_errno_info(MM_ENONAME);

	mm_thread_t t1, t2, t3, t4;
	mm_thr_create(&t1, thread_func, "thread1");
	mm_thr_create(&t2, thread_func, "thread2");
	mm_thr_create(&t3, thread_func, "thread3");
	mm_thr_create(&t4, thread_func, "thread4");
	mm_thr_join(t1, NULL);
	mm_thr_join(t2, NULL);
	mm_thr_join(t3, NULL);
	mm_thr_join(t4, NULL);

	mm_set_errorstate(&state_in_thread3);
	printf("\nretrieve thread3  error state in main:\n * errnum=%i\n extid=%s * in %s at %s\n * %s\n",
			mm_get_lasterror_number(),
			mm_get_lasterror_module(),
			mm_get_lasterror_extid(),
			mm_get_lasterror_location(),
			mm_get_lasterror_desc());

	rv = mm_strerror_r(mm_get_lasterror_number(), buffer, sizeof(buffer));
	printf("\n%s to get lase error msg from number: %s\n",
			rv == 0 ? "Succeeded" : "Failed",
			buffer);

	return EXIT_SUCCESS;
}