aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / klog.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* klog.c -- see klog.h. */ |
| 18 | #include "klog.h" |
| 19 | #include "fmt.h" |
| 20 | |
| 21 | static char head[CK_KLOG_HEAD]; |
| 22 | static char tail[CK_KLOG_TAIL]; |
| 23 | static uint32_t head_len; |
| 24 | static uint64_t tail_put; /* bytes put into the ring, ever */ |
| 25 | static uint64_t total; |
| 26 | |
| 27 | void ck_klog_put(const char *s, size_t n) |
| 28 | { |
| 29 | total += n; |
| 30 | for (size_t i = 0; i < n; i++) { |
| 31 | if (head_len < CK_KLOG_HEAD) |
| 32 | head[head_len++] = s[i]; |
| 33 | else |
| 34 | tail[tail_put++ % CK_KLOG_TAIL] = s[i]; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | size_t ck_klog_read(char *out, size_t cap) |
| 39 | { |
| 40 | size_t w = 0; |
| 41 | for (uint32_t i = 0; i < head_len && w < cap; i++) |
| 42 | out[w++] = head[i]; |
| 43 | uint64_t kept = tail_put < CK_KLOG_TAIL ? tail_put : CK_KLOG_TAIL; |
| 44 | if (tail_put > CK_KLOG_TAIL) { |
| 45 | char line[64]; |
| 46 | int n = ck_snprintf(line, sizeof line, "\n[klog: %llu bytes dropped]\n", |
| 47 | (unsigned long long)(tail_put - CK_KLOG_TAIL)); |
| 48 | for (int i = 0; i < n && w < cap; i++) |
| 49 | out[w++] = line[i]; |
| 50 | } |
| 51 | for (uint64_t i = tail_put - kept; i < tail_put && w < cap; i++) |
| 52 | out[w++] = tail[i % CK_KLOG_TAIL]; |
| 53 | return w; |
| 54 | } |
| 55 | |
| 56 | uint64_t ck_klog_total(void) { return total; } |
| 57 | |
| 58 | void ck_klog_clear(void) |
| 59 | { |
| 60 | head_len = 0; |
| 61 | tail_put = 0; |
| 62 | total = 0; |
| 63 | } |