| 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 | FileLog &FileLog::getInstance() { |
| 26 | static FileLog instance; |
| 27 | return instance; |
| 28 | } |
| 29 |
| 30 | FileLog::FileLog() { |
| 31 | pthread_mutex_init(&mutex, NULL); |
| 32 | } |
| 33 |
| 34 | void FileLog::init(std::string path) { |
| 35 | pthread_mutex_lock(&mutex); |
| 36 | if (path.size() > 0 && logFile == nullptr) { |
| 37 | logFile = fopen(path.c_str(), "w"); |
| 38 | } |
| 39 | pthread_mutex_unlock(&mutex); |
| 40 | } |
| 41 |
| 42 | void FileLog::fatal(const char *message, ...) { |
| 43 | if (!LOGS_ENABLED) { |
| 44 | return; |
| 45 | } |
| 46 | va_list argptr; |
| 47 | va_start(argptr, message); |
| 48 | time_t t = time(0); |
| 49 | struct tm *now = localtime(&t); |
| 50 | #ifdef ANDROID |
| 51 | __android_log_vprint(ANDROID_LOG_FATAL, "tgnet", message, argptr); |
| 52 | va_end(argptr); |
| 53 | va_start(argptr, message); |
| 54 | #else |
| 55 | printf("%d-%d %02d:%02d:%02d FATAL ERROR: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 56 | vprintf(message, argptr); |
| 57 | printf("\n"); |
| 58 | fflush(stdout); |
| 59 | va_end(argptr); |
| 60 | va_start(argptr, message); |
| 61 | #endif |
| 62 | FILE *logFile = getInstance().logFile; |
| 63 | if (logFile) { |
| 64 | 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); |
| 65 | vfprintf(logFile, message, argptr); |
| 66 | fprintf(logFile, "\n"); |
| 67 | fflush(logFile); |
| 68 | } |
| 69 |
| 70 | va_end(argptr); |
| 71 |
| 72 | #ifdef DEBUG_VERSION |
| 73 | abort(); |
| 74 | #endif |
| 75 | } |
| 76 |
| 77 | void FileLog::e(const char *message, ...) { |
| 78 | if (!LOGS_ENABLED) { |
| 79 | return; |
| 80 | } |
| 81 | va_list argptr; |
| 82 | va_start(argptr, message); |
| 83 | time_t t = time(0); |
| 84 | struct tm *now = localtime(&t); |
| 85 | #ifdef ANDROID |
| 86 | __android_log_vprint(ANDROID_LOG_ERROR, "tgnet", message, argptr); |
| 87 | va_end(argptr); |
| 88 | va_start(argptr, message); |
| 89 | #else |
| 90 | printf("%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 91 | vprintf(message, argptr); |
| 92 | printf("\n"); |
| 93 | fflush(stdout); |
| 94 | va_end(argptr); |
| 95 | va_start(argptr, message); |
| 96 | #endif |
| 97 | FILE *logFile = getInstance().logFile; |
| 98 | if (logFile) { |
| 99 | fprintf(logFile, "%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 100 | vfprintf(logFile, message, argptr); |
| 101 | fprintf(logFile, "\n"); |
| 102 | fflush(logFile); |
| 103 | } |
| 104 |
| 105 | va_end(argptr); |
| 106 | } |
| 107 |
| 108 | void FileLog::w(const char *message, ...) { |
| 109 | if (!LOGS_ENABLED) { |
| 110 | return; |
| 111 | } |
| 112 | va_list argptr; |
| 113 | va_start(argptr, message); |
| 114 | time_t t = time(0); |
| 115 | struct tm *now = localtime(&t); |
| 116 | #ifdef ANDROID |
| 117 | __android_log_vprint(ANDROID_LOG_WARN, "tgnet", message, argptr); |
| 118 | va_end(argptr); |
| 119 | va_start(argptr, message); |
| 120 | #else |
| 121 | printf("%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 122 | vprintf(message, argptr); |
| 123 | printf("\n"); |
| 124 | fflush(stdout); |
| 125 | va_end(argptr); |
| 126 | va_start(argptr, message); |
| 127 | #endif |
| 128 | FILE *logFile = getInstance().logFile; |
| 129 | if (logFile) { |
| 130 | fprintf(logFile, "%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 131 | vfprintf(logFile, message, argptr); |
| 132 | fprintf(logFile, "\n"); |
| 133 | fflush(logFile); |
| 134 | } |
| 135 |
| 136 | va_end(argptr); |
| 137 | } |
| 138 |
| 139 | void FileLog::d(const char *message, ...) { |
| 140 | if (!LOGS_ENABLED) { |
| 141 | return; |
| 142 | } |
| 143 | va_list argptr; |
| 144 | va_start(argptr, message); |
| 145 | time_t t = time(0); |
| 146 | struct tm *now = localtime(&t); |
| 147 | #ifdef ANDROID |
| 148 | __android_log_vprint(ANDROID_LOG_DEBUG, "tgnet", message, argptr); |
| 149 | va_end(argptr); |
| 150 | va_start(argptr, message); |
| 151 | #else |
| 152 | printf("%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 153 | vprintf(message, argptr); |
| 154 | printf("\n"); |
| 155 | fflush(stdout); |
| 156 | va_end(argptr); |
| 157 | va_start(argptr, message); |
| 158 | #endif |
| 159 | FILE *logFile = getInstance().logFile; |
| 160 | if (logFile) { |
| 161 | fprintf(logFile, "%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 162 | vfprintf(logFile, message, argptr); |
| 163 | fprintf(logFile, "\n"); |
| 164 | fflush(logFile); |
| 165 | } |
| 166 |
| 167 | va_end(argptr); |
| 168 | } |
| 169 |