123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /*
- * This is the source code of tgnet library v. 1.1
- * It is licensed under GNU GPL v. 2 or later.
- * You should have received a copy of the license in this archive (see LICENSE).
- *
- * Copyright Nikolai Kudashov, 2015-2018.
- */
- #include <stdio.h>
- #include <stdarg.h>
- #include <time.h>
- #include "FileLog.h"
- #include "ConnectionsManager.h"
- #ifdef ANDROID
- #include <android/log.h>
- #endif
- #ifdef DEBUG_VERSION
- bool LOGS_ENABLED = true;
- #else
- bool LOGS_ENABLED = false;
- #endif
- FileLog &FileLog::getInstance() {
- static FileLog instance;
- return instance;
- }
- FileLog::FileLog() {
- pthread_mutex_init(&mutex, NULL);
- }
- void FileLog::init(std::string path) {
- pthread_mutex_lock(&mutex);
- if (path.size() > 0 && logFile == nullptr) {
- logFile = fopen(path.c_str(), "w");
- }
- pthread_mutex_unlock(&mutex);
- }
- void FileLog::fatal(const char *message, ...) {
- if (!LOGS_ENABLED) {
- return;
- }
- va_list argptr;
- va_start(argptr, message);
- time_t t = time(0);
- struct tm *now = localtime(&t);
- #ifdef ANDROID
- __android_log_vprint(ANDROID_LOG_FATAL, "tgnet", message, argptr);
- va_end(argptr);
- va_start(argptr, message);
- #else
- printf("%d-%d %02d:%02d:%02d FATAL ERROR: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
- vprintf(message, argptr);
- printf("\n");
- fflush(stdout);
- va_end(argptr);
- va_start(argptr, message);
- #endif
- FILE *logFile = getInstance().logFile;
- if (logFile) {
- 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);
- vfprintf(logFile, message, argptr);
- fprintf(logFile, "\n");
- fflush(logFile);
- }
- va_end(argptr);
- #ifdef DEBUG_VERSION
- abort();
- #endif
- }
- void FileLog::e(const char *message, ...) {
- if (!LOGS_ENABLED) {
- return;
- }
- va_list argptr;
- va_start(argptr, message);
- time_t t = time(0);
- struct tm *now = localtime(&t);
- #ifdef ANDROID
- __android_log_vprint(ANDROID_LOG_ERROR, "tgnet", message, argptr);
- va_end(argptr);
- va_start(argptr, message);
- #else
- printf("%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
- vprintf(message, argptr);
- printf("\n");
- fflush(stdout);
- va_end(argptr);
- va_start(argptr, message);
- #endif
- FILE *logFile = getInstance().logFile;
- if (logFile) {
- fprintf(logFile, "%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
- vfprintf(logFile, message, argptr);
- fprintf(logFile, "\n");
- fflush(logFile);
- }
-
- va_end(argptr);
- }
- void FileLog::w(const char *message, ...) {
- if (!LOGS_ENABLED) {
- return;
- }
- va_list argptr;
- va_start(argptr, message);
- time_t t = time(0);
- struct tm *now = localtime(&t);
- #ifdef ANDROID
- __android_log_vprint(ANDROID_LOG_WARN, "tgnet", message, argptr);
- va_end(argptr);
- va_start(argptr, message);
- #else
- printf("%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
- vprintf(message, argptr);
- printf("\n");
- fflush(stdout);
- va_end(argptr);
- va_start(argptr, message);
- #endif
- FILE *logFile = getInstance().logFile;
- if (logFile) {
- fprintf(logFile, "%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
- vfprintf(logFile, message, argptr);
- fprintf(logFile, "\n");
- fflush(logFile);
- }
-
- va_end(argptr);
- }
- void FileLog::d(const char *message, ...) {
- if (!LOGS_ENABLED) {
- return;
- }
- va_list argptr;
- va_start(argptr, message);
- time_t t = time(0);
- struct tm *now = localtime(&t);
- #ifdef ANDROID
- __android_log_vprint(ANDROID_LOG_DEBUG, "tgnet", message, argptr);
- va_end(argptr);
- va_start(argptr, message);
- #else
- printf("%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
- vprintf(message, argptr);
- printf("\n");
- fflush(stdout);
- va_end(argptr);
- va_start(argptr, message);
- #endif
- FILE *logFile = getInstance().logFile;
- if (logFile) {
- fprintf(logFile, "%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
- vfprintf(logFile, message, argptr);
- fprintf(logFile, "\n");
- fflush(logFile);
- }
-
- va_end(argptr);
- }
|