| 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::e(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_ERROR, "tgnet", message, argptr); |
| 52 | va_end(argptr); |
| 53 | va_start(argptr, message); |
| 54 | #else |
| 55 | printf("%d-%d %02d:%02d:%02d 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 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 |
| 73 | void FileLog::w(const char *message, ...) { |
| 74 | if (!LOGS_ENABLED) { |
| 75 | return; |
| 76 | } |
| 77 | va_list argptr; |
| 78 | va_start(argptr, message); |
| 79 | time_t t = time(0); |
| 80 | struct tm *now = localtime(&t); |
| 81 | #ifdef ANDROID |
| 82 | __android_log_vprint(ANDROID_LOG_WARN, "tgnet", message, argptr); |
| 83 | va_end(argptr); |
| 84 | va_start(argptr, message); |
| 85 | #else |
| 86 | printf("%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 87 | vprintf(message, argptr); |
| 88 | printf("\n"); |
| 89 | fflush(stdout); |
| 90 | va_end(argptr); |
| 91 | va_start(argptr, message); |
| 92 | #endif |
| 93 | FILE *logFile = getInstance().logFile; |
| 94 | if (logFile) { |
| 95 | fprintf(logFile, "%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 96 | vfprintf(logFile, message, argptr); |
| 97 | fprintf(logFile, "\n"); |
| 98 | fflush(logFile); |
| 99 | } |
| 100 |
| 101 | va_end(argptr); |
| 102 | } |
| 103 |
| 104 | void FileLog::d(const char *message, ...) { |
| 105 | if (!LOGS_ENABLED) { |
| 106 | return; |
| 107 | } |
| 108 | va_list argptr; |
| 109 | va_start(argptr, message); |
| 110 | time_t t = time(0); |
| 111 | struct tm *now = localtime(&t); |
| 112 | #ifdef ANDROID |
| 113 | __android_log_vprint(ANDROID_LOG_DEBUG, "tgnet", message, argptr); |
| 114 | va_end(argptr); |
| 115 | va_start(argptr, message); |
| 116 | #else |
| 117 | printf("%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 118 | vprintf(message, argptr); |
| 119 | printf("\n"); |
| 120 | fflush(stdout); |
| 121 | va_end(argptr); |
| 122 | va_start(argptr, message); |
| 123 | #endif |
| 124 | FILE *logFile = getInstance().logFile; |
| 125 | if (logFile) { |
| 126 | fprintf(logFile, "%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 127 | vfprintf(logFile, message, argptr); |
| 128 | fprintf(logFile, "\n"); |
| 129 | fflush(logFile); |
| 130 | } |
| 131 |
| 132 | va_end(argptr); |
| 133 | } |
| 134 |