Skip to content

Commit

Permalink
reorganize debugging macro
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabiszewski authored and Bartek committed Nov 3, 2015
1 parent 816d27b commit f8ec489
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
2 changes: 2 additions & 0 deletions QLMobi.xcodeproj/project.pbxproj
Expand Up @@ -33,6 +33,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
15502B011BE89AD100372AB3 /* debug.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = debug.h; sourceTree = "<group>"; };
1573312C1BE0E1D400455426 /* BAFHtml.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BAFHtml.m; sourceTree = "<group>"; };
1573312E1BE0E20B00455426 /* BAFHtml.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BAFHtml.h; sourceTree = "<group>"; };
15ACCE3B1BE761200070C76F /* libxml2.2.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libxml2.2.tbd; path = usr/lib/libxml2.2.tbd; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -105,6 +106,7 @@
1573312C1BE0E1D400455426 /* BAFHtml.m */,
15ACCE481BE7F8080070C76F /* BAFXpath.h */,
15ACCE461BE7F7FA0070C76F /* BAFXpath.m */,
15502B011BE89AD100372AB3 /* debug.h */,
15DD6F6B1BA48FF400336FF5 /* Supporting Files */,
);
path = QLMobi;
Expand Down
17 changes: 10 additions & 7 deletions QLMobi/BAFHtml.m
Expand Up @@ -9,6 +9,7 @@
*/

#import "BAFHtml.h"
#import "debug.h"

static volatile int32_t instancesCount = 0;

Expand All @@ -26,13 +27,13 @@ @implementation BAFHtml
@synthesize document, body, head;

