3 Коммиты 09a9f69d20 ... 9091f15f65

Автор SHA1 Сообщение Дата
  rafal_rr 9091f15f65 - options passed to main.c 5 лет назад
  rafal_rr 4cc0818442 macro's convention: upper case 5 лет назад
  rafal_rr e4a138ac45 logger added 5 лет назад
7 измененных файлов с 72 добавлено и 8 удалено
  1. 1 1
      Makefile
  2. 2 2
      src/cmdline/cmdline.c
  3. 2 1
      src/directory_scanner/directory_scanner.c
  4. 8 1
      src/main.c
  5. 1 1
      src/main_trainer.c
  6. 2 2
      src/utils/error.h
  7. 56 0
      src/utils/logger.h

+ 1 - 1
Makefile

@@ -26,7 +26,7 @@ output_directories:
 	@mkdir -p $(objdir) $(dir $(obj))
 
 $(target_lib): $(obj)
-	ar rcs $@ $<
+	ar rcs $@ $^
 
 $(target_trainer): $(obj_target_trainer) $(target_lib)
 	$(CC) -o $@ $^ $(LDFLAGS)

+ 2 - 2
src/cmdline/cmdline.c

@@ -36,7 +36,7 @@ map_argument(const char *argument)
     }
   while (++mapping != options_mappings + options_mappings_length);
 
-  print_error_exit("Unknown command argument");
+  PRINT_ERROR_EXIT("Unknown command argument");
 }
 
 tgnews_options_t
@@ -44,7 +44,7 @@ parse_cmdline(int argc, char **argv)
 {
   if (argc != 3)
     {
-      print_error_exit("Too few arguments");
+      PRINT_ERROR_EXIT("Too few arguments");
     }
 
   tgnews_options_t options;

+ 2 - 1
src/directory_scanner/directory_scanner.c

@@ -1,7 +1,8 @@
 #include "directory_scanner.h"
+#include "utils/logger.h"
 
 void
 scan_directory(const char *dir_path)
 {
-
+    LOG_WARNING("Not implemented yet");
 }

+ 8 - 1
src/main.c

@@ -1,7 +1,14 @@
 #include <stdio.h>
+#include "cmdline/cmdline.h"
+#include "directory_scanner/directory_scanner.h"
+#include "utils/logger.h"
 
 int
-main()
+main(int argc, char *argv[])
 {
+    tgnews_options_t options = parse_cmdline(argc, argv);
+    LOG_INFO("Options passed: arg=%d, dir=%s", options.argument, options.directory);
+    scan_directory(options.directory);
+
     return 0;
 }

+ 1 - 1
src/main_trainer.c

@@ -1,4 +1,4 @@
-#include <cmdline/cmdline.h>
+#include "cmdline/cmdline.h"
 
 int
 main(int argc, char *argv[])

+ 2 - 2
src/utils/error.h

@@ -8,9 +8,9 @@
 #define COLOR_GREEN   "\x1b[32m"
 #define COLOR_RESET   "\x1b[0m"
 
-#define print_error_exit(error) \
+#define PRINT_ERROR_EXIT(errorMsg) \
 do { \
-  fprintf(stderr, COLOR_RED "%s:%d: %s!\n" COLOR_RESET, __FILE__, __LINE__, error); \
+  fprintf(stderr, COLOR_RED "%s:%d: %s!\n" COLOR_RESET, __FILE__, __LINE__, errorMsg); \
   exit(EXIT_FAILURE); \
 } while(0)
 

+ 56 - 0
src/utils/logger.h

@@ -0,0 +1,56 @@
+#ifndef LOGGER_H
+#define LOGGER_H
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define DEFAULT_LOGGER_SEVERITY() 1
+#ifndef LOGGER_SEVERITY
+    #define LOGGER_SEVERITY DEFAULT_LOGGER_SEVERITY()
+#endif  // LOGGER_SEVERITY
+
+// if you want change compile time severity you should pass macro definition to make. E.g.:
+// If you want see debug logs:
+// make CFLAGS=-DLOGGER_SEVERITY=0
+// If you want see only error logs:
+// make CFLAGS=-DLOGGER_SEVERITY=3
+// If you want not see any logs except of VIP:
+// make CFLAGS=-DLOGGER_SEVERITY=4
+
+#if LOGGER_SEVERITY==0
+    #define LOG_DEBUG(format, ...)\
+        printf("%s:%d: DBG: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+#else
+    #define LOG_DEBUG(...)
+#endif
+
+#if LOGGER_SEVERITY<=1
+    #define LOG_INFO(format, ...)\
+        printf("%s:%d: INF: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+#else
+    #define LOG_INFO(...)
+#endif
+
+#if LOGGER_SEVERITY<=2
+    #define LOG_WARNING(format, ...)\
+        printf("%s:%d: WRN: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+#else
+    #define LOG_WARNING(...)
+#endif
+
+#if LOGGER_SEVERITY<=3
+    #define LOG_ERROR(format, ...)\
+        printf("%s:%d: ERR: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+#else
+    #define LOG_ERROR(...)
+#endif
+
+#define LOG_VIP(format, ...)\
+    printf("%s:%d: VIP: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
+
+// TODO: correct warnings for 1-argument calls:
+// LOG_INFO("log");
+// src/main.c:13:18: warning: ISO C99 requires at least one argument for the "..." in a variadic macro
+
+
+#endif // LOGGER_H