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
|
#include <revolution/os.h>
#include <revolution/fs.h>
#include <revolution/nand.h>
#include <cstring>
#include <cstdio>
static IOSFd s_fd = -255;
static IOSError s_err = ISFS_ERROR_UNKNOWN;
static int s_stage;
static char s_message[256] ATTRIBUTE_ALIGN(64);
static NANDLoggingCallback s_callback = 0;
static void asyncRoutine(ISFSError, void*);
static void prepareLine(char line[256], int, const char*);
BOOL reserveFileDescriptor(void) {
BOOL enabled = FALSE;
BOOL busy_flag = FALSE;
enabled = OSDisableInterrupts();
if (s_fd == -255) {
s_fd = -254;
busy_flag = FALSE;
} else if (s_fd == -254) {
busy_flag = TRUE;
} else if (s_fd >= 0) {
busy_flag = TRUE;
} else {
busy_flag = TRUE;
}
OSRestoreInterrupts(enabled);
return busy_flag ? FALSE : TRUE;
}
BOOL NANDLoggingAddMessageAsync(NANDLoggingCallback cb, s32 errorCode, const char* fmt, ...) {
va_list ap;
ISFSError err = ISFS_ERROR_UNKNOWN;
if (!reserveFileDescriptor()) {
return FALSE;
}
va_start(ap, fmt);
vsnprintf(s_message, 256, fmt, ap);
va_end(ap);
s_callback = cb;
s_stage = 1;
if (errorCode == ISFS_ERROR_UNKNOWN || errorCode == IOS_ERROR_UNKNOWN) {
s_err = errorCode;
}
err = ISFS_OpenAsync((const u8*)"/shared2/test2/nanderr.log", 3, asyncRoutine, 0);
if (err == ISFS_ERROR_OK) {
return TRUE;
} else {
return FALSE;
}
}
static void callbackRoutine(BOOL result) {
if (s_callback) {
s_callback(result, s_err);
}
}
static void asyncRoutine(ISFSError result, void *ctxt) {
ISFSError ret = ISFS_ERROR_UNKNOWN;
static char s_rBuf[256] ATTRIBUTE_ALIGN(64);
static char s_wBuf[256] ATTRIBUTE_ALIGN(64);
++s_stage;
if (s_stage == 2) {
if (result >= 0) {
s_fd = result;
ret = ISFS_SeekAsync(s_fd, 0, 0, asyncRoutine, 0);
if (ret != ISFS_ERROR_OK) {
callbackRoutine(FALSE);
}
} else {
callbackRoutine(FALSE);
}
} else if (s_stage == 3) {
if (result == 0) {
ret = ISFS_ReadAsync(s_fd, (u8*)s_rBuf, 256, asyncRoutine, 0);
if (ret != ISFS_ERROR_OK) {
callbackRoutine(FALSE);
}
} else {
callbackRoutine(FALSE);
}
} else if (s_stage == 4) {
if (result == 256) {
ret = ISFS_SeekAsync(s_fd, 0, 0, asyncRoutine, 0);
if (ret != ISFS_ERROR_OK) {
callbackRoutine(FALSE);
}
} else {
callbackRoutine(FALSE);
}
} else if (s_stage == 5) {
if (result == 0) {
int n = 0;
s_rBuf[255] = '\0';
n = atoi(s_rBuf);
prepareLine(s_wBuf, n, s_message);
ret = ISFS_WriteAsync(s_fd, (const u8*)s_wBuf, 256, asyncRoutine, 0);
if (ret != ISFS_ERROR_OK) {
callbackRoutine(FALSE);
}
} else {
callbackRoutine(FALSE);
}
} else if (s_stage == 6) {
if (result == 256) {
int n = atoi(s_rBuf);
ret = ISFS_SeekAsync(s_fd, n * 256, 0, asyncRoutine, 0);
if (ret != ISFS_ERROR_OK) {
callbackRoutine(FALSE);
}
} else {
callbackRoutine(FALSE);
}
} else if (s_stage == 7) {
int n = atoi(s_rBuf);
if (result == n * 256) {
ret = ISFS_WriteAsync(s_fd, (const u8*)s_wBuf, 256, asyncRoutine, 0);
if (ret != ISFS_ERROR_OK) {
callbackRoutine(FALSE);
}
} else {
callbackRoutine(FALSE);
}
} else if (s_stage == 8) {
if (result == 256) {
ret = ISFS_CloseAsync(s_fd, asyncRoutine, 0);
if (ret != ISFS_ERROR_OK) {
callbackRoutine(FALSE);
}
} else {
callbackRoutine(FALSE);
}
} else if (s_stage == 9) {
if (result == ISFS_ERROR_OK) {
s_fd = -255;
callbackRoutine(TRUE);
} else {
callbackRoutine(FALSE);
}
}
}
static void prepareLine(char line[256], int n, const char* msg) {
char titleID[64];
int end = 0;
struct OSCalendarTime cal;
memset(line, ' ', 254);
OSTicksToCalendarTime(OSGetTime(), &cal);
strncpy(titleID, nandGetHomeDir() + 7, 8 + 1 + 8);
titleID[8] = '-';
titleID[8 + 1 + 8] = '\0';
end = snprintf(line, 256, "%d %04d/%02d/%02d %02d:%02d:%02d %s %s", n % (64 - 1) + 1, cal.year, cal.mon + 1, cal.mday, cal.hour, cal.min, cal.sec, titleID, msg);
if (end < 256) {
line[end] = ' ';
}
line[254] = '\r';
line[255] = '\n';
}
|