-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustom.c
More file actions
119 lines (102 loc) · 3.93 KB
/
custom.c
File metadata and controls
119 lines (102 loc) · 3.93 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
/*******************************************************************************
* File: custom.c
* Created: 2020-10-29
*
* Authors:
* Tyler Matijevich
*
* License:
* This file custom.c is part of the UserLog project
* released under the MIT license agreement.
******************************************************************************/
#include "Main.h"
/* Write to any user logbook synchronously */
ArEventLogRecordIDType UserLogCustom(char *Logbook, int32_t Severity,
uint16_t Facility, uint16_t Code,
ArEventLogRecordIDType Origin,
char *Object, char *Message, UserLogFormatType *Values)
{
/* Saturate severity */
Severity = MIN(MAX(USERLOG_SEVERITY_DEBUG, Severity),
USERLOG_SEVERITY_CRITICAL);
/* Suppress message */
if (Severity < severity_level) return 0;
/* Get logbook identifier */
ArEventLogGetIdent_typ get_ident = {{0}};
IecStringCopy(get_ident.Name, sizeof(get_ident.Name), Logbook);
get_ident.Execute = true;
ArEventLogGetIdent(&get_ident);
/* Check for error */
static uint8_t error;
UserLogFormatType log_values = {{0}};
if (get_ident.StatusID)
{
/* Block infinite recursion */
if (error) return 0;
/* Log error */
log_values.i[0] = get_ident.StatusID;
IecStringCopy(log_values.s[0], sizeof(log_values.s[0]), Logbook);
log_values.i[1] = Code;
UserLogCustom(LOGBOOK_USER_NAME, USERLOG_SEVERITY_ERROR,
FACILITY_ERROR, CODE_ERROR_IDENT, 0, NULL,
"ArEventLog error %i writing to logbook \"%s\" "
"with code %i using UserLog", &log_values);
error = true;
return 0;
}
/* Write to identified logbook */
ArEventLogWrite_typ write = {0};
write.Ident = get_ident.Ident;
/* Event ID */
write.EventID = ArEventLogMakeEventID(severity_map[Severity + 1],
Facility, Code);
/* Origin record */
write.OriginRecordID = Origin;
/* Additional string data */
char data[DATA_MESSAGE_SIZE] = {0};
IecStringFormat(data, sizeof(data), Message, Values);
write.AddDataSize = strlen(data) + 1;
write.AddDataFormat = arEVENTLOG_ADDFORMAT_TEXT;
write.AddData = (uint32_t)data;
/* Object name */
if (!Object || !*Object)
ST_name(0, write.ObjectID, 0);
else
IecStringCopy(write.ObjectID, sizeof(write.ObjectID), Object);
/* Write to logbook */
write.Execute = true;
ArEventLogWrite(&write);
/* Check for error */
if (write.StatusID)
{
/* Block infinite recursion */
if (error) return 0;
/* Log error */
log_values.i[0] = write.StatusID;
IecStringCopy(log_values.s[0], sizeof(log_values.s[0]), Logbook);
log_values.i[1] = Code;
UserLogCustom(LOGBOOK_USER_NAME, USERLOG_SEVERITY_ERROR,
FACILITY_ERROR, CODE_ERROR_WRITE, 0, NULL,
"ArEventLog error %i writing to logbook \"%s\" "
"with code %i using UserLog", &log_values);
error = true;
return 0;
}
/* No error, share record */
error = false;
return write.RecordID;
}
/* Write to the User logbook synchronously */
ArEventLogRecordIDType UserLogBasic(int32_t Severity, uint16_t Code,
char *Message)
{
return UserLogCustom(LOGBOOK_USER_NAME, Severity, USERLOG_FACILITY, Code,
0, NULL, Message, NULL);
}
/* Write to the User logbook with runtime data */
ArEventLogRecordIDType UserLogAdvanced(int32_t Severity, uint16_t Code, char
*Message, UserLogFormatType *Values)
{
return UserLogCustom(LOGBOOK_USER_NAME, Severity, USERLOG_FACILITY, Code,
0, NULL, Message, Values);
}