-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyUtility-Win.hpp
175 lines (145 loc) · 3.68 KB
/
MyUtility-Win.hpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* MyUtility functions for Windows
* ver 0.99
*/
//boolean type/////////////////////////////////////////////
#ifndef __cplusplus
#define false 0
#define true 1
typedef int bool;
#endif
//Random number generation/////////////////////////////////
#include <stdlib.h>
#define frand() ((double) rand() / (RAND_MAX+1.0))
//Standard functions for logging///////////////////////////
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
FILE *logfile;
char messages_savepath[128];
bool Save2LogCat = true;
bool Save2File = true;
bool Save2TV = true;
void LOGOPEN(char* savepath)
{
if(Save2File){
strcpy(messages_savepath, savepath);
strcat(messages_savepath, "/messagelog.txt");
logfile=fopen(messages_savepath, "a+"); //"a+":=append,read&write
if(logfile==NULL)
printf("LOGOPEN: Message Logger Could not Open .txt file\n");
}
}
void LOGDUMP()
{
if(Save2File)
fflush(logfile);//fflush needed before closing for the buffer to be written to file
}
void LOGCLOSE()
{
if(Save2File)
fclose(logfile);
}
void LOGI(const char* mTAG, const char *format, ...)
{
char logstr[400]="";
va_list arg_list;
va_start(arg_list, format);
vsprintf(logstr, format, arg_list);
va_end(arg_list);
if(Save2LogCat){
printf("%s", mTAG);
printf(logstr);
}
if(Save2File){
fprintf(logfile, "%s", mTAG);
fprintf(logfile, logstr);
}
}
void LOGW(const char* mTAG, const char *format, ...)
{
char logstr[400]="";
va_list arg_list;
va_start(arg_list, format);
vsprintf(logstr, format, arg_list);
va_end(arg_list);
if(Save2LogCat){
printf("%sW:", mTAG);
printf(logstr);
}
if(Save2File){
fprintf(logfile, "%sW:", mTAG);
fprintf(logfile, logstr);
}
}
void LOGE(const char* mTAG, const char *format, ...)
{
char logstr[400]="";
va_list arg_list;
va_start(arg_list, format);
vsprintf(logstr, format, arg_list);
va_end(arg_list);
if(Save2LogCat){
printf("%sE:", mTAG);
printf(logstr);
}
if(Save2File){
fprintf(logfile, "%sE:", mTAG);
fprintf(logfile, logstr);
LOGDUMP();//Emergency Dump
}
}
#ifdef __cplusplus
}
#endif
//LOCKS////////////////////////////////////////////////////
#define _LOCKC(Key,err)\
if ((*env)->MonitorEnter(env, Key) != JNI_OK) {\
LOGW(TAG, " MonitorEnter != JNI_OK\n");\
return err;}
#define _UNLOCKC(Key,err)\
if ((*env)->MonitorExit(env, Key) != JNI_OK) {\
LOGW(TAG, " MonitorExit != JNI_OK\n");\
return err;}
#define _LOCKCPP(Key,err)\
if ((env)->MonitorEnter(Key) != JNI_OK) {\
LOGW(TAG, " MonitorEnter != JNI_OK\n");\
return err;}
#define _UNLOCKCPP(Key,err)\
if ((env)->MonitorExit(Key) != JNI_OK) {\
LOGW(TAG, " MonitorExit != JNI_OK\n");\
return err;}
//Time/////////////////////////////////////////////////////
#include <time.h>
#include <math.h>
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
#define CLOCK_PROCESS_CPUTIME_ID 2
#define CLOCK_THREAD_CPUTIME_ID 3
#ifdef __cplusplus
extern "C" {
#endif
/*double _biasTime = 0;
void mysetTime(double currentTime)
{
_biasTime = (currentTime) - ((double)clock()/CLOCKS_PER_SEC);
}
double myTime()
{
return ((double)clock()/CLOCKS_PER_SEC) + _biasTime;
}*/
long _biasTime = 0;//in clock cycles
void mysetTime(double currentTime)
{
_biasTime = (currentTime * CLOCKS_PER_SEC - (long)clock()) / (double)CLOCKS_PER_SEC;
}
double myTime()
{
return (((long)clock() + _biasTime) / (double)CLOCKS_PER_SEC);
}
#ifdef __cplusplus
}
#endif