How to write a message logger in C

You are probably here because some demanding professor has ordered you to produce an example of a program that recieves messages and pulses and logs stuff about them.

Here I will provide  the answer to all your questions. Well, some of your questions.

I can’t, for example, tell you why you decided to take this stupid degree in the first place.
I can’t tell you where to find true love, or the secret of happiness.

I can’t even tell you how to code – I can attempt to give you some information, but everyone basically has to teach themselves how to program. Also, I apologize for the formatting – wordpress’s text editor is literally the worst thing in the universe.

Oh well, read on if you must.
First, some inclusions. The usual suspects – we’ll need the ability to write to the command line, concatenate a string, make timestamps.

“msg.h” is a header file provided by my teacher. There’s a bunch of stuff in there, but the important bits are:

The message header can be accessed by theMessage.m_hdr. Pulses can send codes, and this particular pulse code can be accessed with theMessage.pulse.code. I can’t remember what the rest of the includes are for. I might’ve copied them from a different lab.


First, we’re going to need a bunch of variables.

You could probably write a better usage message. This just means that the program was called with too few arguements – no logfile was specified, so it doesn’t know where to store logs.

Establish a connection to whatever client program is trying to get ahold of you. This function will not execute until the client shows up.

When the client does show up, its process id, channel id and node descriptor will be recorded in the pidfile, via this bit:

Then open the logfile to record whatever messages the client wants to send.

At this point, you’re going to start an infinite while loop. It will keep running as long as the client wants to keep sending messages. It only terminates when the client sends a pulse.

In each iteration of the while loop, the logger looks for a new message and stores it at theMessage’s memory location. The timestruct initalization doesn’t have to be in the while loop, but there it is. It does have to exist though, cause if you don’t initialize the timestruct then it will just claim that it’s Jan 1 1970. You can pass arguments to specify a time format, or just send it null for the default.

Next is a switch to check the message header to see what sort of thing it is.

  • If it’s a regular message, it’ll have a unique rcvid.
  • If it’s a pulse, the rcvid will be 0, since you don’t need to reply to pulses. The whole idea of a pulse is that it’s non-blocking – it doesn’t wait for a reply. Kinda like HTTP, in that pulses sends out messages and hope for the best. If no acknowledgement arrives after while it just gives up./li>

The first case, a proper message arrives. Report on the command line that something happened, and log the item and the date. Reply with an acknowledgement that the message is fine, flush the buffer, and carry on.

In this program, we’re using pulses to terminate the logger program. So if a pulse arrives,

You know it’s a pulse because the theMessage.m_hdr == 0, though it’s translated to the enum MSG_ENG for the sake of readability.

If anything other than a pulse or a valid message turns up, the logger just shrugs and carries on.

And that’s basically it – just clean up and exit.

The Complete Code

I hope it’s helpful. I also hope you actually understand it, rather than just turning it in as is.



Success with Stripe

In a previous post, I remarked on my difficulty with getting Stripe running.  I see it’s been almost 2 months since I posted that. Boy, I take a while sometimes, but I get stuff done. I finally found success with the help of an excellent tutorial by the handsome and intelligent Mark Hagan.

It turned out that it actually was easy, once I saw how it was done. But like changing a tire or getting a date with an attractive stranger, lots of “easy” things aren’t, until you actually do them.

I had no idea how to use an API from an external website. Some of the pieces I was missing:

  • That you need to include reference to an external library, and how to do so
  • Realizing that the term “library” refers to the C# git repository that was linked to on the Stripe APIs page, which I downloaded, but had no idea what to do with.
  • Adding an API key to an XML file – XML is another one of those things that’s very easy, provided you know how.

Well, I learned.

For a little extra drama, I should let you know how close this project came to helping me fail a course this semester. I decided to use Stripe in a project that required the use of a REST API in a .NET application.

I was motivated to master Stripe. But I couldn’t figure it out. I looked for “Stripe tutorial” on youtube, and got videos about nail polish. I googled everything I could think of, but even when something looked helpful, my rising panic made it difficult to comprehend. The assignment was 30% of the course mark. I did badly on the midterm and had low hopes for the final. On the due date, I had nothing to show for 2 weeks of work, had given up panicking, and almost given up altogether.

But the teacher decided not to show up that day, and we were given another week. That night, Mark uploaded the tutorial I linked to. It looks like it was sitting on his hard drive for a year before he got around to posting it, just in time to save me. I didn’t even look for it, youtube suggested it. From there, Stripe, ASP.NET, my Windows 7 installation,  and the rest of my life fell neatly into place, and the assignment is waiting for it’s A.

Thanks Mark. PS, I tried to buy you a beer, but I got an error, LOL! Message me if you fix it and see this.


For non-experts:

REST stands for Representational State Transfer, and is a standard method for taking information from another website, and using it on your own website.

API means Application Programming Interface. An API is a library of computer code that can be used to implement REST.

.NET is a big library of code provided by Microsoft to make it easier to write Microsoft-style software.

Visual Studio is a program that helps programmers tie all of the above methods together into an application or website.