Better NSLog for Your XCode Projects

A quick replacement to NSLog in your Obj C project
This will need no extra effort at all and changes the way you see your logs in debug area.

How it looks in Debug Area:

Before:

2013-01-29 15:46:24.076 Looptivity[76673:c07] ----2013-01-28 19:02:47 +0000
2013-01-29 15:46:24.076 Looptivity[76673:c07] USER YES
2013-01-29 15:46:24.107 Looptivity[76673:c07] Application become active
2013-01-29 15:46:24.109 Looptivity[76673:c07] Network reachable!

After:

[         MomiLoopAppDelegate.m : 154 ]: ----2013-01-28 19:02:47 +0000
[         MomiLoopAppDelegate.m : 160 ]: USER YES
[         MomiLoopAppDelegate.m : 409 ]: Application become active
[                    MVRemote.m : 102 ]: Network reachable!

Pros:

  • Automatically NSLogs are vanished from product build.
  • Get rid of long date-time stamp from log.
  • Trace logs with a bit more meaningful “Filename : Line-num” format with your log outputs.

Cons:

  • In your Debug-area All Output / Target Output should be selected. As it logs with printf function, it will not be visible when Debugger output is selected. Though, I don’t think it is a problem at all, do you?

How to Use:

  • Just Append this lines in your project’s .pch file, nothing more! See in your debug area when running code, and see how it is changed!!

Code:

#ifdef __OBJC__
#define __FILE_NAME_ONLY__ [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String]
#else
#define __FILE_NAME_ONLY__ ""
#endif

#ifndef OPTIMIZE
# define NSLog(...) printf("[%30s : %-4d]: %s\n", __FILE_NAME_ONLY__, __LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String])
#else
#define NSLog(...) do {} while (0)
#endif

Note:

This fix is collected and mixed up from some of blogs and Stack Overflow answers I cannot remember by now. But thanks to all of them to make my life easier!

Advertisements

Coloring git in Mac Terminal and more..

Most of the programmers prefers using git  from command-line as it unbound the true strength of this tool by helping you know the real flow. This is what most of fancy-gui-tool-users miss (even I was) when using git.

Today i am going to show you how we can afford a little more luxury with our command-prompt aka Terminal when using git.

It’s pretty strait-forward, easy to follow 2-step work-flow. [..Assuming you already have git installed..].

Git-coloring

Step 1 is just a git command to color git outputs and second part is a mac user-config to redesign prompt for better git experience. Anyone can skip step-2 and feel free to be happy with what you have. But believe me, it is also a easy trick with few steps.

  1. Git coloring: The small part with a big outcome: type this and <Enter>
    git config --global color.ui true

    and from now on, you’ll see your git outputs colored!

  2. Bash-prompt coloring: The bigger part with a unique outcome:
    1. Write this and press <Enter>
      cd ~
    2. And this again and press <Enter>
      nano .profile
    3. You should have nano opened with file .profile in your Terminal (a terminal-based notepad or something). Paste lines below (If there is already something written, scroll all the way bottom and paste.)
      # Setting GIT prompt
      c_cyan=`tput setaf 6`
      c_red=`tput setaf 1`
      c_green=`tput setaf 2`
      c_sgr0=`tput sgr0`
      
      branch_color ()
      {
          if git rev-parse --git-dir >/dev/null 2>&1
          then
              color=""
              if git diff --quiet 2>/dev/null >&2
              then
                  color=${c_green}
              else
                  color=${c_red}
              fi
          else
              return 0
          fi
          echo -n $color
      }
      
      parse_git_branch ()
      {
          if git rev-parse --git-dir >/dev/null 2>&1
          then
              gitver="["$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')"]"
          else
              return 0
          fi
      echo -e $gitver
      }
      
      #It's important to escape colors with \[ to indicate the length is 0
      PS1='{\[${c_cyan}\]\u\[${c_sgr0}\]} @\[${c_cyan}\]\W\[${c_sgr0}\]\[\[$(branch_color)\]$(parse_git_branch)\[${c_sgr0}\]# '
      
    4. Then Press Ctrl+x to close nano, y to confirm saving file. And you will be back in your Terminal again.
    5. Close Terminal and open again..(to get config loaded)
    6. Now you can change directory to any of your git repo and see the current branch-name with coloring! Red for uncommitted changes, Green for clean state.

Tell me if you have done it fine and like it. If there were something wrong, tell me that also and I’ll try to help.

Update:

After some experiment, i’ve come to know that .bash_profile and .bash_login gets precedence over .profile and so will not be executed if any of this is existed in your home dir. So if that is your case, you may prefer to append the code to .bash_profile instead of .profile.

A Calculator-like Display with JAVA JPanel

When I was trying to code my first calculator in java, I was wondering, why I can’t display the textfield like a real calculator display? I have no answer that time. But later on, I realized, I can! And at last, I did it!

It is an extended version of JPanel named AKDisplayPane which displays any text like a LED-type display. You can use it like a normal JPanel or JTextField without bothering how it works. (And of course, you can know how it works, if you want – as it is opensource.)

Look at this: (isn’t it nice!)

AKDigitalPane on Action
AKDigitalPane on Action

Features:

  1. You can set text anytime simply with a method setText(String text).
  2. You can set different font-size,
  3. You can set different foreground and background color,
  4. You can set it italic (use an angle in degree, how much it will bend to left/right),
  5. … and of course everything you can customize as a JPanel!

Where to start:

* Download example-project from github.

The project includes

  • Two class file AKDigit.java and AKDisplayPane.java within package com.ashkit.digital
  • And an example class TesterFrame.java, where I made a stopwatch using AKDisplayPane (the one shown in screenshot).

Believe me, It’s really so simple and easy!!

Code example:

Let’s have a look, how to use it:

  1. First of all, add “src/com” folder in your project.
    Add package com.ashkit.digital
  2. … and follow the code-snippet.
// Constructor: AKDisplayPane(int noOfDigits, float size)
AKDisplayPane p = new AKDisplayPane(15, 2.0f);

// set your text
p.setText(“12:45 AM”);

// you can customize to make it more like your's
p.setItalic(-10);
p.setFontColor(Color.BLACK.brighter());

// you can use customization as a JPanel
p.setBackground(Color.DARK_GRAY.brighter());
p.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));

Reference – AKDisplayPane :

Constructors:

  1. public AKDisplayPane(int noOfDigits, float thick)
  2. public AKDisplayPane(int noOfDigits, float thick, float gap)

Public Methods:

  1. public String getText()
  2. public void setText(String text)
  3. public void setFontColor(Color c)
  4. public void setFontParams(float thick, float gap)
  5. public void setItalic(int degree)