Monday, February 16, 2009

A Debug Macro for Xcode

* Updated on 28 November 2011 for Xcode 4.2

I am a newcomer to the Xcode and I decided to check the debug means. Basically, I was looking for a macro to dump line number, file name and function name along with some output. However, I found only some "simple" debug classes. I think debug output should be done by macros. I checked the Apple documentation and found an interesting topic: Variadic Macros. They are introduced by the C99 standard. After a few attempts I decided that the following macro is good enough for me:

//
//  dbg.h
//
//  Created by oguz.kupusoglu@gmail.com on 15/2/2009.
//  Copyright 2009 Oguz Kupusoglu. All rights reserved.
//

#ifdef DEBUG
    #define DBG(format, ...)                   NSLog(@"%d,%s,"format,__LINE__,__FUNCTION__,##__VA_ARGS__);
    #define DBG_CALLED                         DBG("called")
#else
    #define DBG(format, ...)
    #define DBG_CALLED
#endif

#define LOG(format, ...)                       NSLog(@"%d,%s,"format,__LINE__,__FUNCTION__,##__VA_ARGS__);
#define LOG_CALLED                             LOG("called")   

Note that DBG_CALLED is a helper define which is useful for following the callstack. I ignored the file name, i.e. __FILE__, as the method name being in the [class method] format gives a hint of the file in question due to naming files after the class names convention. Certainly one can add __FILE__ at the expense of extra overhead. Sample usage and output as received from the Console which is accessed from the "View/Debug Area/Activate Console" option of the Xcode are below:

-(void) viewWillAppear:(BOOL)animated
{
    DBG_CALLED
    
    DBG("view frame\n"
        "origin x:|%.0f|  y:|%.0f|\n"
        "width:|%.0f|  height:|%.0f|\n",
        self.view.frame.origin.x, self.view.frame.origin.y,
        self.view.frame.size.width, self.view.frame.size.height)   
       
    DBG("year:|%d|", 2009)
   
    DBG("updated")
       
    [super viewWillAppear:animated];
}

...] 69,-[MyViewController viewWillAppear:],called
...] 75,-[MyViewController  viewWillAppear:],view frame
origin x:|0|  y:|20|
width:|768|  height:|1004|
...] 77,-[MyViewController viewWillAppear:],year:|2009|
...] 79,-[MyViewController viewWillAppear:],updated

Note that the DEBUG define is set via the "PROJECT/Build Settings" option. Go to "... Preprocessing" section and add the define "DEBUG" to the "Debug" subsection.

Finally, I think "Product/Generate Output\Generate Preprocessed File" is a good option as it gives the preprocessor output of the selected file. For example the above DBG() macros are expanded as below:

-(void) viewWillAppear:(BOOL)animated
{
    NSLog(@"%d,%s,""called",69,__FUNCTION__);

    NSLog(@"%d,%s,""view frame\n" "origin x:|%.0f|  y:|%.0f|\n" "width:|%.0f|  height:|%.0f|\n",75,__FUNCTION__, self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);





    NSLog(@"%d,%s,""year:|%d|",77,__FUNCTION__, 2009);

    NSLog(@"%d,%s,""updated",79,__FUNCTION__);

    [super viewWillAppear:animated];
}

Note that when there is no variable argument, "##" removes the trailing comma! Just excellent...

Sunday, February 8, 2009

Issız Adam

Bu Çağan Irmak filmini* 7 Şubat 2009 Cumartesi günü seyrettik. Öykü yeni değil: hayatının kadını karşısına çıktığında bağlanmaktan korktuğu için onu terkeden bir adamın, yani Ada ve Alper'in öyküsü. Ayrıca filmdeki karakterler çok tuzu kuru, bohem hayatlar sürdürüyorlar. Ama oyuncuların sevimliliği, "Anne"nin kattığı gerçekçilik ve duygu sömürüsü düzeyinde duyguları yoğunlaştırmayı başaran yönetmen durumu kurtarıyor. Filmde Ada'nın kendisine asılan Alper'e kahve bardağını boşaltması, Ada'nın Alper'den ayrıldıktan sonra Alper'in annesinin yanına gitmesi ve son sahnede karşılaşmaları ve "Anlamazdın Anlamazdın" ile filmin bitişi oldukça hoş.

Introduction to MacBook

I bought a MacBook On 28 January 2009 from the Pupa at IstinyePark. My first impression is that it is very well designed! I think the Apple Key, i.e. "cmd" key, replaces the Control Key on the Windows computers. And, there is no "right" mouse key: in order to open a context menu the user needs to press "cntrl" key. However, I think having no "fullscreen" windows is a problem! Plus, it is not easy to see the open windows: F3 keys shows them and CMD+TAB selects windows sequentially. Overall I think as time goes by I will like the MacBook!