Nagram/TMessagesProj/jni/tgnet/FileLog.cpp

92 lines
2.6 KiB
C++
Raw Normal View History

2015-09-24 20:52:02 +00:00
/*
* This is the source code of tgnet library v. 1.0
* 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.
*/
#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"
#ifdef ANDROID
#include <android/log.h>
#endif
2015-10-29 17:10:07 +00:00
FILE *logFile = nullptr;
void FileLog::init(std::string path) {
logFile = fopen(path.c_str(), "w");
}
2015-09-24 20:52:02 +00:00
void FileLog::e(const char *message, ...) {
va_list argptr;
va_start(argptr, message);
2015-10-29 17:10:07 +00:00
time_t t = time(0);
struct tm *now = localtime(&t);
2015-09-24 20:52:02 +00:00
#ifdef ANDROID
__android_log_vprint(ANDROID_LOG_ERROR, "tgnet", message, argptr);
#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);
#endif
2015-10-29 17:10:07 +00:00
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);
}
2015-09-24 20:52:02 +00:00
va_end(argptr);
}
void FileLog::w(const char *message, ...) {
va_list argptr;
va_start(argptr, message);
2015-10-29 17:10:07 +00:00
time_t t = time(0);
struct tm *now = localtime(&t);
2015-09-24 20:52:02 +00:00
#ifdef ANDROID
__android_log_vprint(ANDROID_LOG_WARN, "tgnet", message, argptr);
#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);
#endif
2015-10-29 17:10:07 +00:00
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);
}
2015-09-24 20:52:02 +00:00
va_end(argptr);
}
void FileLog::d(const char *message, ...) {
va_list argptr;
va_start(argptr, message);
2015-10-29 17:10:07 +00:00
time_t t = time(0);
struct tm *now = localtime(&t);
2015-09-24 20:52:02 +00:00
#ifdef ANDROID
__android_log_vprint(ANDROID_LOG_DEBUG, "tgnet", message, argptr);
#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);
#endif
2015-10-29 17:10:07 +00:00
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);
}
2015-09-24 20:52:02 +00:00
va_end(argptr);
}