summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2025-04-16 21:38:26 +0200
committerMitya Selivanov <automainint@guattari.tech>2025-04-16 21:38:26 +0200
commitbcf1992714fb95d4c4dd16f6b27f8767205bb9f3 (patch)
tree48200933df5c832322a045b7dcf1b81d4cc217d3
parenta24258441ecf47528267500770812f432fa701de (diff)
downloadreduced_system_layer-bcf1992714fb95d4c4dd16f6b27f8767205bb9f3.zip
Log level
-rwxr-xr-xreduced_system_layer.c108
1 files changed, 62 insertions, 46 deletions
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index 794f511..b2bec10 100755
--- a/reduced_system_layer.c
+++ b/reduced_system_layer.c
@@ -818,56 +818,77 @@ static i64 max2_i64_(i64 x, i64 y) {
// - Use print formatting after we implement it.
// - Print time.
+enum {
+ LOG_Level_None = 0,
+ LOG_Level_Fatal = 1,
+ LOG_Level_Error = 2,
+ LOG_Level_Warning = 3,
+ LOG_Level_Info = 4,
+ LOG_Level_Verbose = 5,
+ LOG_Level_Trace = 6,
+};
+
+static u8 LOG_level = LOG_Level_Info;
+
#if defined(__wasm__)
void log_impl(i32 mode, i32 file_len, c8 const *file, i32 line, i32 func_len, c8 const *func, i32 text_len, c8 const *text);
#define LOG_trace() \
do { \
- log_impl( \
- 0, \
- sizeof(__FILE__) - 1, __FILE__, \
- __LINE__, \
- sizeof(__func__) - 1, __func__, \
- 0, NULL); \
+ if (LOG_level >= LOG_Level_Trace) \
+ log_impl( \
+ 0, \
+ sizeof(__FILE__) - 1, __FILE__, \
+ __LINE__, \
+ sizeof(__func__) - 1, __func__, \
+ 0, NULL); \
} while (0)
-#define LOG_print(text_, ...) \
- do { \
- i32 len = 0; \
- while ((text_)[len] != '\0') ++len; \
- log_impl( \
- 0, \
- sizeof(__FILE__) - 1, __FILE__, \
- __LINE__, \
- sizeof(__func__) - 1, __func__, \
- len, text_); \
+#define LOG_print(text_, ...) \
+ do { \
+ if (LOG_level >= LOG_Level_Info) { \
+ i32 len = 0; \
+ while ((text_)[len] != '\0') ++len; \
+ log_impl( \
+ 0, \
+ sizeof(__FILE__) - 1, __FILE__, \
+ __LINE__, \
+ sizeof(__func__) - 1, __func__, \
+ len, text_); \
+ } \
} while (0)
-#define LOG_error(text_, ...) \
- do { \
- i32 len = 0; \
- while ((text_)[len] != '\0') ++len; \
- log_impl( \
- 1, \
- sizeof(__FILE__) - 1, __FILE__, \
- __LINE__, \
- sizeof(__func__) - 1, __func__, \
- len, text_); \
+#define LOG_error(text_, ...) \
+ do { \
+ if (LOG_level >= LOG_Level_Error) { \
+ i32 len = 0; \
+ while ((text_)[len] != '\0') ++len; \
+ log_impl( \
+ 1, \
+ sizeof(__FILE__) - 1, __FILE__, \
+ __LINE__, \
+ sizeof(__func__) - 1, __func__, \
+ len, text_); \
+ } \
} while (0)
#else
#include <stdio.h>
-#define LOG_trace() \
- do { \
- fprintf(stdout, "%s:%d, %s\n", __FILE__, __LINE__, __func__); \
+#define LOG_trace() \
+ do { \
+ if (LOG_level >= LOG_Level_Trace) \
+ fprintf(stdout, "%s:%d, %s\n", __FILE__, __LINE__, __func__); \
} while (0)
-#define LOG_print(...) \
- do { \
- fprintf(stdout, "%s:%d, %s: ", __FILE__, __LINE__, __func__); \
- fprintf(stdout, __VA_ARGS__); \
- fprintf(stdout, "\n"); \
+#define LOG_print(...) \
+ do { \
+ if (LOG_level >= LOG_Level_Info) { \
+ fprintf(stdout, __VA_ARGS__); \
+ fprintf(stdout, "\n"); \
+ } \
} while (0)
-#define LOG_error(...) \
- do { \
- fprintf(stderr, "%s:%d, %s: ", __FILE__, __LINE__, __func__); \
- fprintf(stderr, __VA_ARGS__); \
- fprintf(stderr, "\n"); \
+#define LOG_error(...) \
+ do { \
+ if (LOG_level >= LOG_Level_Error) { \
+ fprintf(stderr, "%s:%d, %s: ", __FILE__, __LINE__, __func__); \
+ fprintf(stderr, __VA_ARGS__); \
+ fprintf(stderr, "\n"); \
+ } \
} while (0)
#endif
@@ -1643,21 +1664,16 @@ void PROFILER_end(u32 slot) {
}
void PROFILER_report_(void) {
- // TODO: Implement printing for WebAssembly.
-
-#if !defined(__wasm__)
if (_profiler_num_slots > 0) {
- printf("\nPROFILER REPORT\n\n");
+ LOG_print("PROFILER REPORT");
for (i64 i = 0; i < _profiler_num_slots; ++i) {
if (_profiler_slots[i].name == NULL || _profiler_slots[i].amount == 0)
continue;
f64 k = _profiler_slots[i].amount == 0 ? 0. : 1. / _profiler_slots[i].amount;
f64 f = ((f64) _profiler_slots[i].time_sec) * k + ((f64 )_profiler_slots[i].time_nsec * .000000001) * k;
- printf("%-31s %3lld.%09lld / %4lld = %.6f\n", _profiler_slots[i].name, _profiler_slots[i].time_sec, _profiler_slots[i].time_nsec, _profiler_slots[i].amount, f);
+ LOG_print("%-31s %3lld.%09lld / %4lld = %.6f", _profiler_slots[i].name, _profiler_slots[i].time_sec, _profiler_slots[i].time_nsec, _profiler_slots[i].amount, f);
}
- printf("\n");
}
-#endif
}
// ================================================================