File: exception_log.c

package info (click to toggle)
uwsgi 2.0.31-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,624 kB
  • sloc: ansic: 87,072; python: 7,010; cpp: 1,133; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (126 lines) | stat: -rw-r--r-- 3,483 bytes parent folder | download | duplicates (9)
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
#include <uwsgi.h>

/*

	This is an example exception handler logging a dump of the exception packet

	It is not built by default, to discourage its usage, as it is non-thread-safe (log lines
	will be clobbered).

	You can use it as a debugger when developing true exception handlers

*/

static void uwsgi_exception_handler_log_parser_vars(char *key, uint16_t keylen, char *value, uint16_t vallen, void *data) {
	uwsgi_log("\t%.*s=%.*s\n", keylen, key, vallen, value);
}

static void uwsgi_exception_handler_log_parser_backtrace(uint16_t pos, char *value, uint16_t vallen, void *data) {
	uint16_t item = 0;
        if (pos > 0) {
                item = pos % 5;
        }

        switch(item) {
                // filename
                case 0:
			uwsgi_log("\tfilename: \"%.*s\" ", vallen, value);
                        break;
                // lineno
                case 1:
			uwsgi_log("line: %.*s ", vallen, value);
                        break;
                // function
                case 2:
			uwsgi_log("function: \"%.*s\" ", vallen, value);
                        break;
                // text
                case 3:
                        if (vallen > 0) {
				uwsgi_log("text/code: \"%.*s\" ", vallen, value);
                        }
                        break;
                // custom
                case 4:
                        if (vallen > 0) {
				uwsgi_log("custom: \"%.*s\"", vallen, value);
                        }
			uwsgi_log("\n");
                        break;
                default:
                        break;
        }

}

static void uwsgi_exception_handler_log_parser(char *key, uint16_t keylen, char *value, uint16_t vallen, void *data) {
	if (!uwsgi_strncmp(key, keylen, "vars", 4)) {
		uwsgi_log("vars:\n");
		uwsgi_hooked_parse(value, vallen, uwsgi_exception_handler_log_parser_vars, NULL);
		uwsgi_log("\n");
		return;
	}

	if (!uwsgi_strncmp(key, keylen, "backtrace", 9)) {
		uwsgi_log("backtrace:\n");
		uwsgi_hooked_parse_array(value, vallen, uwsgi_exception_handler_log_parser_backtrace, NULL);
		uwsgi_log("\n");
		return;
	}

	if (!uwsgi_strncmp(key, keylen, "class", 5)) {
		uwsgi_log("class: %.*s\n", vallen, value);
		return;
	}

	if (!uwsgi_strncmp(key, keylen, "msg", 3)) {
		uwsgi_log("msg: %.*s\n", vallen, value);
		return;
	}

	if (!uwsgi_strncmp(key, keylen, "repr", 4)) {
		uwsgi_log("repr: %.*s\n", vallen, value);
		return;
	}

	if (!uwsgi_strncmp(key, keylen, "unix", 4)) {
		uwsgi_log("unix: %.*s\n", vallen, value);
		return;
	}

	if (!uwsgi_strncmp(key, keylen, "wid", 3)) {
		uwsgi_log("wid: %.*s\n", vallen, value);
		return;
	}

	if (!uwsgi_strncmp(key, keylen, "pid", 3)) {
		uwsgi_log("pid: %.*s\n", vallen, value);
		return;
	}

	if (!uwsgi_strncmp(key, keylen, "core", 4)) {
		uwsgi_log("core: %.*s\n", vallen, value);
		return;
	}

	if (!uwsgi_strncmp(key, keylen, "node", 4)) {
		uwsgi_log("node: %.*s\n", vallen, value);
		return;
	}
}

static int uwsgi_exception_handler_log(struct uwsgi_exception_handler_instance *uehi, char *buf, size_t len) {
	uwsgi_log("\n!!! \"log\" exception handler !!!\n\n");
	uwsgi_hooked_parse(buf, len, uwsgi_exception_handler_log_parser, NULL);
	uwsgi_log("\n!!! end of \"log\" exception handler output !!!\n\n");
	return 0;
}

static void register_exception_log() {
	uwsgi_register_exception_handler("log", uwsgi_exception_handler_log);
}

struct uwsgi_plugin exception_log_plugin = {
	.name = "exception_log",
	.on_load = register_exception_log,
};