Friday, December 22, 2006

Logging Method Name in C#


logMessage("functionName", "something bad happened");

Yes the above works if IF *if* you remember to update "functionName". However, remembering to update the parameter after copying from another method is often not done. It is also a pain and not necessary. A little reflection easily solves this problem:

// start one up so that we don't get the current
// method but the one that called this one
StackFrame sf = new StackFrame(1, true);
System.Reflection.MethodBase mb = sf.GetMethod();
string methodName = mb != null ? mb.Name : "";
// fileName can be null, if unable to determine
string fileName = sf.GetFileName();
// we only want the filename not the complete path
if (fileName != null) fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
int lineNumber = sf.GetFileLineNumber();

However the above does not work if you are using RichException or other proxies. So something a bit more complicated is needed:

private StackFrame GetCallingStackFrame()
{
// Determine the method that called the one that is calling this one.
// This is not just two up the Stack because of RichException support.
StackFrame sf = null;
// Start at 2. 1 for this one and another for the one above that.
StackTrace st = new StackTrace(2, true);
Type thisType = GetType();
foreach (StackFrame sfi in st.GetFrames())
{
// Find a calling method that is not part of this log class but is part
// of the same Namespace.
Type callType = sfi.GetMethod().DeclaringType;
if (callType != thisType &&
callType.Namespace == thisType.Namespace &&
!callType.IsInterface)
{
sf = sfi;
break;
}
}
return sf;
}

This can then be used like so:

StackFrame sf = GetCallingStackFrame();
if (sf != null) // if found add info to log message
{
System.Reflection.MethodBase mb = sf.GetMethod();
string methodName = mb != null ? mb.Name : "";
string fileName = sf.GetFileName();
if (fileName != null) fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
int lineNumber = sf.GetFileLineNumber();

if (fileName != null)
{
strMsg = fileName + "(" + lineNumber + ") - " + methodName + " - " + strMsg;
}
else
{
strMsg = "unknown - " + methodName + " - " + strMsg;
}
}

Wednesday, December 20, 2006

WRT54GL with Thibor's HyperWRT QoS setup

I have been using Vonage's VOIP with my WRT54GL setup with Thibor's HyperWRT as outlined in my previous post. It has been working without any problems.

I realized that I never did post my QoS setup. So here it is:

Priority Name Information
Medium POP3 ---
Medium DNS ---
Medium SMTP ---
Medium Http ---
Medium SSH ---
High Skype ---
Medium Yahoo Messenger ---
Medium MSN Messenger ---
Medium AOL Messenger ---
Low Bittorrent ---
Low MSN File Transfers ---
Low FTP ---
Highest Vonage VT2442 MAC xx:xx:xx:xx:xx:xx
Low Port 8080 Port 8080 - 8080

Saturday, September 02, 2006

10 Steps to setup WRT54GL with Thibor's HyperWRT

In my previous post I discussed the problems I am having with Vonage VOIP. In short I have been having problems with choppy, one-way, dropped audio. I'm attempting to fix this by installing a LinkSys WRT54GL with third party firmware. The third party firmware should allow me to setup QoS (Quality of Service) prioritization for the VOIP router. Currently I have the following setup:
Charter-Cable-Modem 
    -> WRT54GL 
          -> Vonage-Motorola-VT2442 -> telephone
          -> Wireless Network
          -> Wired Network

I have not yet setup the QoS. I'll outline those steps in a future post.

Here are the steps I followed for setting up my WRT54GL with Thibor's HyperWRT firmware.

Steps:

1. Order WRT54GL LinkSys router from Amazon.
(If your RSS reader does not display a link to Amazon, please visit my blog).

2. Choose third party firmware to use. Here is a good spot to start looking. I decided to go with Thibor's HyperWRT because it is based on the original firmware and provided all the extra features I wanted. Check out the feature list. Also there are really good directions here.

3. Download the firmware. Nate at vonage.nmhoy.net recommends version 14 of Thibor. However it is not clear from his site or the Thibor site which of the version 14 to use for the WRT54GL. I figured I would go with the latest version. I can always downgrade to version 14 in the future if I want. Since I'm upgrading from the stock firmware I downloaded this one. I verified that the S/N of my WRT54GL started with CL7A or CL7B (mine was CL7B).

