FileLog.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * This is the source code of tgnet library v. 1.1
  3. * It is licensed under GNU GPL v. 2 or later.
  4. * You should have received a copy of the license in this archive (see LICENSE).
  5. *
  6. * Copyright Nikolai Kudashov, 2015-2018.
  7. */
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10. #include <time.h>
  11. #include "FileLog.h"
  12. #include "ConnectionsManager.h"
  13. #ifdef ANDROID
  14. #include <android/log.h>
  15. #endif
  16. #ifdef DEBUG_VERSION
  17. bool LOGS_ENABLED = true;
  18. #else
  19. bool LOGS_ENABLED = false;
  20. #endif
  21. FileLog &FileLog::getInstance() {
  22. static FileLog instance;
  23. return instance;
  24. }
  25. FileLog::FileLog() {
  26. pthread_mutex_init(&mutex, NULL);
  27. }
  28. void FileLog::init(std::string path) {
  29. pthread_mutex_lock(&mutex);
  30. if (path.size() > 0 && logFile == nullptr) {
  31. logFile = fopen(path.c_str(), "w");
  32. }
  33. pthread_mutex_unlock(&mutex);
  34. }
  35. void FileLog::fatal(const char *message, ...) {
  36. if (!LOGS_ENABLED) {
  37. return;
  38. }
  39. va_list argptr;
  40. va_start(argptr, message);
  41. time_t t = time(0);
  42. struct tm *now = localtime(&t);
  43. #ifdef ANDROID
  44. __android_log_vprint(ANDROID_LOG_FATAL, "tgnet", message, argptr);
  45. va_end(argptr);
  46. va_start(argptr, message);
  47. #else
  48. printf("%d-%d %02d:%02d:%02d FATAL ERROR: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
  49. vprintf(message, argptr);
  50. printf("\n");
  51. fflush(stdout);
  52. va_end(argptr);
  53. va_start(argptr, message);
  54. #endif
  55. FILE *logFile = getInstance().logFile;
  56. if (logFile) {
  57. fprintf(logFile, "%d-%d %02d:%02d:%02d FATAL ERROR: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
  58. vfprintf(logFile, message, argptr);
  59. fprintf(logFile, "\n");
  60. fflush(logFile);
  61. }
  62. va_end(argptr);
  63. #ifdef DEBUG_VERSION
  64. abort();
  65. #endif
  66. }
  67. void FileLog::e(const char *message, ...) {
  68. if (!LOGS_ENABLED) {
  69. return;
  70. }
  71. va_list argptr;
  72. va_start(argptr, message);
  73. time_t t = time(0);
  74. struct tm *now = localtime(&t);
  75. #ifdef ANDROID
  76. __android_log_vprint(ANDROID_LOG_ERROR, "tgnet", message, argptr);
  77. va_end(argptr);
  78. va_start(argptr, message);
  79. #else
  80. printf("%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
  81. vprintf(message, argptr);
  82. printf("\n");
  83. fflush(stdout);
  84. va_end(argptr);
  85. va_start(argptr, message);
  86. #endif
  87. FILE *logFile = getInstance().logFile;
  88. if (logFile) {
  89. fprintf(logFile, "%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
  90. vfprintf(logFile, message, argptr);
  91. fprintf(logFile, "\n");
  92. fflush(logFile);
  93. }
  94. va_end(argptr);
  95. }
  96. void FileLog::w(const char *message, ...) {
  97. if (!LOGS_ENABLED) {
  98. return;
  99. }
  100. va_list argptr;
  101. va_start(argptr, message);
  102. time_t t = time(0);
  103. struct tm *now = localtime(&t);
  104. #ifdef ANDROID
  105. __android_log_vprint(ANDROID_LOG_WARN, "tgnet", message, argptr);
  106. va_end(argptr);
  107. va_start(argptr, message);
  108. #else
  109. printf("%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
  110. vprintf(message, argptr);
  111. printf("\n");
  112. fflush(stdout);
  113. va_end(argptr);
  114. va_start(argptr, message);
  115. #endif
  116. FILE *logFile = getInstance().logFile;
  117. if (logFile) {
  118. fprintf(logFile, "%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
  119. vfprintf(logFile, message, argptr);
  120. fprintf(logFile, "\n");
  121. fflush(logFile);
  122. }
  123. va_end(argptr);
  124. }
  125. void FileLog::d(const char *message, ...) {
  126. if (!LOGS_ENABLED) {
  127. return;
  128. }
  129. va_list argptr;
  130. va_start(argptr, message);
  131. time_t t = time(0);
  132. struct tm *now = localtime(&t);
  133. #ifdef ANDROID
  134. __android_log_vprint(ANDROID_LOG_DEBUG, "tgnet", message, argptr);
  135. va_end(argptr);
  136. va_start(argptr, message);
  137. #else
  138. printf("%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
  139. vprintf(message, argptr);
  140. printf("\n");
  141. fflush(stdout);
  142. va_end(argptr);
  143. va_start(argptr, message);
  144. #endif
  145. FILE *logFile = getInstance().logFile;
  146. if (logFile) {
  147. fprintf(logFile, "%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
  148. vfprintf(logFile, message, argptr);
  149. fprintf(logFile, "\n");
  150. fflush(logFile);
  151. }
  152. va_end(argptr);
  153. }