-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.c
127 lines (103 loc) · 4.22 KB
/
main.c
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
#include "pact.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef CURL
#include <curl/curl.h>
#endif
#define ERROR_MSG_LEN 256
int main(void) {
int status = 0;
/*=======================================================================
* Begin logger setup.
*---------------------------------------------------------------------*/
pactffi_logger_init();
/*=======================================================================
* Attach a sink pointing info-level output to stdout.
*---------------------------------------------------------------------*/
status = pactffi_logger_attach_sink("stdout", LevelFilter_Info);
if (status != 0) {
char error_msg[ERROR_MSG_LEN];
int error = pactffi_get_error_message(error_msg, ERROR_MSG_LEN);
printf("%s\n", error_msg);
return EXIT_FAILURE;
}
/*=======================================================================
* Attach another sink pointing debug output to a log file.
*---------------------------------------------------------------------*/
status = pactffi_logger_attach_sink("file ./pm_ffi.log", LevelFilter_Debug);
if (status != 0) {
char error_msg[ERROR_MSG_LEN];
int error = pactffi_get_error_message(error_msg, ERROR_MSG_LEN);
printf("%s\n", error_msg);
return EXIT_FAILURE;
}
/*=======================================================================
* Attach another sink to collect log events into a memory buffer.
*---------------------------------------------------------------------*/
status = pactffi_logger_attach_sink("buffer", LevelFilter_Trace);
if (status != 0) {
char error_msg[ERROR_MSG_LEN];
int error = pactffi_get_error_message(error_msg, ERROR_MSG_LEN);
printf("%s\n", error_msg);
return EXIT_FAILURE;
}
/*=======================================================================
* Apply the logger, completing logging setup.
*---------------------------------------------------------------------*/
status = pactffi_logger_apply();
if (status != 0) {
char error_msg[ERROR_MSG_LEN];
int error = pactffi_get_error_message(error_msg, ERROR_MSG_LEN);
printf("%s\n", error_msg);
return EXIT_FAILURE;
}
pactffi_log_message("example C", "debug", "This is a debug message");
pactffi_log_message("example C", "info", "This is an info message");
pactffi_log_message("example C", "error", "This is an error message");
pactffi_log_message("example C", "trace", "This is a trace message");
const char *logs = pactffi_fetch_log_buffer(NULL);
if (logs == NULL) {
printf("Could not get the buffered logs\n");
return EXIT_FAILURE;
}
printf("---- Logs from buffer ----\n");
printf("%s", logs);
printf("--------------------------\n");
int len = strlen(logs);
if (len == 0) {
printf("Buffered logs are empty\n");
return EXIT_FAILURE;
}
pactffi_string_delete(logs);
/**
* Test the logs from the mock server
*/
#ifdef CURL
PactHandle pact = pactffi_new_pact("logging-test", "logging-test");
int port = pactffi_create_mock_server_for_transport(pact, "127.0.0.1", 0, "http", NULL);
CURL *curl = curl_easy_init();
if (curl) {
char url[32];
sprintf(url, "http://localhost:%d/", port);
printf("Executing request against %s\n", url);
curl_easy_setopt(curl, CURLOPT_URL, url);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
const char *mockserver_logs = pactffi_mock_server_logs(port);
printf("---- Logs from mock server ----\n");
printf("%s", mockserver_logs);
printf("--------------------------\n");
int mockserver_logs_len = strlen(mockserver_logs);
pactffi_cleanup_mock_server(port);
if (mockserver_logs_len == 0) {
printf("Mock server logs are empty\n");
return EXIT_FAILURE;
}
} else {
printf("CURL is not available\n");
return EXIT_FAILURE;
}
#endif
return EXIT_SUCCESS;
}