Nagram/TMessagesProj/jni/tgnet/FileLog.cpp

212 lines
5.7 KiB
C++
Raw Permalink Normal View History

2015-09-24 20:52:02 +00:00
/*
2018-07-30 02:07:02 +00:00
* This is the source code of tgnet library v. 1.1
2015-09-24 20:52:02 +00:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2018-07-30 02:07:02 +00:00
* Copyright Nikolai Kudashov, 2015-2018.
2015-09-24 20:52:02 +00:00
*/
#include <stdio.h>
#include <stdarg.h>
2015-10-29 17:10:07 +00:00
#include <time.h>
2015-09-24 20:52:02 +00:00
#include "FileLog.h"
2018-07-30 02:07:02 +00:00
#include "ConnectionsManager.h"
2015-09-24 20:52:02 +00:00
#ifdef ANDROID
#include <android/log.h>
#endif
2019-01-23 17:03:33 +00:00
#ifdef DEBUG_VERSION
bool LOGS_ENABLED = true;
#else
bool LOGS_ENABLED = false;
#endif
2023-03-24 11:38:14 +00:00
bool REF_LOGS_ENABLED = false;
2018-07-30 02:07:02 +00:00
FileLog &FileLog::getInstance() {
static FileLog instance;
return instance;
}
FileLog::FileLog() {
pthread_mutex_init(&mutex, NULL);
}
2015-10-29 17:10:07 +00:00
void FileLog::init(std::string path) {
2018-07-30 02:07:02 +00:00
pthread_mutex_lock(&mutex);
if (path.size() > 0 && logFile == nullptr) {
2015-11-26 21:04:02 +00:00
logFile = fopen(path.c_str(), "w");
}
2018-07-30 02:07:02 +00:00
pthread_mutex_unlock(&mutex);
2015-10-29 17:10:07 +00:00
}
2022-09-16 18:48:21 +00:00
void FileLog::fatal(const char *message, ...) {
if (!LOGS_ENABLED) {
return;
}
va_list argptr;
va_start(argptr, message);
2023-07-20 20:15:36 +00:00
struct timeval time_now;
gettimeofday(&time_now, NULL);
struct tm *now = localtime(&time_now.tv_sec);
2022-09-16 18:48:21 +00:00
#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) {
2023-07-20 20:15:36 +00:00
fprintf(logFile, "%d-%d %02d:%02d:%02d.%03d FATAL ERROR: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, (int) (time_now.tv_usec / 1000));
2022-09-16 18:48:21 +00:00
vfprintf(logFile, message, argptr);
fprintf(logFile, "\n");
fflush(logFile);
}
va_end(argptr);
#ifdef DEBUG_VERSION
abort();
#endif
}
2015-09-24 20:52:02 +00:00
void FileLog::e(const char *message, ...) {
2019-01-23 17:03:33 +00:00
if (!LOGS_ENABLED) {
return;
}
2015-09-24 20:52:02 +00:00
va_list argptr;
va_start(argptr, message);
2023-07-20 20:15:36 +00:00
struct timeval time_now;
gettimeofday(&time_now, NULL);
struct tm *now = localtime(&time_now.tv_sec);
2015-09-24 20:52:02 +00:00
#ifdef ANDROID
__android_log_vprint(ANDROID_LOG_ERROR, "tgnet", message, argptr);
2018-07-30 02:07:02 +00:00
va_end(argptr);
va_start(argptr, message);
2015-09-24 20:52:02 +00:00
#else
2015-10-29 17:10:07 +00:00
printf("%d-%d %02d:%02d:%02d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
2015-09-24 20:52:02 +00:00
vprintf(message, argptr);
printf("\n");
fflush(stdout);
2018-07-30 02:07:02 +00:00
va_end(argptr);
va_start(argptr, message);
2015-09-24 20:52:02 +00:00
#endif
2018-07-30 02:07:02 +00:00
FILE *logFile = getInstance().logFile;
2015-10-29 17:10:07 +00:00
if (logFile) {
2023-07-20 20:15:36 +00:00
fprintf(logFile, "%d-%d %02d:%02d:%02d.%03d error: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, (int) (time_now.tv_usec / 1000));
2015-10-29 17:10:07 +00:00
vfprintf(logFile, message, argptr);
fprintf(logFile, "\n");
fflush(logFile);
}
2015-09-24 20:52:02 +00:00
va_end(argptr);
}
void FileLog::w(const char *message, ...) {
2019-01-23 17:03:33 +00:00
if (!LOGS_ENABLED) {
return;
}
2015-09-24 20:52:02 +00:00
va_list argptr;
va_start(argptr, message);
2023-07-20 20:15:36 +00:00
struct timeval time_now;
gettimeofday(&time_now, NULL);
struct tm *now = localtime(&time_now.tv_sec);
2015-09-24 20:52:02 +00:00
#ifdef ANDROID
__android_log_vprint(ANDROID_LOG_WARN, "tgnet", message, argptr);
2018-07-30 02:07:02 +00:00
va_end(argptr);
va_start(argptr, message);
2015-09-24 20:52:02 +00:00
#else
2015-10-29 17:10:07 +00:00
printf("%d-%d %02d:%02d:%02d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
2015-09-24 20:52:02 +00:00
vprintf(message, argptr);
printf("\n");
fflush(stdout);
2018-07-30 02:07:02 +00:00
va_end(argptr);
va_start(argptr, message);
2015-09-24 20:52:02 +00:00
#endif
2018-07-30 02:07:02 +00:00
FILE *logFile = getInstance().logFile;
2015-10-29 17:10:07 +00:00
if (logFile) {
2023-07-20 20:15:36 +00:00
fprintf(logFile, "%d-%d %02d:%02d:%02d.%03d warning: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, (int) (time_now.tv_usec / 1000));
2015-10-29 17:10:07 +00:00
vfprintf(logFile, message, argptr);
fprintf(logFile, "\n");
fflush(logFile);
}
2015-09-24 20:52:02 +00:00
va_end(argptr);
}
void FileLog::d(const char *message, ...) {
2019-01-23 17:03:33 +00:00
if (!LOGS_ENABLED) {
return;
}
2015-09-24 20:52:02 +00:00
va_list argptr;
va_start(argptr, message);
2023-07-20 20:15:36 +00:00
struct timeval time_now;
gettimeofday(&time_now, NULL);
struct tm *now = localtime(&time_now.tv_sec);
2015-09-24 20:52:02 +00:00
#ifdef ANDROID
__android_log_vprint(ANDROID_LOG_DEBUG, "tgnet", message, argptr);
2018-07-30 02:07:02 +00:00
va_end(argptr);
va_start(argptr, message);
2015-09-24 20:52:02 +00:00
#else
2015-10-29 17:10:07 +00:00
printf("%d-%d %02d:%02d:%02d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
2015-09-24 20:52:02 +00:00
vprintf(message, argptr);
printf("\n");
fflush(stdout);
2018-07-30 02:07:02 +00:00
va_end(argptr);
va_start(argptr, message);
2015-09-24 20:52:02 +00:00
#endif
2018-07-30 02:07:02 +00:00
FILE *logFile = getInstance().logFile;
2015-10-29 17:10:07 +00:00
if (logFile) {
2023-07-20 20:15:36 +00:00
fprintf(logFile, "%d-%d %02d:%02d:%02d.%03d debug: ", now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, (int) (time_now.tv_usec / 1000));
2015-10-29 17:10:07 +00:00
vfprintf(logFile, message, argptr);
fprintf(logFile, "\n");
fflush(logFile);
}
2015-09-24 20:52:02 +00:00
va_end(argptr);
}
2023-03-24 11:38:14 +00:00
static int refsCount = 0;
void FileLog::ref(const char *message, ...) {
if (!REF_LOGS_ENABLED) {
return;
}
va_list argptr;
va_start(argptr, message);
refsCount++;
#ifdef ANDROID
std::ostringstream s;
s << refsCount << " refs (+ref): " << message;
__android_log_vprint(ANDROID_LOG_VERBOSE, "tgnetREF", s.str().c_str(), argptr);
va_end(argptr);
va_start(argptr, message);
#endif
va_end(argptr);
}
void FileLog::delref(const char *message, ...) {
if (!REF_LOGS_ENABLED) {
return;
}
va_list argptr;
va_start(argptr, message);
refsCount--;
#ifdef ANDROID
std::ostringstream s;
s << refsCount << " refs (-ref): " << message;
__android_log_vprint(ANDROID_LOG_VERBOSE, "tgnetREF", s.str().c_str(), argptr);
va_end(argptr);
va_start(argptr, message);
#endif
va_end(argptr);
}