Second Extended Filesystem (ext2) and other improvements (#33)
[lunaix-os.git] / lunaix-os / kernel / kprint / kprintf.c
index 12c421fd6d252ff48eda3c1345adb96d64e5cab0..02bc0ee9feca75a2cbca921372be3e44db26b0a1 100644 (file)
@@ -1,9 +1,12 @@
 #include <lunaix/fs/twifs.h>
 #include <lunaix/fs/twimap.h>
 #include <lunaix/syscall.h>
+#include <lunaix/syscall_utils.h>
 #include <lunaix/syslog.h>
+#include <lunaix/device.h>
+#include <lunaix/owloysius.h>
 
-#include <lunaix/lxconsole.h>
+#include <hal/term.h>
 
 #include <klibc/strfmt.h>
 
@@ -21,6 +24,7 @@ static struct kp_records kprecs = {
     .max_recs = MAX_KPENT_NUM,
     .kp_ent_wp = &kprecs.kp_ents.ents
 };
+export_symbol(debug, kprintf, kprecs);
 
 static char*
 shift_level(const char* str, int* level)
@@ -35,18 +39,24 @@ shift_level(const char* str, int* level)
     return str;
 }
 
+static inline void
+kprintf_put(int level, const char* buf, size_t sz)
+{
+    kprec_put(&kprecs, level, buf, sz);
+
+    if (likely(sysconsole)) {
+        sysconsole->ops.write(sysconsole, buf, 0, sz);
+    }
+}
+
 static inline void
 kprintf_ml(const char* component, int level, const char* fmt, va_list args)
 {
     char* buf = &tmp_buf[MAX_BUFSZ_HLF];
-    ksnprintf(buf, MAX_BUFSZ_HLF, "%s: %s", component, fmt);
+    ksnprintf(buf, MAX_BUFSZ_HLF, "%s: %s\n", component, fmt);
 
     size_t sz = ksnprintfv(tmp_buf, buf, MAX_BUFSZ_HLF, args);
-    kprec_put(&kprecs, level, tmp_buf, sz);
-
-    // FIXME temp measure, get some output
-    console_write_str(tmp_buf);
-    console_write_char('\n');
+    kprintf_put(level, tmp_buf, sz);
 }
 
 void
@@ -58,17 +68,26 @@ kprintf_m(const char* component, const char* fmt, va_list args)
     kprintf_ml(component, level, fmt, args);
 }
 
+void
+kprintf_v(const char* component, const char* fmt, ...)
+{
+    va_list args;
+    va_start(args, fmt);
+    kprintf_m(component, fmt, args);
+    va_end(args);
+}
+
 static void
 __twimap_kprintf_read(struct twimap* map)
 {
-    struct kp_records* kprecs = twimap_data(map, struct kp_records*);
+    struct kp_records* __kprecs = twimap_data(map, struct kp_records*);
 
     /*
         XXX we can foreach all records in a single twimap read call,
             as records is monotonic increasing by design.
     */
     struct kp_entry *pos, *n;
-    llist_for_each(pos, n, kprecs->kp_ent_wp, ents)
+    llist_for_each(pos, n, __kprecs->kp_ent_wp, ents)
     {
         time_t s = pos->time / 1000;
         time_t ms = pos->time % 1000;
@@ -83,7 +102,22 @@ kprintf_mapping_init()
 }
 EXPORT_TWIFS_PLUGIN(kprintf, kprintf_mapping_init);
 
-__DEFINE_LXSYSCALL3(void, syslog, int, level, const char*, fmt, va_list, args)
+
+void 
+kprintf_dump_logs() {
+    if (unlikely(!sysconsole)) {
+        return;
+    }
+
+    struct kp_entry *pos, *n;
+    llist_for_each(pos, n, kprecs.kp_ent_wp, ents)
+    {
+        sysconsole->ops.write(sysconsole, pos->content, 0, pos->len);
+    }
+}
+
+__DEFINE_LXSYSCALL3(void, syslog, int, level, 
+                    const char*, buf, unsigned int, size)
 {
-    kprintf_ml("syslog", level, fmt, args);
+    kprintf_put(level, buf, size);
 }
\ No newline at end of file