-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalAPI-Logging-Flatfile.sp
More file actions
205 lines (149 loc) · 6.33 KB
/
Copy pathGlobalAPI-Logging-Flatfile.sp
File metadata and controls
205 lines (149 loc) · 6.33 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// ====================== DEFINITIONS ======================== //
#define LOGS_PATH "logs/GlobalAPI"
#define FAILEDLOG_NAME "GlobalAPI-failed"
#define STARTEDLOG_NAME "GlobalAPI-started"
#define FINISHEDLOG_NAME "GlobalAPI-finished"
// =========================================================== //
#include <GlobalAPI>
#include <GlobalAPI/stocks>
// ====================== FORMATTING ========================= //
#pragma dynamic 131072
#pragma newdecls required
// ====================== VARIABLES ========================== //
// Paths
char gC_HTTPLogs_Directory[PLATFORM_MAX_PATH];
char gC_HTTPFailed_LogFile[PLATFORM_MAX_PATH];
char gC_HTTPStarted_LogFile[PLATFORM_MAX_PATH];
char gC_HTTPFinished_LogFile[PLATFORM_MAX_PATH];
// Phrases
char gC_HTTPMethodPhrases[][] = { "GET", "POST" };
// ======================= ENUMS ============================= //
enum BuildLogType
{
BuildLog_Failed,
BuildLog_Started,
BuildLog_Finished
}
// ====================== PLUGIN INFO ======================== //
public Plugin myinfo =
{
name = "GlobalAPI-Logging-Flatfile",
author = "The KZ Global Team",
description = "Logging for GlobalAPI in Flatfile format",
version = GlobalAPI_Plugin_Version,
url = GlobalAPI_Plugin_Url
};
// ======================= MAIN CODE ========================= //
public void OnPluginStart()
{
BuildPath(Path_SM, gC_HTTPLogs_Directory, sizeof(gC_HTTPLogs_Directory), "%s", LOGS_PATH);
if (!TryCreateDirectory(gC_HTTPLogs_Directory))
{
SetFailState("Failed to create directory %s", gC_HTTPLogs_Directory);
}
}
// =========================================================== //
public void GlobalAPI_OnRequestFailed(Handle request, GlobalAPIRequestData hData)
{
BuildDateToLogs(BuildLog_Failed);
char params[GlobalAPI_Max_QueryParams_Length] = "-";
hData.ToString(params, sizeof(params));
char url[GlobalAPI_Max_BaseUrl_Length];
hData.GetString("url", url, sizeof(url));
char pluginName[GlobalAPI_Max_PluginName_Length];
hData.GetString("pluginName", pluginName, sizeof(pluginName));
char method[5];
FormatEx(method, sizeof(method), gC_HTTPMethodPhrases[hData.RequestType]);
char logTag[128];
FormatTime(logTag, sizeof(logTag), "[GlobalAPI-Logging] %m/%d/%Y - %H:%M:%S");
File hLogFile = OpenFile(gC_HTTPFailed_LogFile, "a+");
hLogFile.WriteLine("%s (%s) API call failed!", logTag, pluginName);
hLogFile.WriteLine("%s => Method: %s", logTag, method);
hLogFile.WriteLine("%s => Status: %d", logTag, hData.Status);
hLogFile.WriteLine("%s => URL: %s", logTag, url);
if (hData.RequestType == GlobalAPIRequestType_GET)
{
hLogFile.WriteLine("%s => Params: %s", logTag, params);
}
else if (hData.RequestType == GlobalAPIRequestType_POST)
{
hData.Encode(params, sizeof(params));
hLogFile.WriteLine("%s => Body: %s", logTag, params);
}
delete hLogFile;
}
// =========================================================== //
public void GlobalAPI_OnRequestStarted(Handle request, GlobalAPIRequestData hData)
{
BuildDateToLogs(BuildLog_Started);
char params[GlobalAPI_Max_QueryParams_Length] = "-";
hData.ToString(params, sizeof(params));
char url[GlobalAPI_Max_BaseUrl_Length];
hData.GetString("url", url, sizeof(url));
char pluginName[GlobalAPI_Max_PluginName_Length];
hData.GetString("pluginName", pluginName, sizeof(pluginName));
char method[5];
FormatEx(method, sizeof(method), gC_HTTPMethodPhrases[hData.RequestType]);
char logTag[128];
FormatTime(logTag, sizeof(logTag), "[GlobalAPI-Logging] %m/%d/%Y - %H:%M:%S");
File hLogFile = OpenFile(gC_HTTPStarted_LogFile, "a+");
hLogFile.WriteLine("%s (%s) API call started!", logTag, pluginName);
hLogFile.WriteLine("%s => Method: %s", logTag, method);
hLogFile.WriteLine("%s => URL: %s", logTag, url);
if (hData.RequestType == GlobalAPIRequestType_GET)
{
hLogFile.WriteLine("%s => Params: %s", logTag, params);
}
else if (hData.RequestType == GlobalAPIRequestType_POST)
{
hData.Encode(params, sizeof(params));
hLogFile.WriteLine("%s => Body: %s", logTag, params);
}
delete hLogFile;
}
// =========================================================== //
public void GlobalAPI_OnRequestFinished(Handle request, GlobalAPIRequestData hData)
{
BuildDateToLogs(BuildLog_Finished);
char params[GlobalAPI_Max_QueryParams_Length] = "-";
hData.ToString(params, sizeof(params));
char url[GlobalAPI_Max_BaseUrl_Length];
hData.GetString("url", url, sizeof(url));
char pluginName[GlobalAPI_Max_PluginName_Length];
hData.GetString("pluginName", pluginName, sizeof(pluginName));
char method[5];
FormatEx(method, sizeof(method), gC_HTTPMethodPhrases[hData.RequestType]);
char logTag[128];
FormatTime(logTag, sizeof(logTag), "[GlobalAPI-Logging] %m/%d/%Y - %H:%M:%S");
File hLogFile = OpenFile(gC_HTTPFinished_LogFile, "a+");
hLogFile.WriteLine("%s (%s) API call finished!", logTag, pluginName);
hLogFile.WriteLine("%s => Method: %s", logTag, method);
hLogFile.WriteLine("%s => Status: %d", logTag, hData.Status);
hLogFile.WriteLine("%s => Failure: %s", logTag, hData.Failure ? "YES" : "NO");
hLogFile.WriteLine("%s => URL: %s", logTag, url);
if (hData.RequestType == GlobalAPIRequestType_GET)
{
hLogFile.WriteLine("%s => Params: %s", logTag, params);
}
else if (hData.RequestType == GlobalAPIRequestType_POST)
{
hData.Encode(params, sizeof(params));
hLogFile.WriteLine("%s => Body: %s", logTag, params);
}
delete hLogFile;
}
// =========================================================== //
public void BuildDateToLogs(BuildLogType type)
{
char date[64];
FormatTime(date, sizeof(date), "%Y%m%d");
switch(type)
{
case BuildLog_Failed:
Format(gC_HTTPFailed_LogFile, sizeof(gC_HTTPFailed_LogFile), "%s/%s_%s.log", gC_HTTPLogs_Directory, FAILEDLOG_NAME, date);
case BuildLog_Started:
Format(gC_HTTPStarted_LogFile, sizeof(gC_HTTPStarted_LogFile), "%s/%s_%s.log", gC_HTTPLogs_Directory, STARTEDLOG_NAME, date);
case BuildLog_Finished:
Format(gC_HTTPFinished_LogFile, sizeof(gC_HTTPFinished_LogFile), "%s/%s_%s.log", gC_HTTPLogs_Directory, FINISHEDLOG_NAME, date);
}
}