-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNAVFoundation.ErrorLogUtils.axi
217 lines (160 loc) · 7.33 KB
/
NAVFoundation.ErrorLogUtils.axi
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
206
207
208
209
210
211
212
213
214
215
216
217
PROGRAM_NAME='NAVFoundation.ErrorLogUtils'
/*
_ _ _ ___ __
| \ | | ___ _ __ __ _ __ _| |_ ___ / \ \ / /
| \| |/ _ \| '__/ _` |/ _` | __/ _ \ / _ \ \ / /
| |\ | (_) | | | (_| | (_| | || __// ___ \ V /
|_| \_|\___/|_| \__, |\__,_|\__\___/_/ \_\_/
|___/
MIT License
Copyright (c) 2023 Norgate AV Services Limited
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#IF_NOT_DEFINED __NAV_FOUNDATION_ERRORLOGUTILS__
#DEFINE __NAV_FOUNDATION_ERRORLOGUTILS__ 'NAVFoundation.ErrorLogUtils'
#include 'NAVFoundation.Core.axi'
#include 'NAVFoundation.DateTimeUtils.axi'
#include 'NAVFoundation.FileUtils.axi'
define_function char[NAV_MAX_CHARS] NAVGetLogLevel(long level) {
switch (level) {
case NAV_LOG_LEVEL_ERROR:
case NAV_LOG_LEVEL_WARNING:
case NAV_LOG_LEVEL_INFO:
case NAV_LOG_LEVEL_DEBUG: {
return NAV_LOG_LEVELS[level]
}
default: {
return NAVGetLogLevel(NAV_LOG_LEVEL_INFO)
}
}
}
define_function NAVLibraryFunctionErrorLog(long level, char libraryName[], char functionName[], char message[]) {
stack_var char log[NAV_MAX_BUFFER]
log = NAVFormatLibraryFunctionLog(libraryName, functionName, message)
NAVErrorLog(level, log)
}
define_function char[NAV_MAX_BUFFER] NAVFormatLibraryFunction(char libraryName[], char functionName[]) {
return "libraryName, '.', functionName, '()'"
}
define_function char[NAV_MAX_BUFFER] NAVFormatLibraryFunctionLog(char libraryName[], char functionName[], char value[]) {
return "NAVFormatLibraryFunction(libraryName, functionName), ' => ', __FILE__, ':: ', value"
}
define_function char[NAV_MAX_BUFFER] NAVFormatLog(long level, char value[]) {
return "NAVGetLogLevel(level), ':: ', NAVFormatHex(value)"
}
define_function NAVErrorLog(long level, char value[]) {
switch (level) {
case NAV_LOG_LEVEL_ERROR:
case NAV_LOG_LEVEL_WARNING:
case NAV_LOG_LEVEL_INFO:
case NAV_LOG_LEVEL_DEBUG: {
amx_log(level, NAVFormatLog(level, value))
#IF_DEFINED __NAV_FOUNDATION_DEBUGCONSOLE__
NAVDebugConsoleLog(NAVFormatLog(level, value))
#END_IF
#IF_DEFINED __NAV_FOUNDATION_LOGTOFILE__
NAVErrorLogToFile(NAV_LOG_FILE_NAME, level, value)
#END_IF
}
default: {
NAVErrorLog(NAV_LOG_LEVEL_ERROR, value)
}
}
}
define_function char[NAV_MAX_CHARS] NAVGetStandardLogMessageType(integer type) {
return NAV_STANDARD_LOG_MESSAGE_TYPE[type]
}
define_function char[NAV_MAX_BUFFER] NAVFormatStandardLogMessage(integer type, dev device, char message[]) {
return "NAVGetStandardLogMessageType(type), ' ', NAVStringSurroundWith(NAVDeviceToString(device), '[', ']'), '-', NAVStringSurroundWith(NAVFormatHex(message), '[', ']')"
}
define_function char[NAV_MAX_BUFFER] NAVFormatLogToFile(long level, char value[]) {
stack_var char timestamp[NAV_MAX_CHARS]
stack_var char date[10]
stack_var char time[8]
if (!length_array(value)) {
return ''
}
timestamp = NAVDateTimeGetTimestampNow()
date = left_string(timestamp, 10)
time = NAVStringSubstring(timestamp, 12, 8)
return "date, ' (', time, '):: ', NAVFormatLog(level, value)"
}
define_function slong NAVErrorLogToFile(char file[], long level, char value[]) {
stack_var char log[NAV_MAX_BUFFER]
stack_var slong result
stack_var long size
if (!length_array(value)) {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_ERRORLOGUTILS__,
'NAVErrorLogToFile',
"NAVGetFileError(NAV_FILE_ERROR_INVALID_PARAMETER), ' : No value provided to log'")
return NAV_FILE_ERROR_INVALID_PARAMETER
}
log = NAVFormatLogToFile(level, value)
if (!length_array(log)) {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_ERRORLOGUTILS__,
'NAVErrorLogToFile',
"NAVGetFileError(NAV_FILE_ERROR_INVALID_PARAMETER), ' : Unable to format log message'")
return NAV_FILE_ERROR_INVALID_PARAMETER
}
// Check if the logs directory exists, if not create it
if (!NAVDirectoryExists(NAV_LOGS_DIRECTORY)) {
result = NAVDirectoryCreate(NAV_LOGS_DIRECTORY)
if (result < 0) {
return result
}
}
// If the file does not exist, create it and write the message
if (!NAVFileExists(NAV_LOGS_DIRECTORY, file)) {
return NAVFileWriteLine("NAV_LOGS_DIRECTORY, '/', file", log)
}
result = NAVFileGetSize("NAV_LOGS_DIRECTORY, '/', file")
if (result < 0) {
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_ERROR,
__NAV_FOUNDATION_ERRORLOGUTILS__,
'NAVErrorLogToFile',
"NAVGetFileError(result), ' : Unable to get the size of the log file'")
return result
}
size = type_cast(result)
// Check the size to see if we first need to rotate the log file
if ((size + length_array(log)) > NAV_MAX_LOG_FILE_SIZE) {
NAVErrorLogFileRotate(file)
return NAVErrorLogToFile(file, level, value)
}
// Finally, append the current log message
return NAVFileAppendLine("NAV_LOGS_DIRECTORY, '/', file", log)
}
define_function slong NAVErrorLogFileRotate(char file[]) {
stack_var integer count
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_INFO,
__NAV_FOUNDATION_ERRORLOGUTILS__,
'NAVErrorLogFileRotate',
"'Rotating log file: ', file")
if (NAVFileExists(NAV_LOGS_DIRECTORY, "file, '.old.', itoa(NAV_MAX_OLD_LOG_FILES)")) {
NAVFileDelete("NAV_LOGS_DIRECTORY, '/', file, '.old.', itoa(NAV_MAX_OLD_LOG_FILES)")
}
for (count = (NAV_MAX_OLD_LOG_FILES - 1); count > 0; count--) {
if (!NAVFileExists(NAV_LOGS_DIRECTORY, "file, '.old.', itoa(count)")) {
continue
}
NAVFileRename("NAV_LOGS_DIRECTORY, '/', file, '.old.', itoa(count)", "NAV_LOGS_DIRECTORY, '/', file, '.old.', itoa(count + 1)")
}
return NAVFileRename("NAV_LOGS_DIRECTORY, '/', file", "NAV_LOGS_DIRECTORY, '/', file, '.old.1'")
}
#END_IF // __NAV_FOUNDATION_ERRORLOGUTILS__