|
17 | 17 | * under the License.
|
18 | 18 | */
|
19 | 19 |
|
| 20 | +#include <ctype.h> |
20 | 21 | #include <pulsar/c/client.h>
|
21 | 22 | #include <stdio.h>
|
22 | 23 | #include <stdlib.h>
|
23 | 24 | #include <string.h>
|
24 | 25 | #include <time.h>
|
25 | 26 |
|
26 | 27 | char *current_time() {
|
27 |
| - char *time_str = malloc(128); |
| 28 | + char *time_str = (char *)malloc(128); |
28 | 29 | struct tm *p;
|
29 | 30 | time_t now = time(0);
|
30 | 31 | p = gmtime(&now);
|
31 | 32 | strftime(time_str, 128, "%Y-%m-%d %H:%M:%S", p);
|
32 | 33 | return time_str;
|
33 | 34 | }
|
34 | 35 |
|
35 |
| -void custom_logger(pulsar_logger_level_t level, const char *file, int line, const char *message, void *ctx) { |
| 36 | +typedef struct LogContext { |
| 37 | + FILE *file; |
| 38 | + pulsar_logger_level_t level; |
| 39 | +} LogContext; |
| 40 | + |
| 41 | +void log_context_init(LogContext *ctx, const char *level, const char *filename); |
| 42 | +void log_context_destroy(LogContext *ctx); |
| 43 | + |
| 44 | +bool is_enabled(pulsar_logger_level_t level, void *ctx) { return level >= ((LogContext *)ctx)->level; } |
| 45 | + |
| 46 | +void log_func(pulsar_logger_level_t level, const char *file, int line, const char *message, void *ctx) { |
36 | 47 | char *time_str = current_time();
|
37 |
| - printf("[%s] [%u] [%s] [%d] [%s] \n", time_str, level, file, line, message); |
| 48 | + fprintf(((LogContext *)ctx)->file, "[%s] [%u] [%s] [%d] [%s] \n", time_str, level, file, line, message); |
38 | 49 | free(time_str);
|
39 | 50 | }
|
40 | 51 |
|
41 |
| -int main() { |
| 52 | +int main(int argc, char *argv[]) { |
| 53 | + if (argc < 2) { |
| 54 | + fprintf(stderr, |
| 55 | + "Usage: %s log-level <filename>\n\n" |
| 56 | + " log-level could be DEBUG, INFO, WARN or ERROR\n" |
| 57 | + " If filename is specified, logs will be printed into the given file.\n" |
| 58 | + " Otherwise, logs will be printed into the standard output.\n", |
| 59 | + argv[0]); |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + LogContext ctx; |
| 64 | + log_context_init(&ctx, argv[1], (argc > 2) ? argv[2] : NULL); |
| 65 | + |
| 66 | + pulsar_logger_t logger; |
| 67 | + logger.ctx = &ctx; |
| 68 | + logger.is_enabled = &is_enabled; |
| 69 | + logger.log = &log_func; |
| 70 | + |
42 | 71 | pulsar_client_configuration_t *conf = pulsar_client_configuration_create();
|
43 | 72 |
|
44 |
| - pulsar_client_configuration_set_logger_and_level(conf, custom_logger, pulsar_DEBUG, NULL); |
| 73 | + pulsar_client_configuration_set_logger_t(conf, logger); |
45 | 74 | pulsar_client_configuration_set_memory_limit(conf, 64 * 1024 * 1024);
|
46 | 75 | pulsar_client_t *client = pulsar_client_create("pulsar://localhost:6650", conf);
|
47 | 76 |
|
@@ -79,4 +108,46 @@ int main() {
|
79 | 108 | pulsar_client_close(client);
|
80 | 109 | pulsar_client_free(client);
|
81 | 110 | pulsar_client_configuration_free(conf);
|
| 111 | + log_context_destroy(&ctx); |
| 112 | +} |
| 113 | + |
| 114 | +static bool str_equal_ignore_case(const char *lhs, const char *rhs) { |
| 115 | + int length = strlen(lhs); |
| 116 | + for (int i = 0; i < length; i++) { |
| 117 | + if (lhs[i] != rhs[i]) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | + return true; |
| 122 | +} |
| 123 | + |
| 124 | +void log_context_init(LogContext *ctx, const char *level, const char *filename) { |
| 125 | + if (str_equal_ignore_case(level, "debug")) { |
| 126 | + ctx->level = pulsar_DEBUG; |
| 127 | + } else if (str_equal_ignore_case(level, "info")) { |
| 128 | + ctx->level = pulsar_INFO; |
| 129 | + } else if (str_equal_ignore_case(level, "warn")) { |
| 130 | + ctx->level = pulsar_WARN; |
| 131 | + } else if (str_equal_ignore_case(level, "error")) { |
| 132 | + ctx->level = pulsar_ERROR; |
| 133 | + } else { |
| 134 | + fprintf(stderr, "Unknown log level: %s\n", level); |
| 135 | + exit(1); |
| 136 | + } |
| 137 | + |
| 138 | + if (filename) { |
| 139 | + ctx->file = fopen(filename, "w+"); |
| 140 | + if (!ctx->file) { |
| 141 | + fprintf(stderr, "Failed to open %s\n", filename); |
| 142 | + exit(2); |
| 143 | + } |
| 144 | + } else { |
| 145 | + ctx->file = stdout; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +void log_context_destroy(LogContext *ctx) { |
| 150 | + if (ctx && ctx->file && ctx->file != stdout) { |
| 151 | + fclose(ctx->file); |
| 152 | + } |
82 | 153 | }
|
0 commit comments