Last active
August 29, 2015 14:20
-
-
Save elpsk/9b72ffaffec947546c6b to your computer and use it in GitHub Desktop.
APLogger - Write console log to disk
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
// | |
// APLogger.h | |
// Copyright (c) 2015 Alberto Pasca. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface APLogger : NSObject | |
void APLog ( NSString* format, ... ); | |
void APMemLog ( NSString *func, int line ); | |
@end |
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
// | |
// APLogger.m | |
// Copyright (c) 2015 Alberto Pasca. All rights reserved. | |
// | |
#import "APLogger.h" | |
@implementation APLogger | |
void APLog (NSString* format, ...) | |
{ | |
va_list argList; | |
va_start(argList, format); | |
NSString *formattedMessage = [[NSString alloc] initWithFormat:format arguments: argList]; | |
va_end(argList); | |
NSLog(@"[UI:%d] - %@", [NSThread isMainThread], formattedMessage); | |
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES ); | |
NSString *docPath = [paths firstObject]; | |
NSString *logFile = [NSString stringWithFormat:@"%@/console.log", docPath]; | |
formattedMessage = [NSString stringWithFormat:@"\r\n%@ :: %@", [NSDate date], formattedMessage]; | |
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:logFile error:nil]; | |
int size = (int)(([fileAttribs fileSize]/1024)/1024); | |
if ( size >= 10 ) { | |
[[NSFileManager defaultManager] removeItemAtPath:logFile error:nil]; | |
APLog(@"[APL] # Removed log file. Over 10Mb! Starting again..."); | |
} | |
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:logFile]; | |
if ( fileHandle ) { | |
[fileHandle seekToEndOfFile]; | |
[fileHandle writeData:[formattedMessage dataUsingEncoding:NSUTF8StringEncoding]]; | |
[fileHandle closeFile]; | |
} | |
else { | |
[formattedMessage writeToFile:logFile atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil]; | |
} | |
} | |
void APMemLog (NSString *func, int line) | |
{ | |
APLog( @"[APL] # Free memory: %f (in %@ :: %d)", [[BMEDeviceManager sharedDevice] freeMemory] / 1000000.0f, func, line ); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment