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.