log_common.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include <stdarg.h>
  16. #include "log_common.h"
  17. #define LOG_COLOR_RED_YELLO_BACK "\033[1;31;43m"
  18. #define LOG_COLOR_RED "\033[2;31;49m"
  19. #define LOG_COLOR_YELLOW "\033[2;33;49m"
  20. #define LOG_COLOR_GREEN "\033[2;32;49m"
  21. #define LOG_COLOR_GRAY "\033[1;30m"
  22. #define LOG_COLOR_RESET "\033[0m"
  23. static char sprint_green_buf[1024]=LOG_COLOR_GREEN;
  24. int printf_green(char *fmt, ...)
  25. {
  26. va_list args;
  27. int n;
  28. int color_len = strlen(LOG_COLOR_GREEN);
  29. va_start(args, fmt);
  30. n = vsprintf(sprint_green_buf+color_len, fmt, args);
  31. va_end(args);
  32. memcpy(sprint_green_buf+color_len+n, LOG_COLOR_RESET, strlen(LOG_COLOR_RESET)+1);
  33. printf("%s", sprint_green_buf);
  34. return n;
  35. }
  36. static char sprint_red_buf[1024]=LOG_COLOR_RED;
  37. int printf_red(char *fmt, ...)
  38. {
  39. va_list args;
  40. int n;
  41. int color_len = strlen(LOG_COLOR_RED);
  42. va_start(args, fmt);
  43. n = vsprintf(sprint_red_buf+color_len, fmt, args);
  44. va_end(args);
  45. memcpy(sprint_red_buf+color_len+n, LOG_COLOR_RESET, strlen(LOG_COLOR_RESET));
  46. printf("%s", sprint_red_buf);
  47. return n;
  48. }