log.cpp 649 B

123456789101112131415161718192021222324252627282930313233
  1. #include <log.h>
  2. const int log_level=log_debug;
  3. char log_text[][10]={"FATAL","ERROR","WARN","INFO","DEBUG","TRACE"};
  4. char log_color[][10]={RED,RED,YEL,GRN,BLU,""};
  5. void log(int level,const char* str, ...) {
  6. if(level>log_level) return ;
  7. if(level>log_trace||level<0) return ;
  8. time_t timer;
  9. char buffer[100];
  10. struct tm* tm_info;
  11. time(&timer);
  12. tm_info = localtime(&timer);
  13. printf(log_color[level]);
  14. strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info);
  15. printf("[%s][%s]",buffer,log_text[level]);
  16. va_list vlist;
  17. va_start(vlist, str);
  18. vfprintf(stdout, str, vlist);
  19. va_end(vlist);
  20. printf(RESET);
  21. //printf("\n");
  22. fflush(stdout);
  23. }