CoAkka Logger · 1.2.1 · Native SDK
Dùng CoAkka Logger với C / C++
Cài gói, đọc một bản ghi và kiểm tra kết quả trước khi đưa Logger vào ứng dụng.
Cơ bản
Cài gói
Cần CMake 3.20+, trình biên dịch C11 hoặc C++17, curl, shasum và tar. Bài này chạy trên macOS ARM64 hoặc Linux ARM64/x86-64.
#!/usr/bin/env bash
set -euo pipefail
archive=coakka-logger-native-1.2.1.tar.gz
catalog=https://raw.githubusercontent.com/phuong-tran/coakka-publish/83edd3dcfffaf18ea905013da7872b7dbfd45e18
curl --fail --silent --show-error --location \
"$catalog/logger/native/releases/1.2.1%2Bf50756ebff0d/$archive" \
--output "$archive"
printf '%s %s\n' 6411abdc74749e48efbe3385f3be6cb280625c42659ec739a1f35e5d48bdf9f3 "$archive" |
shasum -a 256 -c -
tar -xzf "$archive"
bash install.sh
cmake_minimum_required(VERSION 3.20)
project(logger_study LANGUAGES C CXX)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED YES)
find_package(CoAkkaLoggerNative CONFIG REQUIRED)
function(add_logger_example target source library)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${source}")
add_executable(${target} "${source}")
target_link_libraries(${target} PRIVATE CoAkkaLoggerNative::${library})
endif()
endfunction()
add_logger_example(logger_c basic/main.c core)
add_logger_example(logger_cpp basic/main.cpp native_cpp_connector)
if(TARGET logger_cpp)
target_compile_features(logger_cpp PRIVATE cxx_std_17)
endif()
add_logger_example(logger_pressure pressure/main.c core)
add_logger_example(logger_file file/main.c core)
#!/usr/bin/env bash
set -euo pipefail
target=$1
shift
sdk="$PWD/coakka-logger-native-1.2.1"
cmake -S . -B build -DCoAkkaLoggerNative_DIR="$sdk/cmake"
cmake --build build --target "$target"
case "$(uname -s):$(uname -m)" in
Darwin:arm64) export DYLD_LIBRARY_PATH="$sdk/native/macos-aarch64" ;;
Linux:aarch64) export LD_LIBRARY_PATH="$sdk/native/linux-aarch64" ;;
Linux:x86_64) export LD_LIBRARY_PATH="$sdk/native/linux-x86_64" ;;
*) printf '%s\n' 'This lab targets macOS ARM64 or Linux ARM64/x86-64.' >&2; exit 1 ;;
esac
"./build/$target" "$@"
Cơ bản
Đọc bản ghi đầu tiên
C
#include "coakka/logger/core.h"
#include "coakka/logger/utils.h"
#include <stdio.h>
#include <string.h>
static int require_ok(coakka_logger_status_t status, const char* step) {
if (status == COAKKA_LOGGER_STATUS_OK) {
return 0;
}
fprintf(stderr, "%s failed: %s\n", step, coakka_logger_status_name(status));
return 1;
}
int main(void) {
coakka_logger_core_info_t info;
memset(&info, 0, sizeof(info));
info.struct_size = sizeof(info);
if (require_ok(coakka_logger_core_get_info(&info), "get_info")) {
return 1;
}
printf("coakka_logger_info abi=%u version=%s git=%s language=c\n",
info.abi_version,
info.runtime_version,
info.git_commit);
coakka_logger_core_config_t config = coakka_logger_core_default_config();
config.system_name = "native-c-basic-logger";
config.queue_capacity = 8;
config.category_capacity = 96;
config.message_capacity = 160;
coakka_logger_core_handle_t* handle = NULL;
if (require_ok(coakka_logger_core_create(&config, &handle), "create")) {
return 1;
}
if (require_ok(coakka_logger_core_start(handle), "start")) {
coakka_logger_core_destroy(handle);
return 1;
}
uint64_t sequence = 0;
if (require_ok(coakka_logger_core_log_info(handle,
"samples.logger.native.c.basic",
"{\"event\":\"hello\",\"language\":\"c\"}",
&sequence),
"log_info")) {
coakka_logger_core_destroy(handle);
return 1;
}
char category[96];
char message[160];
memset(category, 0, sizeof(category));
memset(message, 0, sizeof(message));
coakka_logger_core_record_buffer_t record;
memset(&record, 0, sizeof(record));
record.struct_size = sizeof(record);
record.category = category;
record.category_capacity = sizeof(category);
record.message = message;
record.message_capacity = sizeof(message);
if (require_ok(coakka_logger_core_read_next(handle, 100, &record), "read_next")) {
coakka_logger_core_destroy(handle);
return 1;
}
printf("coakka_logger_record sequence=%llu level=%s category=%.*s message=%.*s\n",
(unsigned long long)record.sequence,
coakka_logger_level_name(record.level),
(int)record.category_length,
record.category,
(int)record.message_length,
record.message);
coakka_logger_core_stats_t stats;
memset(&stats, 0, sizeof(stats));
stats.struct_size = sizeof(stats);
if (require_ok(coakka_logger_core_get_stats(handle, &stats), "get_stats")) {
coakka_logger_core_destroy(handle);
return 1;
}
printf("coakka_logger_stats emitted=%llu delivered=%llu dropped=%llu language=c\n",
(unsigned long long)stats.emitted_count,
(unsigned long long)stats.delivered_count,
(unsigned long long)stats.dropped_count);
if (require_ok(coakka_logger_core_stop(handle), "stop")) {
coakka_logger_core_destroy(handle);
return 1;
}
coakka_logger_core_destroy(handle);
return sequence == 1 ? 0 : 1;
}
bash run.sh logger_c
C++
#include "coakka/logger/native_cpp/Connector.h"
#include <chrono>
#include <iostream>
#include <stdexcept>
int main() {
using coakka::logger::native_cpp::LoggerOrchestrator;
using coakka::logger::native_cpp::StartSpec;
const auto info = LoggerOrchestrator::readLoggerInfo();
std::cout << "coakka_logger_info abi=" << info.abi_version
<< " version=" << info.runtime_version
<< " git=" << info.git_commit
<< " language=cpp\n";
StartSpec spec;
spec.system_name = "native-cpp-basic-logger";
spec.queue_capacity = 8;
spec.category_capacity = 96;
spec.message_capacity = 160;
LoggerOrchestrator logger(spec);
const auto sequence =
logger.logInfo("samples.logger.native.cpp.basic", "{\"event\":\"hello\",\"language\":\"cpp\"}");
const auto record = logger.await(std::chrono::milliseconds(100));
if (!record.has_value()) {
throw std::runtime_error("expected one logger record");
}
std::cout << "coakka_logger_record sequence=" << record->sequence
<< " level=" << record->level_name
<< " category=" << record->category
<< " message=" << record->message
<< '\n';
const auto stats = logger.readStats();
std::cout << "coakka_logger_stats emitted=" << stats.emitted_count
<< " delivered=" << stats.delivered_count
<< " dropped=" << stats.dropped_count
<< " language=cpp\n";
logger.stop();
return sequence == 1 ? 0 : 1;
}
bash run.sh logger_cpp
-
system_name - Đặt tên cho phiên Logger.
-
min_level - Đặt ngưỡng mức log được nhận.
-
coakka_logger_core_read_next - Giới hạn thời gian chờ đọc. Đối chiếu số thứ tự đọc được với lần gửi đã nhận.
coakka_logger_record sequence=1 level=info ...
coakka_logger_stats emitted=1 delivered=1 dropped=0
Nâng cao
Xử lý hàng đợi đầy
Tạo chương trình riêng cho bài này. Đặt sức chứa là 2, gửi 8 bản ghi đủ mức rồi mới đọc. Kiểm tra kết quả hàng đợi đầy trước khi đếm lần bị từ chối.
#include "coakka/logger/core.h"
#include "coakka/logger/utils.h"
#include <stdio.h>
#include <string.h>
static int fail(const char* message) {
fprintf(stderr, "%s\n", message);
return 1;
}
static int require_ok(coakka_logger_status_t status, const char* step) {
if (status == COAKKA_LOGGER_STATUS_OK) {
return 0;
}
fprintf(stderr, "%s failed: %s\n", step, coakka_logger_status_name(status));
return 1;
}
int main(void) {
const int attempts = 8;
int accepted = 0;
int rejected = 0;
int drained = 0;
coakka_logger_core_config_t config = coakka_logger_core_default_config();
config.system_name = "native-c-pressure-logger";
config.queue_capacity = 2;
config.min_level = COAKKA_LOGGER_LEVEL_INFO;
config.category_capacity = 96;
config.message_capacity = 160;
coakka_logger_core_handle_t* handle = NULL;
if (require_ok(coakka_logger_core_create(&config, &handle), "create")) {
return 1;
}
if (require_ok(coakka_logger_core_start(handle), "start")) {
coakka_logger_core_destroy(handle);
return 1;
}
for (int index = 0; index < attempts; ++index) {
char message[96];
snprintf(message, sizeof(message), "{\"event\":\"pressure\",\"index\":%d}", index);
uint64_t sequence = 0;
const coakka_logger_status_t status =
coakka_logger_core_log_info(handle, "samples.logger.native.c.pressure", message, &sequence);
if (status == COAKKA_LOGGER_STATUS_OK) {
accepted += 1;
} else if (status == COAKKA_LOGGER_STATUS_QUEUE_FULL) {
rejected += 1;
} else {
fprintf(stderr, "log_info failed: %s\n", coakka_logger_status_name(status));
coakka_logger_core_destroy(handle);
return 1;
}
}
coakka_logger_core_stats_t before_drain;
memset(&before_drain, 0, sizeof(before_drain));
before_drain.struct_size = sizeof(before_drain);
if (require_ok(coakka_logger_core_get_stats(handle, &before_drain), "get_stats_before_drain")) {
coakka_logger_core_destroy(handle);
return 1;
}
for (;;) {
char category[96];
char message[160];
memset(category, 0, sizeof(category));
memset(message, 0, sizeof(message));
coakka_logger_core_record_buffer_t record;
memset(&record, 0, sizeof(record));
record.struct_size = sizeof(record);
record.category = category;
record.category_capacity = sizeof(category);
record.message = message;
record.message_capacity = sizeof(message);
const coakka_logger_status_t status = coakka_logger_core_read_next(handle, 0, &record);
if (status == COAKKA_LOGGER_STATUS_TIMED_OUT) {
break;
}
if (status != COAKKA_LOGGER_STATUS_OK) {
fprintf(stderr, "read_next failed: %s\n", coakka_logger_status_name(status));
coakka_logger_core_destroy(handle);
return 1;
}
drained += 1;
}
coakka_logger_core_stats_t after_drain;
memset(&after_drain, 0, sizeof(after_drain));
after_drain.struct_size = sizeof(after_drain);
if (require_ok(coakka_logger_core_get_stats(handle, &after_drain), "get_stats_after_drain")) {
coakka_logger_core_destroy(handle);
return 1;
}
if (accepted != 2) {
coakka_logger_core_destroy(handle);
return fail("expected accepted=2");
}
if (rejected != attempts - accepted) {
coakka_logger_core_destroy(handle);
return fail("expected rejected=attempts-accepted");
}
if (drained != accepted) {
coakka_logger_core_destroy(handle);
return fail("expected drained=accepted");
}
if (before_drain.queue_high_watermark != 2) {
coakka_logger_core_destroy(handle);
return fail("expected queueHighWatermark=2");
}
if (after_drain.dropped_count != (uint64_t)rejected) {
coakka_logger_core_destroy(handle);
return fail("expected dropped=rejected");
}
printf("coakka_logger_pressure attempts=%d accepted=%d rejected=%d capacity=%u highWatermark=%u language=c\n",
attempts,
accepted,
rejected,
after_drain.queue_capacity,
after_drain.queue_high_watermark);
printf("coakka_logger_stats emitted=%llu delivered=%llu dropped=%llu language=c\n",
(unsigned long long)after_drain.emitted_count,
(unsigned long long)after_drain.delivered_count,
(unsigned long long)after_drain.dropped_count);
if (require_ok(coakka_logger_core_stop(handle), "stop")) {
coakka_logger_core_destroy(handle);
return 1;
}
coakka_logger_core_destroy(handle);
return 0;
}
bash run.sh logger_pressure
coakka_logger_pressure attempts=8 accepted=2 rejected=6 capacity=2 highWatermark=2
coakka_logger_stats emitted=2 delivered=2 dropped=6
Nâng cao
Ghi vào tệp do Logger quản lý
Dùng thư mục có quyền ghi. Chờ Logger ghi xong rồi đối chiếu cả bộ đếm ghi và nội dung tệp.
#include "coakka/logger/core.h"
#include "coakka/logger/utils.h"
#include <inttypes.h>
#include <stdio.h>
static int check(coakka_logger_status_t status, const char *operation) {
if (status == COAKKA_LOGGER_STATUS_OK) {
return 1;
}
fprintf(stderr, "%s: %s\n", operation, coakka_logger_status_name(status));
return 0;
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: logger_file /path/to/study.log\n");
return 1;
}
coakka_logger_core_config_t config = coakka_logger_core_default_config();
config.system_name = "orders-file-study";
config.sink_mode = COAKKA_LOGGER_SINK_MODE_FILE;
config.output_path = argv[1];
config.max_file_size_bytes = 64 * 1024;
config.max_total_size_bytes = 256 * 1024;
config.max_archived_files = 2;
coakka_logger_core_handle_t *logger = NULL;
if (!check(coakka_logger_core_create(&config, &logger), "create")) {
return 1;
}
int result = 1;
if (!check(coakka_logger_core_start(logger), "start")) {
goto cleanup;
}
uint64_t sequence = 0;
const int accepted = check(
coakka_logger_core_log_info(logger, "orders.checkout", "order 42 accepted", &sequence),
"log order");
// Stop joins the managed writer before reading its final counters.
if (!check(coakka_logger_core_stop(logger), "stop") || !accepted) {
goto cleanup;
}
coakka_logger_core_stats_t stats = {.struct_size = sizeof(stats)};
if (!check(coakka_logger_core_get_stats(logger, &stats), "read counters")) {
goto cleanup;
}
printf("sequence=%" PRIu64 " written=%" PRIu64 " write_failures=%" PRIu64 "\n",
sequence, stats.sink_write_count, stats.sink_write_failure_count);
if (stats.sink_write_count == 1 && stats.sink_write_failure_count == 0) {
result = 0;
}
cleanup:
coakka_logger_core_destroy(logger);
return result;
}
mkdir -p logs
bash run.sh logger_file "$PWD/logs/study.log"
grep -F "order 42 accepted" logs/study.log
sequence=1 written=1 write_failures=0
Bài đặt ngưỡng chuyển tệp đang ghi là 64 KiB, giới hạn dung lượng
tệp quản lý là 256 KiB và giữ tối đa 2 tệp lưu. Chương trình dừng
phần ghi trước khi đọc bộ đếm. Mỗi lần chạy tạo một thư mục riêng
trong logs.