4. Open Amazon box and remove router. Remove the "Run CD First before connecting cables" sticker. Kind of scary to completely ignore the directions but I didn't order this router to use the firmware that came with it.

5. Give the router power and connect it to your computer via a wired connection (I used port #1). Do not hook it up to your cable modem yet. That will come later.

6. Navigate Internet Explorer (I use Firefox normally, but it is best to be safe) to: http://192.168.1.1. I had to first do a ipconfig /renew since I was connected to my existing wireless when I started. Login using blank userid and password: admin.

7. I started with a factory reset. Since it is from the factory it seems like that wouldn't been necessary, but once again I figure better safe than sorry. Go to Administration - Factory Defaults.

8. Install firmware downloaded above. Unzip the firmware. Go to Administration - Firmware Upgrade. Select the unziped bin file: Hyperwrt_G_Thibor15c.bin. Be patient and do not interrupt the upload. Once the upload is complete you should see: "Firmware Version: v4.71.1, Hyperwrt 2.1b1 + Thibor15c" in the upper right hand corner. Restart Internet explorer to clear out its cache. Go to Administration - Factory Defaults and select "Clear NVRAM, restore factory defaults".

9. Setup your router. Here is what I did:
9a. Administration - Management: Changed password, SSHD enabled, changed SSHD ports, remote management disabled.
9b. Setup - Basic Setup: Changed router name, max DHCP clients: 10, changed to central standard time. Since I have a cable modem I left it on the default of DHCP. If you have PPPoE or something else then you will of course have to change this.
9c. Wireless - Basic Wireless Setup: Changed SSID
9d. Wireless - Advanced Wireless Setup: Transmit Power: 67% (I'll try this for now)
9e. Security - Firewall: checked Block Portscans
9f. That's enough to get us started. I'll be posting more about configuration later.

10. Add the WRT54GL to your network. I hooked the WRT54GL router to my cable/dsl modem; hooked the Motorola VT2442 Vonage device to port #1 of the WRT54GL; hooked my wired computers to the WRT54GL and turned everything back on.

My network is back up and working. The Vonage phone is working (I have not tested yet with a large file upload in progress). Its late and I'm going to bed...

Monday, August 28, 2006

Vonage VOIP Saga

We recently switched from ATT DSL to Charter Cable for Internet. My bother-in-law has had cable modems for years and I have always enjoyed the speed when over at his house. So when my SBC DSL contract ran out I signed up for Charter Cable. So far I have been pleased.

Now that we were free of DSL, we could sign up for VOIP. I went with Vonage because it is popular and I knew some folks that were using it and they didn't have any complaints.

I also had decided to try out a online backup program called ElephantDrive. It turns out that ElephantDrive sucks up all the outgoing bandwidth it can get its hands on (makes sense because it was doing a complete backup). My initial backup took weeks since I didn't let it run all the time. BTW, I was trying ElephantDrive because it was free. It is still in Beta and I wouldn't recommend it at this time since it sucks the life out of your machine when it runs and it takes forever to do a backup. However, it makes an excellent test program for using available upload bandwidth.

What I quickly discovered is the WRTP54G router that I paid extra for because I wanted to upgrade from my old 802.11b to 802.11g didn't prioritize my line so that VOIP worked. It is suppose to and Vonage tech-support tried their hardest to configure it to, but they just couldn't get a good outgoing connection when ElephantDrive was running. I kept getting Choppy, One-way, Dropped Audio. Finally advanced tech-support told me that I just couldn't run a program like this and expect good audio. Well, I didn't believe it. I knew that this could be done by prioritizing the packets using QoS. Actually, to be fair, I did finally get a hold of an advanced tech-support person who confirmed that you would need a high-end non-consumer grade router to accomplish what I was attempting to do.

After a number of calls and hours on the phone with tech-support and customer service, I convinced Vonage to replace my WRTP54G with a Motorola VT2442. I wanted to switch to the VT2442 not because I think it will provide better QoS, but so that I could get my $50 back that I paid extra for the WRTP54G. My VT2442 should come today.

I took the $50 and put it toward a WRT54GL. The 'L' stands for Linux. The WRT54GL allows you to add third-party firmware. So my plan is to implement this solution.

Stay tuned as I attempt to get good VOIP while running file uploads at the same time.

If I get it to work I plan on posting step by step instructions. Although the link above does a good job of that already.

Friday, August 11, 2006

VS 2005 Debugging C++ and C#

I was having problems attempting to step into a C# method from C++ using Visual Studio 2005. I asked Charles if he was able to get it to work and he said that he wasn't but that it should be possible. A few minutes later he had found this:

A handy trick when debugging C++ and C# in VS 2005...

For a project, go to Properties->Debugging and set the Debugger Type to Mixed from Auto. It now properly detects the associated languages, so one can step into a C# call from C++ and back.

Thursday, August 10, 2006

Emacs style indent line

One of the best features that Emacs provides is auto-formating of source code. When I moved to developing in Visual Studio 6, Justin provided the following macro that I mapped to the tab key that provided the same behavior.

Sub EmacsTab()
ext = GetFileExt(ActiveDocument.FullName)
if ext = "cpp" or ext = "h" or ext = "cxx" or ext = "hxx" or ext = "hpp" Then
With ActiveDocument.Selection
If .BottomLine > .TopLine Then
.SmartFormat
Else
col = .CurrentColumn
.StartOfLine dsFirstText
first = .CurrentColumn
.Indent
.SmartFormat
If col > first Then
.MoveTo .CurrentLine, col
End If
End If
End With
Else
ActiveDocument.Selection.Indent
End If
End Sub

When I moved to Visual Studio .Net 2003, the above macro no longer worked. So the following was created. Unfortunately you can no longer map the tab key, so I mapped it to ctrl-;.

Sub EmacsStyleIndentLine()
Dim ts As TextSelection = DTE.ActiveWindow.Selection
ts.SelectLine()
ts.SmartFormat()
ts.WordRight()
End Sub

Recently I moved to Visual Studio .Net 2005 and have been doing some C# coding. I found the above macro does not seem to work. Today I changed it to the following which appears to work just fine.

Sub EmacsStyleIndentLine()
With DTE.ActiveDocument.Selection
.LineUp()
.EndOfLine()
.Delete()
.NewLine()
.LineDown()
DTE.ExecuteCommand("Edit.FormatSelection")
End With
End Sub

Monday, May 01, 2006

RTSJ - The Real-Time Specification for Java

The latest Java News Brief by Don Busch is about RTSJ - The Real-Time Specification for Java. I had the opportunity to review and make comments before Don put the finishing touches on the article. Its a nice overview of Real-Time Java. Check it out.

Thursday, March 30, 2006

Command Prompt Here from Visual Studio

I use the explorer command prompt here capability all the time.

Today I was working in Visual Studio and thought wouldn't it be nice if you could just right click on a Solution Explorer file and select "Command Prompt Here". Well, I didn't find out how to do that, but I figured out how to do something even better. Add the command prompt as an External Tool.

Its rather easy to create an External Tool that opens a command prompt. Just select Tools.. External Tools...

Title: CommandPrompt
Command: c:\windows\system32\cmd.exe
Initial directory: $(ProjectDir)

Thats all there is to it. This will open a command prompt in the current project directory.

Now lets add one that opens a windows explorer to the project directory.

Title: Explorer
Command: explorer
Arguments: $(ProjectDir)

After you have created these external tools, you can then setup a shortcut or customize your menu to place them on the main menu.

Tuesday, February 21, 2006

Boost Training Opportunities In St. Louis

I'm still finishing up the course, but OCI has started advertising the class.

Boost is a quickly growing and widely utilized Open Source C++ collection of libraries that provide tomorrow's C++ today. Boost allows developers to more quickly develop high performance production ready tailored applications. Many of the libraries are either already part of the draft C++ Standards Committee's Library Technical Report (TR1) or are proposed for the upcoming TR2.

I'll be covering the following Boost libraries.

Boost Test Library
Boost Conversion Library
Boost Smart_ptr Library
Boost Container Library
Boost Bind Library
Boost Function Library
Boost Lambda Library
Boost Thread Library
Boost Regex Library
and more...

The first class is scheduled for April 26 - 28, 2006 with three more offerings in 2006. OCI offers onsite training as well.

Boost Training Class