| 6 |
| * Copyright Nikolai Kudashov, 2015-2018. |
| 7 | */ |
| 8 |
| 9 | #include <stdio.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <time.h> |
| 12 | #include "FileLog.h" |
| 13 | #include "ConnectionsManager.h" |
| 14 |
| 15 | #ifdef ANDROID |
| 16 | #include <android/log.h> |
| 17 | #endif |
| 18 |
| 19 | #ifdef DEBUG_VERSION |
| 20 | bool LOGS_ENABLED = true; |
| 21 | #else |
| 22 | bool LOGS_ENABLED = false; |
| 23 | #endif |
| 24 |
| 25 | bool REF_LOGS_ENABLED = false; |
| 26 |
| 27 | FileLog &FileLog::getInstance() { |
| 28 | static FileLog instance; |
| 29 | return instance; |
| 30 | } |
| 31 |
| 32 | FileLog::FileLog() { |
| 33 | pthread_mutex_init(&mutex, NULL); |
| 34 | } |
| 35 |
| 36 | void FileLog::init(std::string path) { |
| 37 | pthread_mutex_lock(&mutex); |
| 38 | if (path.size() > 0 && logFile == nullptr) { |
| 39 | logFile = fopen(path.c_str(), "w"); |
| 40 | } |
| 41 | pthread_mutex_unlock(&mutex); |
| 42 | } |
| 43 |
| 44 | void FileLog::fatal(const char *message, ...) { |
| 45 | if (!LOGS_ENABLED) { |
| 46 | return; |
| 47 | } |
| 48 | va_list argptr; |
| 49 | va_start(argptr, message); |
| 50 |
| 51 | struct timeval time_now; |
| 52 | gettimeofday(&time_now, NULL); |
| 53 | struct tm *now = localtime(&time_now.tv_sec); |
| 54 | #ifdef ANDROID |
| 55 | __android_log_vprint(ANDROID_LOG_FATAL, "tgnet", message, argptr); |
| 56 | va_end(argptr); |
| 57 | va_start(argptr, message); |
| 58 | #else |
| 59 | printf("%d-%d %02d:%02d:%02d FATAL ERROR: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 60 | vprintf(message, argptr); |
| 61 | printf("\n"); |
| 62 | fflush(stdout); |
| 63 | va_end(argptr); |
| 64 | va_start(argptr, message); |
| 65 | #endif |
| 66 | FILE *logFile = getInstance().logFile; |
| 67 | if (logFile) { |
| 68 | fprintf(logFile, "%d-%d %02d:%02d:%02d.%03d FATAL ERROR: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, (int) (time_now.tv_usec / 1000)); |
| 69 | vfprintf(logFile, message, argptr); |
| 70 | fprintf(logFile, "\n"); |
| 71 | fflush(logFile); |
| 72 | } |
| 73 |
| 74 | va_end(argptr); |
| 75 |
| 76 | #ifdef DEBUG_VERSION |
| 77 | abort(); |
| 78 | #endif |
| 79 | } |
| 80 |
| 81 | void FileLog::e(const char *message, ...) { |
| 82 | if (!LOGS_ENABLED) { |
| 83 | return; |
| 84 | } |
| 85 | va_list argptr; |
| 86 | va_start(argptr, message); |
| 87 | struct timeval time_now; |
| 88 | gettimeofday(&time_now, NULL); |
| 89 | struct tm *now = localtime(&time_now.tv_sec); |
| 90 | #ifdef ANDROID |
| 91 | __android_log_vprint(ANDROID_LOG_ERROR, "tgnet", message, argptr); |
| 92 | va_end(argptr); |
| 93 | va_start(argptr, message); |
| 94 | #else |
| 95 | printf("%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 96 | vprintf(message, argptr); |
| 97 | printf("\n"); |
| 98 | fflush(stdout); |
| 99 | va_end(argptr); |
| 100 | va_start(argptr, message); |
| 101 | #endif |
| 102 | FILE *logFile = getInstance().logFile; |
| 103 | if (logFile) { |
| 104 | fprintf(logFile, "%d-%d %02d:%02d:%02d.%03d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, (int) (time_now.tv_usec / 1000)); |
| 105 | vfprintf(logFile, message, argptr); |
| 106 | fprintf(logFile, "\n"); |
| 107 | fflush(logFile); |
| 108 | } |
| 109 |
| 110 | va_end(argptr); |
| 111 | } |
| 112 |
| 113 | void FileLog::w(const char *message, ...) { |
| 114 | if (!LOGS_ENABLED) { |
| 115 | return; |
| 116 | } |
| 117 | va_list argptr; |
| 118 | va_start(argptr, message); |
| 119 | struct timeval time_now; |
| 120 | gettimeofday(&time_now, NULL); |
| 121 | struct tm *now = localtime(&time_now.tv_sec); |
| 122 | #ifdef ANDROID |
| 123 | __android_log_vprint(ANDROID_LOG_WARN, "tgnet", message, argptr); |
| 124 | va_end(argptr); |
| 125 | va_start(argptr, message); |
| 126 | #else |
| 127 | printf("%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 128 | vprintf(message, argptr); |
| 129 | printf("\n"); |
| 130 | fflush(stdout); |
| 131 | va_end(argptr); |
| 132 | va_start(argptr, message); |
| 133 | #endif |
| 134 | FILE *logFile = getInstance().logFile; |
| 135 | if (logFile) { |
| 136 | fprintf(logFile, "%d-%d %02d:%02d:%02d.%03d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, (int) (time_now.tv_usec / 1000)); |
| 137 | vfprintf(logFile, message, argptr); |
| 138 | fprintf(logFile, "\n"); |
| 139 | fflush(logFile); |
| 140 | } |
| 141 |
| 142 | va_end(argptr); |
| 143 | } |
| 144 |
| 145 | void FileLog::d(const char *message, ...) { |
| 146 | if (!LOGS_ENABLED) { |
| 147 | return; |
| 148 | } |
| 149 | va_list argptr; |
| 150 | va_start(argptr, message); |
| 151 |
| 152 | struct timeval time_now; |
| 153 | gettimeofday(&time_now, NULL); |
| 154 | struct tm *now = localtime(&time_now.tv_sec); |
| 155 | #ifdef ANDROID |
| 156 | __android_log_vprint(ANDROID_LOG_DEBUG, "tgnet", message, argptr); |
| 157 | va_end(argptr); |
| 158 | va_start(argptr, message); |
| 159 | #else |
| 160 | printf("%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 161 | vprintf(message, argptr); |
| 162 | printf("\n"); |
| 163 | fflush(stdout); |
| 164 | va_end(argptr); |
| 165 | va_start(argptr, message); |
| 166 | #endif |
| 167 | FILE *logFile = getInstance().logFile; |
| 168 | if (logFile) { |
| 169 | fprintf(logFile, "%d-%d %02d:%02d:%02d.%03d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, (int) (time_now.tv_usec / 1000)); |
| 170 | vfprintf(logFile, message, argptr); |
| 171 | fprintf(logFile, "\n"); |
| 172 | fflush(logFile); |
| 173 | } |
| 174 |
| 175 | va_end(argptr); |
| 176 | } |
| 177 |
| 178 | static int refsCount = 0; |
| 179 |
| 180 | void FileLog::ref(const char *message, ...) { |
| 181 | if (!REF_LOGS_ENABLED) { |
| 182 | return; |
| 183 | } |
| 184 | va_list argptr; |
| 185 | va_start(argptr, message); |
| 186 | refsCount++; |
| 187 | #ifdef ANDROID |
| 188 | std::ostringstream s; |
| 189 | s << refsCount << " refs (+ref): " << message; |
| 190 | __android_log_vprint(ANDROID_LOG_VERBOSE, "tgnetREF", s.str().c_str(), argptr); |
| 191 | va_end(argptr); |
| 192 | va_start(argptr, message); |
| 193 | #endif |
| 194 | va_end(argptr); |
| 195 | } |
| 196 |
| 197 | void FileLog::delref(const char *message, ...) { |
| 198 | if (!REF_LOGS_ENABLED) { |
| 199 | return; |
| 200 | } |
| 201 | va_list argptr; |
| 202 | va_start(argptr, message); |
| 203 | refsCount--; |
| 204 | #ifdef ANDROID |
| 205 | std::ostringstream s; |
| 206 | s << refsCount << " refs (-ref): " << message; |
| 207 | __android_log_vprint(ANDROID_LOG_VERBOSE, "tgnetREF", s.str().c_str(), argptr); |
| 208 | va_end(argptr); |
| 209 | va_start(argptr, message); |
| 210 | #endif |
| 211 | va_end(argptr); |
| 212 | } |