forked from v-atamanenko/FalsoJNI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FalsoJNI_Logger.c
108 lines (90 loc) · 2.8 KB
/
FalsoJNI_Logger.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* FalsoJNI_Logger.c
*
* Fake Java Native Interface, providing JavaVM and JNIEnv objects.
*
* Copyright (C) 2022 Volodymyr Atamanenko
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdarg.h>
#include <pthread.h>
#include <malloc.h>
#include <string.h>
#include "FalsoJNI_Logger.h"
#include "FalsoJNI.h"
pthread_mutex_t * _fjni_log_mutex = NULL;
#define COLOR_RED "\x1B[31m"
#define COLOR_ORANGE "\x1B[33m"
#define COLOR_BLUE "\x1B[34m"
#define COLOR_END "\033[0m"
void _fjni_log_lock() {
if (_fjni_log_mutex == NULL) {
pthread_mutex_t initTmpNormal;
_fjni_log_mutex = malloc(sizeof(pthread_mutex_t));
memcpy(_fjni_log_mutex, &initTmpNormal, sizeof(pthread_mutex_t));
if (pthread_mutex_init(_fjni_log_mutex, NULL) != 0) {
_fjni_log_mutex = NULL;
return;
}
}
pthread_mutex_lock(_fjni_log_mutex);
}
void _fjni_log_unlock() {
if (_fjni_log_mutex) {
pthread_mutex_unlock(_fjni_log_mutex);
}
}
int _fjni_log_info(const char *fname, int lineno, const char *fxname, const char* fmt, ...) {
#if FALSOJNI_DEBUGLEVEL <= FALSOJNI_DEBUG_INFO
_fjni_log_lock();
char fmt_colored[2048];
snprintf(fmt_colored, 2047, "%s[INFO] %s%s\n", COLOR_BLUE, fmt, COLOR_END);
va_list list;
va_start(list, fmt);
vfprintf(stderr, fmt_colored, list);
va_end(list);
_fjni_log_unlock();
#endif
return 0;
}
int _fjni_log_warn(const char *fname, int lineno, const char *fxname, const char* fmt, ...) {
#if FALSOJNI_DEBUGLEVEL <= FALSOJNI_DEBUG_WARN
_fjni_log_lock();
char fmt_colored[2048];
snprintf(fmt_colored, 2047, "%s[WARNING][%s:%d][%s] %s%s\n", COLOR_ORANGE, fname, lineno, fxname, fmt, COLOR_END);
va_list list;
va_start(list, fmt);
vfprintf(stderr, fmt_colored, list);
va_end(list);
_fjni_log_unlock();
#endif
return 0;
}
int _fjni_log_debug(const char *fname, int lineno, const char *fxname, const char* fmt, ...) {
#if FALSOJNI_DEBUGLEVEL <= FALSOJNI_DEBUG_ALL
_fjni_log_lock();
char fmt_real[4096];
snprintf(fmt_real, 2047, "[DEBUG][%s:%d][%s] %s\n", fname, lineno, fxname, fmt);
va_list list;
va_start(list, fmt);
vfprintf(stderr, fmt_real, list);
va_end(list);
_fjni_log_unlock();
#endif
return 0;
}
int _fjni_log_error(const char *fname, int lineno, const char *fxname, const char* fmt, ...) {
#if FALSOJNI_DEBUGLEVEL <= FALSOJNI_DEBUG_ERROR
_fjni_log_lock();
char fmt_colored[2048];
snprintf(fmt_colored, 2047, "%s[ERROR][%s:%d][%s] %s%s\n", COLOR_RED, fname, lineno, fxname, fmt, COLOR_END);
va_list list;
va_start(list, fmt);
vfprintf(stderr, fmt_colored, list);
va_end(list);
_fjni_log_unlock();
#endif
return 0;
}