Last active
September 17, 2023 07:50
-
-
Save DGh0st/bead62400b8d15d065894dd27e5876ee to your computer and use it in GitHub Desktop.
Log to a custom file, can be used to get logs from users with a debug build.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FILELOGGING | |
#define FILELOGGING | |
#ifndef DEBUG | |
#define FLOG(args...) | |
#else | |
#define kLOGFILEPATH @"/path/to/log/file.log" | |
void _FLog(const char *functionName, int lineNumber, NSString *msgFormat, ...) { | |
va_list ap; | |
va_start(ap, msgFormat); | |
NSString *logFormattedMessage = [[NSString alloc] initWithFormat:msgFormat arguments:ap]; | |
va_end(ap); | |
NSString *logMessage = [NSString stringWithFormat:@"%s[%d] %@\n", functionName, lineNumber, logFormattedMessage]; | |
#if !__has_feature(objc_arc) | |
[logFormattedMessage release]; | |
#endif | |
if (![[NSFileManager defaultManager] fileExistsAtPath:kLOGFILEPATH]) | |
[[NSData data] writeToFile:kLOGFILEPATH atomically:YES]; | |
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:kLOGFILEPATH]; | |
[handle truncateFileAtOffset:[handle seekToEndOfFile]]; | |
[handle writeData:[logMessage dataUsingEncoding:NSUTF8StringEncoding]]; | |
[handle closeFile]; | |
} | |
#define FLOG(args...) _FLog(__func__, __LINE__, args); | |
#endif // DEBUG | |
#endif // FILELOGGING |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment