CoAkka Logger · 1.2.1 · C ABI
Use CoAkka Logger with Mojo
Install the package, read a record and check the result before connecting Logger to an application.
Beginner
Install the package
This source exercise uses Mojo 1.0 beta and a small C adapter to the verified Native SDK. It needs the Mojo toolchain, a C compiler and macOS ARM64 or Linux ARM64/x86-64.
install.sh
#!/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"
Terminal
bash install.sh
Beginner
Read the first record
main.mojo
from std.ffi import OwnedDLHandle, c_int
from std.sys import argv
def main() raises:
var args = argv()
if len(args) != 2:
print("usage: mojo main.mojo <logger-shim-library>")
raise Error("missing logger shim library path")
var lib = OwnedDLHandle(String(args[1]))
var status = lib.call["coakka_mojo_logger_basic", c_int](0)
if status != 0:
raise Error("coakka_mojo_logger_basic failed")
logger_shim.c
#include "coakka/logger/core.h"
#include "coakka/logger/utils.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define COAKKA_SAMPLE_EXPORT __attribute__((visibility("default")))
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;
}
COAKKA_SAMPLE_EXPORT int coakka_mojo_logger_basic(int ignored) {
(void)ignored;
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=mojo\n",
info.abi_version,
info.runtime_version,
info.git_commit);
coakka_logger_core_config_t config = coakka_logger_core_default_config();
config.system_name = "mojo-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.mojo.basic",
"{\"event\":\"hello\",\"language\":\"mojo\"}",
&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=mojo\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;
}
run.sh
#!/usr/bin/env bash
set -euo pipefail
sdk="$PWD/coakka-logger-native-1.2.1"
case "$(uname -s):$(uname -m)" in
Darwin:arm64)
native="$sdk/native/macos-aarch64"
export DYLD_LIBRARY_PATH="$native"
shim="$PWD/logger-shim.dylib"
cc -dynamiclib -I"$sdk/include" logger_shim.c "$native/libcoakka_logger_core.dylib" -o "$shim"
;;
Linux:aarch64|Linux:x86_64)
case "$(uname -m)" in
aarch64) native="$sdk/native/linux-aarch64" ;;
x86_64) native="$sdk/native/linux-x86_64" ;;
esac
export LD_LIBRARY_PATH="$native"
shim="$PWD/logger-shim.so"
cc -shared -fPIC -I"$sdk/include" logger_shim.c "$native/libcoakka_logger_core.so" -o "$shim"
;;
*) printf '%s\n' 'This lab targets macOS ARM64 or Linux ARM64/x86-64.' >&2; exit 1 ;;
esac
mojo main.mojo "$shim"
Terminal
bash run.sh
coakka_logger_record sequence=1 level=info ...
coakka_logger_stats emitted=1 delivered=1 dropped=0
Advanced
Handle queue pressure
The native C exercise uses the same SDK to reproduce queue-full and to verify managed file output. Keep the SDK already installed, then follow the C example with the same runtime library.