Skip to content

Commit

Permalink
added macro AFLog for logging and removed most of the AFCACHE_LOGGING…
Browse files Browse the repository at this point in the history
…_ENABLED ifdefs
  • Loading branch information
artifacts committed Feb 8, 2011
1 parent ca69f12 commit 50afa63
Show file tree
Hide file tree
Showing 13 changed files with 731 additions and 320 deletions.
8 changes: 3 additions & 5 deletions 3rdparty/ZipArchive.m
Expand Up @@ -10,7 +10,7 @@
#import "ZipArchive.h"
#import "zlib.h"
#import "zconf.h"

#import "AFCache_Logging.h"


@interface ZipArchive (Private)
Expand Down Expand Up @@ -148,9 +148,7 @@ -(BOOL) UnzipOpenFile:(NSString*) zipFile
unz_global_info globalInfo = {0};
if( unzGetGlobalInfo(_unzFile, &globalInfo )==UNZ_OK )
{
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"%@", [NSString stringWithFormat:@"%d entries in the zip file",globalInfo.number_entry] );
#endif
AFLog(@"%@", [NSString stringWithFormat:@"%d entries in the zip file",globalInfo.number_entry] );
}
}
return _unzFile!=NULL;
Expand Down Expand Up @@ -257,7 +255,7 @@ -(BOOL) UnzipFileTo:(NSString*) path overWrite:(BOOL) overwrite
if( ![[NSFileManager defaultManager] setAttributes:attr ofItemAtPath:fullPath error:nil] )
{
// cann't set attributes
NSLog(@"Failed to set attributes");
AFLog(@"Failed to set attributes");
}

}
Expand Down
16 changes: 4 additions & 12 deletions AFCache+Packaging.m
Expand Up @@ -12,6 +12,7 @@
#import "DateParser.h"
#import "AFPackageInfo.h"
#import "AFCache+Packaging.h"
#import "AFCache_Logging.h"

@implementation AFCache (Packaging)

Expand Down Expand Up @@ -84,9 +85,7 @@ - (void)consumePackageArchive:(AFCacheableItem*)cacheableItem preservePackageInf
- (void)unzipWithArguments:(NSDictionary*)arguments {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"starting to unzip archive");
#endif
AFLog(@"starting to unzip archive");

