-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog.h
96 lines (82 loc) · 3.18 KB
/
log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef LOG_H
#define LOG_H
#include <pthread.h>
#if 0
#include <android/log.h>
#include "singleton.hpp"
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
class FileLogger
{
public:
FileLogger()
{
//extern const char* g_data_file_path;
//char log_path[256] = {0};
//snprintf(log_path, sizeof(log_path), "%s/il2cpp.log", g_data_file_path);
const char* log_path = "/data/local/tmp/il2cpp.log";
__android_log_print(ANDROID_LOG_DEBUG , "il2cpp", "log to file:%s", log_path);
file_handle_ = ::fopen(log_path, "w");
if (file_handle_ == NULL){
__android_log_print(ANDROID_LOG_ERROR, "il2cpp", "log to file:%s, errno:%d", log_path, errno);
exit(-1);
}
};
virtual ~FileLogger()
{
if (file_handle_)
{
::fclose(file_handle_);
}
}
void log(const char* _fmt, ...)
{
if (file_handle_ == NULL) return;
char log_buf[2048] = {0};
va_list arg_list;
va_start(arg_list, _fmt);
int i = vsnprintf(log_buf, sizeof(log_buf), _fmt, arg_list);
va_end(arg_list);
if (i > 0)
{
fwrite(log_buf, sizeof(char), i, file_handle_ );
fflush( file_handle_ );
}
}
private:
FILE* file_handle_;
};
#define g_file_logger (Singleton<FileLogger, 0>::instance())
#define MY_VERBOSE(...)
#define MY_LOG(fmt,...) g_file_logger->log("LOG [%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#define MY_METHOD(fmt,...) g_file_logger->log("METHOD [%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#define MY_INFO(fmt, ...) g_file_logger->log("INFO [%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#define MY_ERROR(fmt,...) g_file_logger->log("ERROR [%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#elif 0
#include <android/log.h>
#define MY_VERBOSE(...)
#define MY_LOG(fmt,...) __android_log_print(ANDROID_LOG_DEBUG , "il2cpp", "[%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#define MY_METHOD(fmt,...) __android_log_print(ANDROID_LOG_INFO , "il2cpp", "[%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#define MY_INFO(fmt, ...) __android_log_print(ANDROID_LOG_INFO , "il2cpp", "[%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#define MY_ERROR(fmt,...) __android_log_print(ANDROID_LOG_ERROR , "il2cpp", "[%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#elif 0
#define MY_VERBOSE(fmt, ...)
#define MY_LOG(fmt, ...) printf( "[%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#define MY_METHOD(fmt, ...) printf( "[%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#define MY_ERROR(fmt, ...) printf( "[%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#elif __ANDROID__
#include <android/log.h>
#define MY_VERBOSE(fmt, ...)
#define MY_LOG(fmt, ...)
#define MY_METHOD(fmt, ...)
#define MY_INFO(fmt, ...) __android_log_print(ANDROID_LOG_INFO , "il2cpp", "[%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#define MY_ERROR(fmt, ...) __android_log_print(ANDROID_LOG_ERROR , "il2cpp", "[%zx]" fmt "\n", (size_t)pthread_self(), ##__VA_ARGS__)
#endif
#define ALOGD MY_LOG
#define ALOGW MY_ERROR
#define ALOGV MY_VERBOSE
#define ALOGE MY_ERROR
#define LOG_TAG "il2cpp"
#endif