]> www.git.momoyon.org Git - commonlib.git/commitdiff
[commonlib.h] Add log_debug...
authorahmedsamyh <ahmedsamyh10@gmail.com>
Thu, 29 May 2025 15:46:03 +0000 (20:46 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Thu, 29 May 2025 15:48:26 +0000 (20:48 +0500)
- [tests/loggin.c] Add test for log_debug.

commonlib.h
tests/.logging.out.expected
tests/logging.c

index 2a1477777235a48217e4a767686056c8d25eaa4e..30d7e5196eee1ed22dff2d168910d68583fe3575 100644 (file)
@@ -35,6 +35,7 @@
 #define log_error c_log_error\r
 #define log_info c_log_info\r
 #define log_warning c_log_warning\r
+#define log_debug c_log_debug\r
 \r
 #define read_file c_read_file\r
 #define touch_file_if_doesnt_exist c_touch_file_if_doesnt_exist\r
@@ -228,7 +229,13 @@ bool c_os_file_exists(cstr filename);
 #define c_log_warning(fmt, ...) do {\\r
                fprintf(stdout, "%s"fmt"\n", "[WARNING] ", ##__VA_ARGS__);\\r
        } while (0)\r
-\r
+#ifdef DEBUG\r
+#define c_log_debug(fmt, ...) do {\\r
+               fprintf(stdout, "%s"fmt"\n", "[DEBUG] ", ##__VA_ARGS__);\\r
+       } while (0)\r
+#else\r
+#define c_log_debug(...)\r
+#endif // DEBUG\r
 //\r
 // File\r
 //\r
@@ -410,10 +417,27 @@ const char *c_read_file(const char* filename, int *file_size) {
 \r
     size_t read = fread((char*)result, sizeof(char), fsize, f);\r
 \r
-    *file_size = (int)read;\r
+    // Process the result to remove '\r' characters\r
+    char* cleaned_result = malloc(read + 1); // Allocate memory for cleaned result\r
+    if (cleaned_result == NULL) {\r
+        fprintf(stderr, "Memory allocation failed\n");\r
+        free(result);\r
+        return NULL;\r
+    }\r
 \r
-    // set null-terminator\r
-    result[read] = '\0';\r
+    size_t j = 0; // Index for cleaned_result\r
+    for (size_t i = 0; i < read; i++) {\r
+        if (result[i] != '\r') { // Skip '\r' characters\r
+            cleaned_result[j++] = result[i];\r
+        }\r
+    }\r
+    cleaned_result[j] = '\0'; // Null-terminate the cleaned result\r
+\r
+    free(result); // Free the original result\r
+    *file_size = (int)j; // Update the file size without '\r'\r
+    return cleaned_result; // Return the cleaned result\r
+\r
+    *file_size = (int)read;\r
 \r
  defer:\r
     if (f) fclose(f);\r
index e3a0d369c820b4bf72b280442c0f4122628e19e6..03489035953e8b0402bdd7467f1574dc79a0556a 100644 (file)
@@ -1,2 +1,3 @@
 [INFO] This is an info log
 [WARNING] And this is a warning log
+[DEBUG] And this is a debug log
index 1194b0dda4eb674e07ca144392bc14f4d5989c04..f47c2dd318339dfd4dd05e43ee65d58d97500087 100644 (file)
@@ -1,3 +1,5 @@
+#define DEBUG
+
 #define COMMONLIB_IMPLEMENTATION
 #include "../commonlib.h"
 
@@ -5,6 +7,7 @@ int main(void) {
     c_log_info("This is an info log");
     c_log_error("This one is an error log");
     c_log_warning("And this is a warning log");
+    c_log_debug("And this is a debug log");
 
     return 0;
 }