Skip to content

Instantly share code, notes, and snippets.

@elpsk elpsk/APLogger.h
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.