Browse Source

Add basics for context-based Logger

gingerBill 6 years ago
parent
commit
099995e7dd
3 changed files with 39 additions and 0 deletions
  1. 34 0
      core/log/log.odin
  2. 4 0
      core/runtime/core.odin
  3. 1 0
      src/ir.cpp

+ 34 - 0
core/log/log.odin

@@ -0,0 +1,34 @@
+package log
+
+Level :: enum {
+	Debug,
+	Info,
+	Warning,
+	Error,
+	Fatal,
+}
+
+Option :: enum {
+	Level,
+	Time,
+	File,
+	Line,
+	Procedure,
+}
+Options :: bit_set[Option];
+
+Logger_Proc :: #type proc(data: rawptr, level: Level, ident, text: string, options: Options, location := #caller_location);
+
+Logger :: struct {
+	procedure: Logger_Proc,
+	data:      rawptr,
+}
+
+
+nil_logger_proc :: proc(data: rawptr, level: Level, ident, text: string, options: Options, location := #caller_location) {
+	// Do nothing
+}
+
+nil_logger :: proc() -> Logger {
+	return Logger{nil_logger_proc, nil};
+}

+ 4 - 0
core/runtime/core.odin

@@ -5,6 +5,7 @@ package runtime
 
 
 import "core:os"
 import "core:os"
 import "core:mem"
 import "core:mem"
+import "core:log"
 
 
 // Naming Conventions:
 // Naming Conventions:
 // In general, Ada_Case for types and snake_case for values
 // In general, Ada_Case for types and snake_case for values
@@ -193,6 +194,8 @@ Context :: struct {
 
 
 	assertion_failure_proc:  Assertion_Failure_Proc,
 	assertion_failure_proc:  Assertion_Failure_Proc,
 
 
+	logger: log.Logger,
+
 	user_data:  any,
 	user_data:  any,
 	user_index: int,
 	user_index: int,
 
 
@@ -321,6 +324,7 @@ __init_context :: proc "contextless" (c: ^Context) {
 	c.temp_allocator = mem.scratch_allocator(&global_scratch_allocator_data);
 	c.temp_allocator = mem.scratch_allocator(&global_scratch_allocator_data);
 	c.thread_id = os.current_thread_id();
 	c.thread_id = os.current_thread_id();
 	c.assertion_failure_proc = default_assertion_failure_proc;
 	c.assertion_failure_proc = default_assertion_failure_proc;
+	c.logger = log.nil_logger();
 }
 }
 
 
 @(builtin)
 @(builtin)

+ 1 - 0
src/ir.cpp

@@ -1767,6 +1767,7 @@ irValue *ir_find_or_generate_context_ptr(irProcedure *proc) {
 	// 1 will be handled later
 	// 1 will be handled later
 	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 2), ir_emit_package_call(proc, "os", "current_thread_id", args));
 	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 2), ir_emit_package_call(proc, "os", "current_thread_id", args));
 	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 3), ir_get_package_value(proc->module, str_lit("runtime"), str_lit("default_assertion_failure_proc")));
 	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 3), ir_get_package_value(proc->module, str_lit("runtime"), str_lit("default_assertion_failure_proc")));
+	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 4), ir_emit_package_call(proc, "log", "nil_logger", args));
 
 
 	array_init(&args, heap_allocator(), 1);
 	array_init(&args, heap_allocator(), 1);
 	args[0] = ir_get_package_value(proc->module, str_lit("runtime"), str_lit("global_scratch_allocator_data"));
 	args[0] = ir_get_package_value(proc->module, str_lit("runtime"), str_lit("global_scratch_allocator_data"));