NSLog output only when building in Debug Mode
This information can be found quite a few places online but I thought the information was a little scattered, so for my benefit and anyone else who might find this, I thought I’d do a quick and dirty how-to for getting some logging that only shows up when you build your project in debug mode. This is specific to the iPhone but I can’t imagine why you couldn’t re-factor it to work with any Xcode projects.
We’ll start by adding this to your application’s prefix file (should be named MyAppName_Prefix.pch.
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
Keeping with the quick and dirty thing, basically this just checks to see if DEBUG is defined somewhere and if it is it defines a preprocessor function called DLog(). DLog works just like NSLog() with only 3 exceptions.
- You can’t do
DLog(someVariable);. Instead doDLog(@"%@",someVariable). - The PRETTY_FUNCTION stuff adds some extra info about which class and the line number DLog was called from so you can easily find it in your code.
- You should only see log output while your app is built in Debug Mode.
We also define ALog() which works just like DLog but will always display and isn’t dependent on DEBUG.
Now all we have to do is define DEBUG in our project settings! Head into your Project Settings (Project -> Edit Project Settings) and in the BUILD tab, select Configuration: Debug and Show: Settings Defined At This Level. Add a User-Defined Setting and call it OTHER_CFLAGS. Set it’s value to -DDEBUG=1 and your all set.
Now you should only see any DLog calls when you build in Debug Mode! Like I said, this information is all around the web and I just wanted to kind of condense it all into a quick and dirty tutorial so I didn’t want to go into too much detail. If you want more information about how this is done, you check here and here for ALL the information I have here in much more detail. All the credit for the information here goes to those 2 posts.
Cocos2D-iPhone Xcode Template
I’m still researching how to get started with some game development on the iPhone and one of the articles I came across was a short tutorial detailing how to setup and start an application/game on the iPhone using the free cocos2d-iphone engine. Being the anal person I am, I couldn’t just refer to the article every time I wanted to start a new project, so I decided to research how to put together my own template for Xcode that would do all the dirty work necessary automatically. It turns out it’s quite easy to put together an Xcode template and what kind of good natured developer would I be if I didn’t share with the rest of the world?? I went ahead and created a new git repository with the template. To grab it, go ahead and follow these quick instructions..
cd /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project\ Templates/Application
git clone git://github.com/swemoney/xcode_cocos2d_iphone_app_template.git Cocos2D\ Based\ Application\ \(0.6.x\)
Do that in any terminal and next time you start up xcode and try and create a new project, you should see a Cocos2D-Based Application options there that will give you the basic starting blocks for using the cocos2d engine.. Enjoy!
UPDATE: Now includes version 0.6.3 of Cocos2d-iphone as well as a menu scene.
Low level caching for Rails 2.1
Rails has allowed you to do things like page caching and fragment caching for a while now but with Rails 2.1, they added a new cool feature for doing some more lower level things with cache. You can now cache pretty much anything you want, any way you want. It’s easy as pie too!
Rails.cache.fetch("my_caches_name", {:expires_in => 5.minutes}) { download_xml_file_from_web }
I’m using the above example in an application to cache an xml file from another website for 5 minutes. The expires_in option is only for if your using memcached as your cache store which I setup below in my config/development.rb..
config.cache_store = :mem_cache_store, '123.45.6.78:11211', {:namespace => 'myapp_cache'}
It’s that easy. I’m loving it. You can play around with the various settings. You can use a file store, memory store (which I think is default but won’t work well with multiple mongrels), drb store or the (imho, best option) memcached. There are other methods for Rails.cache that you can find here# in the documentation… Enjoy!
Colloquy with CTCP sounds!
I’ve had my mac for almost a full year now. I honestly don’t remember exactly when I got it but I think it was near the end of June or the beginning of July last year. Coming from Linux and Windows meant I had some choices to make about certain applications I was going to use. One of those choices was Colloquy for IRC. One feature I missed from mIRC and XChat was the ability to accept ctcp SOUND requests to play a sound. My friends and I use this in our own channel as an alert system. A trigger is typed that makes a bot send a ctcp SOUND request to the channel with a filename. We each have these sounds locally installed on our computers so we hear the sound after the request is made.
Today I decided to do something about Colloquy’s lack of sound so I started researching some scripting languages supports by the IRC client. I came across F-Script as a cocoa scripting language based on SmallTalk. Since I’ve always wanted to learn SmallTalk, I gave it a go. After a little bit of time, I was able to figure something out to enable sound requests to play sounds from Colloquy!
You can grab the plugin here (right click and save) and throw it in your ~/Library/Application Support/Colloquy/PlugIns directory. Type /fscript load sounds and it should be ready to roll! When a ctcp SOUND request is received, this script will try and find the file in your Sounds folder (i.e. ~/Library/Sounds) and play it automatically for you.
XMonline in Ruby!
Who wants to log in and go through the hassle of navigating a website when you can type a simple command to get the same thing? Not me! I usually use the MediaPlayerConnectivity Extension which lets me launch streaming media outside of the browser, but I wanted to go a step further and remove the browser completely from my XM Radio listening when I’m outside of my car. I created a nice little Ruby script for doing just that. With the wonderful Mechanize gem it’s quite easy to traverse websites without every looking at them.
The first thing (I mean, you obviously need Ruby, so go get that if you don’t have it yet) your going to want to do, is install the mechanize gem.
gem install mechanize -y
If you get permission errors, you probably need to run sudo gem install mechanize -y instead.
After that, you can copy the code below and paste it into your own xmradio.rb file. It’s pretty easy to use too. The first time you run the script, it will ask you a few questions about your login information and what type of media player you want to use. After this, you won’t see that message again unless you delete your ~/.xmradio file. You’ll probably need to make sure to chmod a+x xmradio.rb and you may want to throw the file somewhere like /usr/bin or somewhere that’s in your PATH so that you can execute it easily. The rest is easy!
xmradio.rb 202
The above command will launch the script, connect to listen.xmradio.com, log you in and start your preferred player. The number you specify after the xmradio.rb is the channel you wish to listen to. If you don’t specify a channel, it will play whatever default channel you chose when you first configured. If you ever want to reconfigure the script, either edit ~/.xmradio or just delete it and the next time you launch xmradio.rb, you’ll be asked the same questions again.
In the near future, I think it will be cool to add a couple of features, such as the ability to change channels while the script is running, or maybe the ability to save the stream with certain players. The recommended player is mplayer right now, but I also tested it a little bit with xine. Code is after the jump! Go get it!
UPDATE (2011/5/7) :: This code was created with the very old version of XMRadio online that has since been merged and updated to SiriusXM. I’m 99% sure this code doesn’t work anymore. If I ever get around to it, maybe I’ll try and create a new script that will work with the new site.
Burn an Audio CD with Ruby (Ubuntu/Linux)
I’m a simple person. I don’t need much when it comes to burning CD’s. Which is why I don’t really care that the CD burning support in linux isn’t all that spectacular. If there’s one thing that Ubuntu doesn’t do well, it’s burn CD’s. For their own sake, I hope they fix up Serpentine for the release of Feisty, because right now it’s terrible. It takes 10 minutes just to import a few mp3’s and then another 10 minutes to convert them.. and then we have to cross our fingers and hope the burn works or that Ubuntu see’s the blank CD that’s in the CDRW.
Luckily, this is the only thing I really use a CD burner for. Sometimes I need to burn some songs to an audio CD to listen to in the car. Since I was tired or working with Rails for an evening, I took a couple of hours to code a small script in Ruby that automates the process for me. There’s no GUI. It’s very simple. But it doesn’t need to be complicated, so who cares? It requires Ruby (of course) and the ruby-mp3info ruby gem. It also requires cdrdao. Which I’m pretty sure is standard in most Ubuntu systems, but I could be wrong about that.
gem install ruby-mp3info
Here’s the code. Have fun with it. Maybe I’ll slap a GUI on it when I feel like playing with Ruby and GTK some day. This shouldn’t have any problems running on any linux disto that has cdrao, ruby, and ruby-mp3info installed.