1 |
efrain |
1 |
// Copyright 2019 Google
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// You may obtain a copy of the License at
|
|
|
6 |
//
|
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
//
|
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
|
|
|
15 |
#include <dispatch/dispatch.h>
|
|
|
16 |
|
|
|
17 |
#include "Crashlytics/Crashlytics/Helpers/FIRCLSInternalLogging.h"
|
|
|
18 |
#include "Crashlytics/Crashlytics/Components/FIRCLSContext.h"
|
|
|
19 |
#include "Crashlytics/Crashlytics/Components/FIRCLSGlobals.h"
|
|
|
20 |
#include "Crashlytics/Crashlytics/Helpers/FIRCLSUtility.h"
|
|
|
21 |
|
|
|
22 |
void FIRCLSSDKFileLog(FIRCLSInternalLogLevel level, const char* format, ...) {
|
|
|
23 |
if (!_firclsContext.readonly || !_firclsContext.writable) {
|
|
|
24 |
return;
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
const char* path = _firclsContext.readonly->logPath;
|
|
|
28 |
if (!FIRCLSIsValidPointer(path)) {
|
|
|
29 |
return;
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
if (_firclsContext.writable->internalLogging.logLevel > level) {
|
|
|
33 |
return;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
static dispatch_once_t onceToken;
|
|
|
37 |
dispatch_once(&onceToken, ^{
|
|
|
38 |
if (_firclsContext.writable->internalLogging.logFd == -1) {
|
|
|
39 |
_firclsContext.writable->internalLogging.logFd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
|
|
|
40 |
}
|
|
|
41 |
});
|
|
|
42 |
|
|
|
43 |
const int fd = _firclsContext.writable->internalLogging.logFd;
|
|
|
44 |
if (fd < 0) {
|
|
|
45 |
return;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
va_list args;
|
|
|
49 |
va_start(args, format);
|
|
|
50 |
|
|
|
51 |
#if DEBUG && 0
|
|
|
52 |
// It's nice to use printf here, so all the formatting works. However, its possible to hit a
|
|
|
53 |
// deadlock if you call vfprintf in a crash handler. So, this code is handy to keep, just in case,
|
|
|
54 |
// if there's a really tough thing to debug.
|
|
|
55 |
FILE* file = fopen(path, "a+");
|
|
|
56 |
vfprintf(file, format, args);
|
|
|
57 |
fclose(file);
|
|
|
58 |
#else
|
|
|
59 |
size_t formatLength = strlen(format);
|
|
|
60 |
for (size_t idx = 0; idx < formatLength; ++idx) {
|
|
|
61 |
if (format[idx] != '%') {
|
|
|
62 |
write(fd, &format[idx], 1);
|
|
|
63 |
continue;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
idx++; // move to the format char
|
|
|
67 |
switch (format[idx]) {
|
|
|
68 |
case 'd': {
|
|
|
69 |
int value = va_arg(args, int);
|
|
|
70 |
FIRCLSFileFDWriteInt64(fd, value);
|
|
|
71 |
} break;
|
|
|
72 |
case 'u': {
|
|
|
73 |
uint32_t value = va_arg(args, uint32_t);
|
|
|
74 |
FIRCLSFileFDWriteUInt64(fd, value, false);
|
|
|
75 |
} break;
|
|
|
76 |
case 'p': {
|
|
|
77 |
uintptr_t value = va_arg(args, uintptr_t);
|
|
|
78 |
write(fd, "0x", 2);
|
|
|
79 |
FIRCLSFileFDWriteUInt64(fd, value, true);
|
|
|
80 |
} break;
|
|
|
81 |
case 's': {
|
|
|
82 |
const char* string = va_arg(args, const char*);
|
|
|
83 |
if (!string) {
|
|
|
84 |
string = "(null)";
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
write(fd, string, strlen(string));
|
|
|
88 |
} break;
|
|
|
89 |
case 'x': {
|
|
|
90 |
unsigned int value = va_arg(args, unsigned int);
|
|
|
91 |
FIRCLSFileFDWriteUInt64(fd, value, true);
|
|
|
92 |
} break;
|
|
|
93 |
default:
|
|
|
94 |
// unhandled, back up to write out the percent + the format char
|
|
|
95 |
write(fd, &format[idx - 1], 2);
|
|
|
96 |
break;
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
#endif
|
|
|
100 |
va_end(args);
|
|
|
101 |
}
|