Skip to content

Instantly share code, notes, and snippets.

@CreatureSurvive
Forked from DGh0st/FileLogging.h
Created May 19, 2019 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CreatureSurvive/dc07c4d191a2d503f626554f12ed0da1 to your computer and use it in GitHub Desktop.
Save CreatureSurvive/dc07c4d191a2d503f626554f12ed0da1 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.
#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];
[logFormattedMessage release];
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