- (instancetype)init {
NSLog(@"HTML init (%li)", (long)instancesCount);
DebugLog(@"HTML init (%li)", (long)instancesCount);
self = [super init];
if (self) {
static dispatch_once_t once;
OSAtomicIncrement32(&instancesCount);
dispatch_once(&once, ^{
NSLog(@"xmlInitParser()");
DebugLog(@"xmlInitParser()");
xmlInitParser();
LIBXML_TEST_VERSION
});
Expand Down Expand Up @@ -78,15 +79,15 @@ - (instancetype)initWithData:(const char *)data length:(NSUInteger)length;
}

- (void)dealloc {
NSLog(@"HTML dealloc (%li)", (long)instancesCount - 1);
DebugLog(@"HTML dealloc (%li)", (long)instancesCount - 1);
/* Free the document */
if (document) {
NSLog(@"xmlFreeDoc()");
DebugLog(@"xmlFreeDoc()");
xmlFreeDoc(document);
}
OSAtomicDecrement32(&instancesCount);
if (instancesCount == 0) {
NSLog(@"xmlCleanupParser()");
DebugLog(@"xmlCleanupParser()");
xmlCleanupParser();
}
}
Expand Down Expand Up @@ -152,10 +153,12 @@ - (void)bodyNodeSetName:(NSString *)name {
}

- (void)dumpDocumentToLog {
#if defined(MOBI_DEBUG)
xmlBufferPtr buffer = xmlBufferCreate();
int bufSize = htmlNodeDump(buffer, document, body);
NSLog(@"Document size: %i", bufSize);
NSLog(@"Document content: %s", buffer->content);
DebugLog(@"Document size: %i", bufSize);
DebugLog(@"Document content: %s", buffer->content);
#endif
}

- (NSData *)documentData {
Expand Down
25 changes: 13 additions & 12 deletions QLMobi/BAFMobi.m
Expand Up @@ -13,6 +13,7 @@
#import <libxml/HTMLtree.h>
#import <mobi.h>
#import "BAFMobi.h"
#import "debug.h"

/**
Basic wrapper for libmobi
Expand All @@ -39,13 +40,13 @@ @implementation BAFMobi
@synthesize mData, mRawml;

- (instancetype)init {
NSLog(@"Mobi init");
DebugLog(@"Mobi init");
self = [super init];
if (self) {
NSLog(@"mobi_init()");
DebugLog(@"mobi_init()");
mData = mobi_init();
if (mData == nil) {
NSLog(@"Memory allocation failed");
DebugLog(@"Memory allocation failed");
return nil;
}
mRawml = nil;
Expand All @@ -54,13 +55,13 @@ - (instancetype)init {
}

- (void)dealloc {
NSLog(@"Mobi dealloc");
DebugLog(@"Mobi dealloc");
if (mData) {
NSLog(@"mobi_free(mData)");
DebugLog(@"mobi_free(mData)");
mobi_free(mData);
}
if (mRawml) {
NSLog(@"mobi_free(mRawml)");
DebugLog(@"mobi_free(mRawml)");
mobi_free_rawml(mRawml);
}
}
Expand All @@ -78,19 +79,19 @@ - (instancetype)initWithURL:(NSURL *)url {
}

- (void)load:(NSURL *)url {
NSLog(@"Loading file %@ (%@)", [url path], [NSThread currentThread]);
DebugLog(@"Loading file %@ (%@)", [url path], [NSThread currentThread]);
// open file
FILE *file = fopen([[url path] UTF8String], "rb");
if (file == NULL) {
NSLog(@"Error opening file %@", [url path]);
DebugLog(@"Error opening file %@", [url path]);
mobi_free(mData);
mData = nil;
}
// load file into mobi structure
MOBI_RET mobi_ret = mobi_load_file(mData, file);
fclose(file);
if (mobi_ret != MOBI_SUCCESS) {
NSLog(@"Error loading file (%u)", mobi_ret);
DebugLog(@"Error loading file (%u)", mobi_ret);
mobi_free(mData);
mData = nil;
}
Expand All @@ -100,16 +101,16 @@ - (void)parse {
if (mData == nil) {
return;
}
NSLog(@"mobi_init_rawml()");
DebugLog(@"mobi_init_rawml()");
mRawml = mobi_init_rawml(mData);
if (mRawml == nil) {
NSLog(@"Memory allocation failed");
DebugLog(@"Memory allocation failed");
return;
}
// parse mobi data into rawml structure
MOBI_RET mobi_ret = mobi_parse_rawml_opt(mRawml, mData, NO, NO, NO);
if (mobi_ret != MOBI_SUCCESS) {
NSLog(@"Error parsing file (%u)", mobi_ret);
DebugLog(@"Error parsing file (%u)", mobi_ret);
mobi_free(mData);
mobi_free_rawml(mRawml);
mData = nil;
Expand Down
9 changes: 3 additions & 6 deletions QLMobi/GeneratePreviewForURL.m
Expand Up @@ -13,14 +13,11 @@
#import <QuickLook/QuickLook.h>
#import "BAFMobi.h"
#import "BAFHtml.h"
#import "debug.h"

/** max size of parsed html */
static const NSUInteger maxSize = (1024 * 1024);

#if !defined(MOBI_DEBUG)
#define NSLog(...)
#endif

/** Replace links to resources in html part with cid scheme and add resources as attachments */
Status processHTML(BAFMobi *mobi, BAFHtml *partHTML, NSMutableDictionary *attachments, QLPreviewRequestRef preview) {
if (!partHTML) {
Expand Down Expand Up @@ -144,7 +141,7 @@ Status processHTML(BAFMobi *mobi, BAFHtml *partHTML, NSMutableDictionary *attach
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
CFURLRef url, CFStringRef contentTypeUTI,
CFDictionaryRef options) {
NSLog(@"Starting QLMobi Preview Generator: %@", [NSThread currentThread]);
DebugLog(@"Starting QLMobi Preview Generator: %@", [NSThread currentThread]);
#ifdef MOBI_DEBUG
NSDate *startTime = [NSDate date];
#endif
Expand Down Expand Up @@ -240,7 +237,7 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
#ifdef MOBI_DEBUG
NSDate *endTime = [NSDate date];
NSTimeInterval executionTime = [endTime timeIntervalSinceDate:startTime];
NSLog(@"execution time: %f sec", executionTime);
DebugLog(@"execution time: %f sec", executionTime);
#endif
return noErr;
}
Expand Down
20 changes: 20 additions & 0 deletions QLMobi/debug.h
@@ -0,0 +1,20 @@
/** @file debug.h
*
* Copyright (c) 2015 Bartek Fabiszewski
* http://www.fabiszewski.net
*
* This file is part of QLMobi.
* Licensed under GPL, either version 3, or any later.
* See <http://www.gnu.org/licenses/>
*/

#ifndef debug_h
#define debug_h

#if defined(MOBI_DEBUG)
#define DebugLog(...) NSLog(__VA_ARGS__)
#else
#define DebugLog(...)
#endif

#endif /* debug_h */

0 comments on commit f8ec489

Please sign in to comment.