| 6 |
| * Copyright Nikolai Kudashov, 2015. |
| 7 | */ |
| 8 |
| 9 | #include <stdio.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <time.h> |
| 12 | #include "FileLog.h" |
| 13 |
| 14 | #ifdef ANDROID |
| 15 | #include <android/log.h> |
| 16 | #endif |
| 17 |
| 18 | FILE *logFile = nullptr; |
| 19 |
| 20 | void FileLog::init(std::string path) { |
| 21 | if (path.size() > 0) { |
| 22 | logFile = fopen(path.c_str(), "w"); |
| 23 | } |
| 24 | } |
| 25 |
| 26 | void FileLog::e(const char *message, ...) { |
| 27 | va_list argptr; |
| 28 | va_start(argptr, message); |
| 29 | time_t t = time(0); |
| 30 | struct tm *now = localtime(&t); |
| 31 | #ifdef ANDROID |
| 32 | __android_log_vprint(ANDROID_LOG_ERROR, "tgnet", message, argptr); |
| 33 | #else |
| 34 | printf("%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 35 | vprintf(message, argptr); |
| 36 | printf("\n"); |
| 37 | fflush(stdout); |
| 38 | #endif |
| 39 | if (logFile) { |
| 40 | fprintf(logFile, "%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 41 | vfprintf(logFile, message, argptr); |
| 42 | fprintf(logFile, "\n"); |
| 43 | fflush(logFile); |
| 44 | } |
| 45 |
| 46 | va_end(argptr); |
| 47 | } |
| 48 |
| 49 | void FileLog::w(const char *message, ...) { |
| 50 | va_list argptr; |
| 51 | va_start(argptr, message); |
| 52 | time_t t = time(0); |
| 53 | struct tm *now = localtime(&t); |
| 54 | #ifdef ANDROID |
| 55 | __android_log_vprint(ANDROID_LOG_WARN, "tgnet", message, argptr); |
| 56 | #else |
| 57 | printf("%d-%d %02d:%02d:%02d warning: ", 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 | #endif |
| 62 | if (logFile) { |
| 63 | fprintf(logFile, "%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 64 | vfprintf(logFile, message, argptr); |
| 65 | fprintf(logFile, "\n"); |
| 66 | fflush(logFile); |
| 67 | } |
| 68 |
| 69 | va_end(argptr); |
| 70 | } |
| 71 |
| 72 | void FileLog::d(const char *message, ...) { |
| 73 | va_list argptr; |
| 74 | va_start(argptr, message); |
| 75 | time_t t = time(0); |
| 76 | struct tm *now = localtime(&t); |
| 77 | #ifdef ANDROID |
| 78 | __android_log_vprint(ANDROID_LOG_DEBUG, "tgnet", message, argptr); |
| 79 | #else |
| 80 | printf("%d-%d %02d:%02d:%02d debug: ", 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 | #endif |
| 85 | if (logFile) { |
| 86 | fprintf(logFile, "%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); |
| 87 | vfprintf(logFile, message, argptr); |
| 88 | fprintf(logFile, "\n"); |
| 89 | fflush(logFile); |
| 90 | } |
| 91 |
| 92 | va_end(argptr); |
| 93 | } |
| 94 |