// get arguments from dictionary
NSString* pathToZip = [arguments objectForKey:@"pathToZip"];
Expand Down Expand Up @@ -120,11 +119,6 @@ - (void)unzipWithArguments:(NSDictionary*)arguments {

[inv getReturnValue:&packageInfo];
[packageInfo retain];

NSLog(@"return value: %@", packageInfo);




// store information about the imported items
if (preservePackageInfo == YES) {
Expand All @@ -145,11 +139,9 @@ - (void)unzipWithArguments:(NSDictionary*)arguments {

[self performSelectorOnMainThread:@selector(archive) withObject:nil waitUntilDone:YES];
[packageInfo autorelease];
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"finished unzipping archive");
#endif
AFLog(@"finished unzipping archive");
} else {
NSLog(@"Unzipping failed. Broken archive?");
AFLog(@"Unzipping failed. Broken archive?");
[self performSelectorOnMainThread:@selector(performUnarchivingFailedWithItem:)
withObject:cacheableItem
waitUntilDone:YES];
Expand Down
1 change: 0 additions & 1 deletion AFCache.h
Expand Up @@ -39,7 +39,6 @@
// max number of concurrent connections
#define kAFCacheDefaultConcurrentConnections 5

//#define AFCACHE_LOGGING_ENABLED true
#define kHTTPHeaderIfModifiedSince @"If-Modified-Since"
#define kHTTPHeaderIfNoneMatch @"If-None-Match"

Expand Down
89 changes: 27 additions & 62 deletions AFCache.m
Expand Up @@ -35,6 +35,7 @@
#include <sys/xattr.h>
#import "ZipArchive.h"
#import "AFRegexString.h"
#import "AFCache_Logging.h"

#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
Expand Down Expand Up @@ -143,17 +144,13 @@ - (void)reinitialize {
clientItems = [[NSMutableDictionary alloc] init];
NSDictionary *archivedExpireDates = [NSKeyedUnarchiver unarchiveObjectWithFile: infoStoreFilename];
if (!archivedExpireDates) {
#if AFCACHE_LOGGING_ENABLED
NSLog(@ "Created new expires dictionary");
#endif
AFLog(@ "Created new expires dictionary");
self.cacheInfoStore = nil;
cacheInfoStore = [[NSMutableDictionary alloc] init];
}
else {
self.cacheInfoStore = [NSMutableDictionary dictionaryWithDictionary: archivedExpireDates];
#if AFCACHE_LOGGING_ENABLED
NSLog(@ "Successfully unarchived expires dictionary");
#endif
AFLog(@ "Successfully unarchived expires dictionary");
}
archivedExpireDates = nil;

Expand All @@ -163,16 +160,12 @@ - (void)reinitialize {
NSDictionary *archivedPackageInfos = [NSKeyedUnarchiver unarchiveObjectWithFile: packageInfoPlistFilename];

if (!archivedPackageInfos) {
#if AFCACHE_LOGGING_ENABLED
NSLog(@ "Created new package infos dictionary");
#endif
AFLog(@ "Created new package infos dictionary");
packageInfos = [[NSMutableDictionary alloc] init];
}
else {
self.packageInfos = [NSMutableDictionary dictionaryWithDictionary: archivedPackageInfos];
#if AFCACHE_LOGGING_ENABLED
NSLog(@ "Successfully unarchived package infos dictionary");
#endif
AFLog(@ "Successfully unarchived package infos dictionary");
}
archivedPackageInfos = nil;

Expand All @@ -186,16 +179,14 @@ - (void)reinitialize {
NSError *error = nil;
/* check for existence of cache directory */
if ([[NSFileManager defaultManager] fileExistsAtPath: dataPath]) {
#if AFCACHE_LOGGING_ENABLED
NSLog(@ "Successfully unarchived cache store");
#endif
AFLog(@ "Successfully unarchived cache store");
}
else {
if (![[NSFileManager defaultManager] createDirectoryAtPath: dataPath
withIntermediateDirectories: YES
attributes: nil
error: &error]) {
NSLog(@ "Failed to create cache directory at path %@: %@", dataPath, [error description]);
AFLog(@ "Failed to create cache directory at path %@: %@", dataPath, [error description]);
}
}
requestCounter = 0;
Expand Down Expand Up @@ -258,9 +249,7 @@ - (void)setContentLengthForFile:(NSString*)filename
NSDictionary* attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filename error:&err];
if (nil != err)
{
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"Could not get file attributes for %@", filename);
#endif
AFLog(@"Could not get file attributes for %@", filename);
return;
}
uint64_t fileSize = [attrs fileSize];
Expand All @@ -270,9 +259,7 @@ - (void)setContentLengthForFile:(NSString*)filename
sizeof(fileSize),
0, 0))
{
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"Could not set content length for file %@", filename);
#endif
AFLog(@"Could not set content length for file %@", filename);
return;
}
}
Expand Down Expand Up @@ -446,9 +433,7 @@ - (AFCacheableItem *)cachedObjectForURL: (NSURL *) url
item.cacheStatus = kCacheStatusFresh;
//item.info.responseTimestamp = [NSDate timeIntervalSinceReferenceDate];
[item performSelector:@selector(connectionDidFinishLoading:) withObject:nil];
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"serving from cache: %@", item.url);
#endif
AFLog(@"serving from cache: %@", item.url);
return item;
}
// Item is not fresh, fire an If-Modified-Since request
Expand Down Expand Up @@ -535,7 +520,7 @@ - (AFCacheableItem *)cachedObjectForURL: (NSURL *) url
- (void)archiveWithInfoStore:(NSDictionary*)infoStore {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
#if AFCACHE_LOGGING_ENABLED
NSLog(@"start archiving");
AFLog(@"start archiving");
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#endif
@synchronized(self)
Expand All @@ -553,7 +538,7 @@ - (void)archiveWithInfoStore:(NSDictionary*)infoStore {
[autoreleasePool release], autoreleasePool = nil;
}
#if AFCACHE_LOGGING_ENABLED
NSLog(@"Finish archiving in %f", CFAbsoluteTimeGetCurrent() - start);
AFLog(@"Finish archiving in %f", CFAbsoluteTimeGetCurrent() - start);
#endif
[pool release];
}
Expand Down Expand Up @@ -687,9 +672,7 @@ - (NSFileHandle*)createFileForItem:(AFCacheableItem*)cacheableItem
// remove file if exists
if ([[NSFileManager defaultManager] fileExistsAtPath: filePath]) {
[self removeCacheEntryWithFilePath:filePath fileOnly:YES];
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"removing %@", filePath);
#endif
AFLog(@"removing %@", filePath);
}

