| 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 | time_t t = time(0); |
| 51 | struct tm *now = localtime(&t); |
| 52 | #ifdef ANDROID |
| 53 | __android_log_vprint(ANDROID_LOG_FATAL, "tgnet", message, argptr); |
| 54 | va_end(argptr); |
| 55 | va_start(argptr, message); |
| 56 | #else |
| 57 | printf("%d-%d %02d:%02d:%02d FATAL ERROR: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 58 | vprintf(message, argptr); |
| 59 | printf("\n"); |
| 60 | fflush(stdout); |
| 61 | va_end(argptr); |
| 62 | va_start(argptr, message); |
| 63 | #endif |
| 64 | FILE *logFile = getInstance().logFile; |
| 65 | if (logFile) { |
| 66 | 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); |
| 67 | vfprintf(logFile, message, argptr); |
| 68 | fprintf(logFile, "\n"); |
| 69 | fflush(logFile); |
| 70 | } |
| 71 |
| 72 | va_end(argptr); |
| 73 |
| 74 | #ifdef DEBUG_VERSION |
| 75 | abort(); |
| 76 | #endif |
| 77 | } |
| 78 |
| 79 | void FileLog::e(const char *message, ...) { |
| 80 | if (!LOGS_ENABLED) { |
| 81 | return; |
| 82 | } |
| 83 | va_list argptr; |
| 84 | va_start(argptr, message); |
| 85 | time_t t = time(0); |
| 86 | struct tm *now = localtime(&t); |
| 87 | #ifdef ANDROID |
| 88 | __android_log_vprint(ANDROID_LOG_ERROR, "tgnet", message, argptr); |
| 89 | va_end(argptr); |
| 90 | va_start(argptr, message); |
| 91 | #else |
| 92 | printf("%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 93 | vprintf(message, argptr); |
| 94 | printf("\n"); |
| 95 | fflush(stdout); |
| 96 | va_end(argptr); |
| 97 | va_start(argptr, message); |
| 98 | #endif |
| 99 | FILE *logFile = getInstance().logFile; |
| 100 | if (logFile) { |
| 101 | fprintf(logFile, "%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 102 | vfprintf(logFile, message, argptr); |
| 103 | fprintf(logFile, "\n"); |
| 104 | fflush(logFile); |
| 105 | } |
| 106 |
| 107 | va_end(argptr); |
| 108 | } |
| 109 |
| 110 | void FileLog::w(const char *message, ...) { |
| 111 | if (!LOGS_ENABLED) { |
| 112 | return; |
| 113 | } |
| 114 | va_list argptr; |
| 115 | va_start(argptr, message); |
| 116 | time_t t = time(0); |
| 117 | struct tm *now = localtime(&t); |
| 118 | #ifdef ANDROID |
| 119 | __android_log_vprint(ANDROID_LOG_WARN, "tgnet", message, argptr); |
| 120 | va_end(argptr); |
| 121 | va_start(argptr, message); |
| 122 | #else |
| 123 | printf("%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 124 | vprintf(message, argptr); |
| 125 | printf("\n"); |
| 126 | fflush(stdout); |
| 127 | va_end(argptr); |
| 128 | va_start(argptr, message); |
| 129 | #endif |
| 130 | FILE *logFile = getInstance().logFile; |
| 131 | if (logFile) { |
| 132 | fprintf(logFile, "%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 133 | vfprintf(logFile, message, argptr); |
| 134 | fprintf(logFile, "\n"); |
| 135 | fflush(logFile); |
| 136 | } |
| 137 |
| 138 | va_end(argptr); |
| 139 | } |
| 140 |
| 141 | void FileLog::d(const char *message, ...) { |
| 142 | if (!LOGS_ENABLED) { |
| 143 | return; |
| 144 | } |
| 145 | va_list argptr; |
| 146 | va_start(argptr, message); |
| 147 | time_t t = time(0); |
| 148 | struct tm *now = localtime(&t); |
| 149 | #ifdef ANDROID |
| 150 | __android_log_vprint(ANDROID_LOG_DEBUG, "tgnet", message, argptr); |
| 151 | va_end(argptr); |
| 152 | va_start(argptr, message); |
| 153 | #else |
| 154 | printf("%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 155 | vprintf(message, argptr); |
| 156 | printf("\n"); |
| 157 | fflush(stdout); |
| 158 | va_end(argptr); |
| 159 | va_start(argptr, message); |
| 160 | #endif |
| 161 | FILE *logFile = getInstance().logFile; |
| 162 | if (logFile) { |
| 163 | fprintf(logFile, "%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 164 | vfprintf(logFile, message, argptr); |
| 165 | fprintf(logFile, "\n"); |
| 166 | fflush(logFile); |
| 167 | } |
| 168 |
| 169 | va_end(argptr); |
| 170 | } |
| 171 |
| 172 | static int refsCount = 0; |
| 173 |
| 174 | void FileLog::ref(const char *message, ...) { |
| 175 | if (!REF_LOGS_ENABLED) { |
| 176 | return; |
| 177 | } |
| 178 | va_list argptr; |
| 179 | va_start(argptr, message); |
| 180 | time_t t = time(0); |
| 181 | struct tm *now = localtime(&t); |
| 182 | refsCount++; |
| 183 | #ifdef ANDROID |
| 184 | std::ostringstream s; |
| 185 | s << refsCount << " refs (+ref): " << message; |
| 186 | __android_log_vprint(ANDROID_LOG_VERBOSE, "tgnetREF", s.str().c_str(), argptr); |
| 187 | va_end(argptr); |
| 188 | va_start(argptr, message); |
| 189 | #endif |
| 190 | va_end(argptr); |
| 191 | } |
| 192 |
| 193 | void FileLog::delref(const char *message, ...) { |
| 194 | if (!REF_LOGS_ENABLED) { |
| 195 | return; |
| 196 | } |
| 197 | va_list argptr; |
| 198 | va_start(argptr, message); |
| 199 | time_t t = time(0); |
| 200 | struct tm *now = localtime(&t); |
| 201 | refsCount--; |
| 202 | #ifdef ANDROID |
| 203 | std::ostringstream s; |
| 204 | s << refsCount << " refs (-ref): " << message; |
| 205 | __android_log_vprint(ANDROID_LOG_VERBOSE, "tgnetREF", s.str().c_str(), argptr); |
| 206 | va_end(argptr); |
| 207 | va_start(argptr, message); |
| 208 | #endif |
| 209 | va_end(argptr); |
| 210 | } |