log.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*************************************************************************
  2. > File Name: log.c
  3. > Author:
  4. ************************************************************************/
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <stdarg.h>
  12. #include <time.h>
  13. #include <pthread.h>
  14. int safe_asprintf(char **strp, const char *fmt, ...);
  15. int safe_vasprintf(char **strp, const char *fmt, va_list ap);
  16. void plog(const char *format, ...) ;
  17. void pinfo(const char *format, ...) ;
  18. #define DEBUG
  19. #ifdef DEBUG
  20. void plog(const char *format, ...);
  21. void pinfo(const char *format, ...);
  22. #define debug(fmt, args...) plog(fmt, ##args)
  23. #else
  24. #define debug(fmt, args...) do{}while(0)
  25. #endif
  26. static pthread_mutex_t fileMutex = PTHREAD_MUTEX_INITIALIZER;
  27. int main(int argc, char *argv)
  28. {
  29. return 0;
  30. }
  31. /*
  32. * safe_asprintf();
  33. */
  34. int safe_asprintf(char **strp, const char *fmt, ...)
  35. {
  36. va_list ap;
  37. int retval;
  38. va_start(ap, fmt);
  39. retval = safe_vasprintf(strp, fmt, ap);
  40. va_end(ap);
  41. return retval;
  42. }
  43. /*
  44. * safe_vasprintf();
  45. */
  46. int safe_vasprintf(char **strp, const char *fmt, va_list ap)
  47. {
  48. int retval;
  49. retval = vasprintf(strp, fmt, ap);
  50. if (retval == -1)
  51. {
  52. printf("Failed to vasprintf: %s. Bailing out\n", strerror(errno));
  53. return 1;
  54. }
  55. return retval;
  56. }
  57. /*
  58. * plog();
  59. */
  60. void plog(const char *format, ...)
  61. {
  62. pthread_mutex_lock(&fileMutex);
  63. FILE *fp = NULL;
  64. va_list vlist;
  65. char *fmt = NULL;
  66. // Open debug info output file.
  67. if (!(fp = fopen("log.txt", "a+"))) {
  68. pthread_mutex_unlock(&fileMutex);
  69. return;
  70. }
  71. va_start(vlist, format);
  72. safe_vasprintf(&fmt, format, vlist);
  73. va_end(vlist);
  74. if (!fmt) {
  75. pthread_mutex_unlock(&fileMutex);
  76. return;
  77. }
  78. time_t timep;
  79. struct tm *ptm = NULL;
  80. time(&timep);
  81. ptm = localtime(&timep);
  82. fprintf(fp, "[%04d-%02d-%02d-%02d-%02d-%02d] %s",
  83. ptm->tm_year + 1900,
  84. ptm->tm_mon + 1,
  85. ptm->tm_mday,
  86. ptm->tm_hour,
  87. ptm->tm_min,
  88. ptm->tm_sec,
  89. fmt);
  90. free(fmt);
  91. fsync(fileno(fp));
  92. fclose(fp);
  93. pthread_mutex_unlock(&fileMutex);
  94. }
  95. /*
  96. * pinfo();
  97. */
  98. void pinfo(const char *format, ...)
  99. {
  100. pthread_mutex_lock(&fileMutex);
  101. FILE *fp = NULL;
  102. va_list vlist;
  103. char *fmt = NULL;
  104. // Open debug info output file.
  105. if (!(fp = fopen("log.txt", "a+"))) {
  106. pthread_mutex_unlock(&fileMutex);
  107. return;
  108. }
  109. va_start(vlist, format);
  110. safe_vasprintf(&fmt, format, vlist);
  111. va_end(vlist);
  112. if (!fmt) {
  113. pthread_mutex_unlock(&fileMutex);
  114. return;
  115. }
  116. fprintf(fp, "%s", fmt);
  117. free(fmt);
  118. fsync(fileno(fp));
  119. fclose(fp);
  120. pthread_mutex_unlock(&fileMutex);
  121. }