// create directory if not exists
Expand All @@ -710,9 +693,7 @@ - (NSFileHandle*)createFileForItem:(AFCacheableItem*)cacheableItem
}
}
[[NSFileManager defaultManager] createDirectoryAtPath:pathToDirectory withIntermediateDirectories:YES attributes:nil error:&error];
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"creating directory %@", pathToDirectory);
#endif
AFLog(@"creating directory %@", pathToDirectory);
}

// write file
Expand All @@ -726,9 +707,7 @@ - (NSFileHandle*)createFileForItem:(AFCacheableItem*)cacheableItem
}

fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"created file at path %@ (%d)", filePath, [fileHandle fileDescriptor]);
#endif
AFLog(@"created file at path %@ (%d)", filePath, [fileHandle fileDescriptor]);
}
else {
NSLog(@ "AFCache: item %@ \nsize exceeds maxItemFileSize (%f). Won't write file to disk",cacheableItem.url, maxItemFileSize);
Expand All @@ -745,40 +724,30 @@ - (AFCacheableItem *)cacheableItemFromCacheStore: (NSURL *) URL {
NSString *key = [self filenameForURL:URL];
// the complete path
NSString *filePath = [self filePathForURL: URL];
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"checking for file at path %@", filePath);
#endif
AFLog(@"checking for file at path %@", filePath);

if (![[NSFileManager defaultManager] fileExistsAtPath: filePath])
{
// file doesn't exist. check if someone else is downloading the url already
if ([[self pendingConnections] objectForKey:URL] != nil
|| [self isQueuedURL:URL])
|| [self isQueuedURL:URL])
{
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"Someone else is already downloading the URL: %@.", [URL absoluteString]);
#endif
AFLog(@"Someone else is already downloading the URL: %@.", [URL absoluteString]);
}
else
{
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"Cache miss for URL: %@.", [URL absoluteString]);
#endif
AFLog(@"Cache miss for URL: %@.", [URL absoluteString]);
return nil;
}
}

#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"Cache hit for URL: %@", [URL absoluteString]);
#endif
AFLog(@"Cache hit for URL: %@", [URL absoluteString]);

AFCacheableItemInfo *info = [cacheInfoStore objectForKey: key];
if (!info) {
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"Cache info store out of sync for url %@: No cache info available for key %@. Removing cached file %@.", [URL absoluteString], key, filePath);
#endif
[self removeCacheEntryWithFilePath:filePath fileOnly:YES];

// Something went wrong
AFLog(@"Cache info store out of sync for url %@: No cache info available for key %@. Removing cached file %@.", [URL absoluteString], key, filePath);
[self removeCacheEntryWithFilePath:filePath fileOnly:YES];

return nil;
}
Expand All @@ -801,9 +770,7 @@ - (void)cancelConnectionsForURL: (NSURL *) url
if (nil != url)
{
NSURLConnection *connection = [pendingConnections objectForKey: url];
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"Cancelling connection for URL: %@", [url absoluteString]);
#endif
AFLog(@"Cancelling connection for URL: %@", [url absoluteString]);
[connection cancel];
[pendingConnections removeObjectForKey: url];
}
Expand Down Expand Up @@ -1069,9 +1036,7 @@ - (void)downloadItem:(AFCacheableItem*)item
if (nil != [pendingConnections objectForKey:item.url])
{
// don't start another connection
#ifdef AFCACHE_LOGGING_ENABLED
NSLog(@"We are downloading already. Won't start another connection for %@", item.url);
#endif
AFLog(@"We are downloading already. Won't start another connection for %@", item.url);
return;
}

Expand Down Expand Up @@ -1216,7 +1181,7 @@ + (void) setLoggingEnabled: (BOOL) enabled
lpkdebugf("AFCache", "using %s", ER_ADDRESS_OF_GLOBAL_OR_EMBEDDED( logPointLibraryIdentifier )() );

#else
NSLog(@"AFCache setLoggingEnabled: ignored (EngineRoom not embedded)");
AFLog(@"AFCache setLoggingEnabled: ignored (EngineRoom not embedded)");
#endif
}

Expand All @@ -1229,7 +1194,7 @@ + (void) setLogFormat: (NSString *) logFormat
lpkdebugf("AFCache", "%s", "ignored (using non-embedded EngineRoom)");
}
#else
NSLog(@"AFCache setLogFormat: ignored (EngineRoom not embedded)");
AFLog(@"AFCache setLogFormat: ignored (EngineRoom not embedded)");
#endif
}

Expand Down

0 comments on commit 50afa63

Please sign in to comment.