Skip to content

Instantly share code, notes, and snippets.

@elpsk
Last active August 29, 2015 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpsk/9b72ffaffec947546c6b to your computer and use it in GitHub Desktop.
Save elpsk/9b72ffaffec947546c6b to your computer and use it in GitHub Desktop.
APLogger - Write console log to disk
//
// 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
//
// 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