Skip to content

Instantly share code, notes, and snippets.

@nolili
Created May 12, 2013 07:15
Show Gist options
  • Save nolili/5562725 to your computer and use it in GitHub Desktop.
Save nolili/5562725 to your computer and use it in GitHub Desktop.
//
//
// Created by nori on 2012/12/21.
// Copyright (c) 2012年 nori. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIApplication(LoggerExtention)
+ (void)startLogging;
@end
@implementation UIApplication(LoggerExtention)
+ (void)startLogging
{
// ファイルをsandboxのDocumentsに保存
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
// ファイル名はタイム・スタンプを使う
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddhhmmss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"log%@", dateString];
NSString *fileExtention = @"log";
NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:fileName]
stringByAppendingPathExtension:fileExtention];
// ファイル名が重複している場合は、サフィックスをつける
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
NSUInteger n = 2;
do {
filePath = [[documentsDirectory stringByAppendingPathComponent:[fileName stringByAppendingFormat:@"_%d",n]] stringByAppendingPathExtension:fileExtention];
n++;
}
while ( [fileManager fileExistsAtPath:filePath] );
}
NSLog(@"%@", filePath);
// ログをリダイレクト
freopen([filePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
NSLog(@"Start Logging");
// バージョン情報をログに出力
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSLog(@"Bundle Version : %@",version);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment