Me, myself and I – A short biography

Please note that this is incomplete and I’m still working on it.

Prefix

I am about to turn 51, and recently been diagnosed with probable dysthymia. That have made me think about myself and my life, and made me write this short biography about myself.

Childhood

I was born in January of 1973 to what can only be described as two working class parents. My dad didn’t stick around for a long time (but we still have a good relationship), so I was left to be raised by a working single mom. As she was still in her early twenties, she still went out to party and dance with friends, but she would then take me to my maternal grandmother for me to sleep over. We would go there most weekends anyway, so I spent quite a lot of time there. My uncle lived there as well, and we started to take long walks in the forests and fields around the area. Most of it was owned by the military and they had a large compound just a little bit away. So I often found used shell casings or cartridges which I for some reason collected and brought with me to my grandmothers house where they were stored in a drawer in a cabinet. I had another uncle living close by, and he had a son in my own age and I used to go there to play with him quite often, or he would come to us. I was rather young but I still was allowed to walk the kilometer or so to their house alone. I also often got a little money by my grandmother and would walk alone to the kiosk a couple of hundred meters away, to buy candy. Salty and sour was my favorites, and still are. The apartment we lived in was a simple two-bedroom apartment, and I got the big room. It was in an area with a lot of other families, with plenty of playgrounds and children around. You could easily go around the whole complex without crossing any roads, so pretty safe as well. Our bedrooms faced a forest which stretched for quite a bit (for a small child) and it was great for playing around in. Unfortunately there was one kid in my own age that decided that I could easily be teased, and unfortunately that was only the start of my missery. The first couple of years my daycare was a lady who had an apartment close to moms work, so me and a few other children spent our days there. After a while I got into a “proper” kindergarten where I stayed until I was six. Then I went to something called “lekis” (approximately “playing” in English) to prepare for school. While I have memories from kindergarten and school, “lekis” was rather uneventful and I don’t really remember much of it. It was at kindergarten that my life-long problems started. I was what you can only call stubborn, perhaps even obstinate. If I didn’t want to do something, I didn’t do it. Unfortunately that also was true for food and eating, and the teachers at kindergarten just couldn’t handle it in a good way. I could often be left alone at the table at lunch, while the other children went away to play, because the teachers needed me to eat my food and wouldn’t let me go. This made me want to each what they told me even less. And it still sticks with me to this day: I am a very picky eater, not even wanting to try new things (well, it happens, but very seldom, and only for very specific types of food).

School

In the autumn of 1980, when I was seven, I started first class in school. Here the problems from kindergarten was pretty much dwarfed by a couple of teaches who just could not handle a child with above average intelligence. You see, when I started school I could already read well. I might even have known a little math, and even if I didn’t I picked it up extremely easy. I learned to read already when I was around 3-4 years old, and sometimes brought books with me to kindergarten to read. The problem was that this was the age of the “everyone’s equal and so should be taught exactly the same and become average” type of school in Sweden. If you were below the average you got help to catch up, but if you were above the average you didn’t get any help at all. So the first few years of school was just simply boring, there was no challenges, nothing that could help me grow. That coupled with one of the two teachers picking on me, and me being bullied, also stuck around, and it didn’t take many years until I just couldn’t be bothered with school. I still have problems taking courses and classes. Just after I have started school, we (me and my mom) moved to a brand new apartment a little bit away. I could still walk to the old apartment without crossing any roads, still close to the forest and the school, so still a good neighborhood. However things soon took a turn for the worse… Suddenly a strange man moved into the apartment with us. It was quite hard for me, and I didn’t make his first time living with us very nice. Fortunately we soon started to get along, and as he is still married to mom we still see each other when I’m visiting. With him came other changes as well, the biggest was building a house in the countryside and moving there. The move also meant that I had to change school. For the first time I could not just walk to wherever I needed, I had to take a bus to go to school. That wasn’t really a big deal fortunately, and I enjoyed the walks to the bus-stop. What was worse that the bullying didn’t stop, and neither did my distaste for school in general. I still got a couple of friends, even if no-one was very close. As a side-note, one of the bullies (the worst one) whom I really hated at first, I soon just despised. The reason was that he only bullied me when others were around, once we were alone on our own he tried to suck up to me like he never bullied me at all. He still continue to bully me, but I couldn’t care less about him anymore after that. After grade 6, in 1986, we had to go to the last three grades in another school quite a bit away. The bullying didn’t stop completely, but as school took more time and effort it died down quite a bit. But by then it was really too late for me, the insecurities and mental trauma never went away and I still have those scars inside of me, still affecting my life. By now my distaste for school started to bloom, and I started to skip classes. I even skipped whole days sometimes, staying at home without mom or my stepdad knowing (as far as I know… :-)). Other than that things were better than they had been for a long time, and my memories of those years are that it was rather decent. One of my strongest memories from there are the smell of the sugar refinery. In the autumn when the sugar-beet harvest season is upon us, and the closest (and these days Sweden’s only) sugar-refinery is running, when the wind is right I can feel the smell and it brings me right back to these years. After elementary school ended I went to “gymnasium” (upper secondary school) where I was studying technical engineering. Lots of math, some chemistry, biology, a little architecture, and other related subjects. I really wanted to study, but the spark just wasn’t there. All the previous years just had destroyed it. Worst however was the German as a third language studies I elected. I just couldn’t stand the teacher. Our chemistry was just wrong, and at the end she told me that I wouldn’t be failed if I just came to the very last class. Other than that I got some new friends that I spent some time with, but by then also my insecurities was in full effect so I just couldn’t trust some of them completely. I still spent time and partied with them, but whenever I heard talk that some was doing things without me it felt like they just din’t want me. A feeling I can still feel some times even now. And talking about partying, this was the time when started to drink alcohol. And drank we did. I had blackouts, I got sick, I got lift home once by the police. And we drank the cheapest and worst drinks known to man, either because they were cheap or because it had a lot of alcohol. We had rather fun though. In my second year here, I rented a room at my paternal grandmother, and moved away from home part-time. Still went home on weekends and hiatuses and between terms. In my third year I opted for electrics and electronics. For my fourth and last year I had to move to Stockholm. There I first lived with my stepdads parents and later shared an apartment with a couple of guys that was also from Gotland. I was lucky that one of the guys from my earlier class also went together to study the same, so I had at least someone I knew, because by then I had rather large problems making friends. This year I studied computers and communication. However I didn’t finish. As before I just couldn’t be bothered with classes that didn’t interest me, and I knew I would be failed in some classes. So I just packed my things and went back home to Gotland a week early.

Intro for sci-fi / horror movie

A gang of friends are walking in a city. One of the friends drops a little behind the others, and sees some kids lying back down just next to each other on a patch of grass (in a park or similar). A small ball (baseball?) comes rolling and cuts straight through the kids bodies. Some mysterious men come and wrap the remains of the kids bodies in some kind of plastic and hurls them up into the sky, and the bags just whooshes of with extremely high speed, and the men disappear. All in all it takes just a couple of seconds. The guy is shocked, and runs towards his friends. Then he see a football (soccer ball) come rolling toward his friends. He shouts to them to not touch it. They look at him and laugh, as one of the friends stops it with his foot. Unharmed. A second ball comes rolling and a second friend laughs and stops it as well, but the ball just continues straight through cutting of the leg (foot disappears). As the friend falls over screaming a couple of mysterious men runs over, wraps the stump in the plastic-like material, and grabs the friend throwing him into the air where he just speeds of. Then the men runs away just as quickly as they came. Again it’s all over in a couple of seconds.

Simple function-call tracing in C++

There are many ways to do function-call tracing in C++, where the simplest method is to just print "Entering function X" when entering the function, and right before returning print "Leaving function X". It’s a lot of work though, especially if the function have multiple return statements.

One solution to the problem above is to use a trace object. You create a class whose constructor prints the entering message, stores the function name, and whose destructor prints the leaving message. Then create an instance of this class as a local variable. When the object is instantiated, which happens when the function is called, the enter message will be printed. And when the function leave, no matter when or where or how it happens, the object will be destructed and the leave message will be printed.

Now how should such a class look like? It could be something simple like this:

struct tracer
{
    std::string name_;  // Name of the function

    tracer(std::string const& name)
        : name_(name)
    {
        std::clog << "Entering function " << name_ << std::endl;  // Flushing is important
    }

    ~tracer()
    {
        std::clog << "Leaving function " << name_ << std::endl;  // Flushing is still important
    }
};

That’s the basic of what is needed.

Now to use it is very simple:

void some_class::some_member_function()
{
    tracer _unique_name_{"some_class::some_member_function");

    // Lots of code and multiple `return` statements...

}

That is the basic usage. It has some drawbacks, in that tracing will always be enabled. It’s seldom needed for release builds for example, so only using it when a _DEBUG macro is defined is a good start. It might be even better to have a special TRACING_ENABLED macro so it can be enabled even in release builds which can be useful some times. Extra logic could also be added to check for flags set at run-time.


Here is an example full solution that uses preprocessor macros to enable and disable tracing at the time of compilation.

#pragma once
#ifndef TRACING_H
#define TRACING_H

#include <string>
#include <iostream>
#include <iomanip>

// Simple structure to handle function-call tracing.

// On debug builds, always build with tracing enabled unless explicitly disabled
#if defined(_DEBUG) && !defined(TRACING_DISABLED)
# define TRACING_ENABLED
#endif

// Define a preprocessor macro to help with the tracing
#ifdef TRACING_ENABLED
# define TRACE() tracing::tracer _tracer_object__ ## __COUNTER__ {__func__, __FILE__, __LINE__}
#else
# define TRACE() // Nothing
#endif

#ifdef TRACING_ENABLED
namespace tracing
{
    class tracer
    {
    public:
        tracer() = delete;  // Disallow default construction
        tracer(tracer const&) = delete;  // Disallow copy construction
        tracer(tracer&&) = delete;  // Disallow move construction
        tracer& operator=(tracer const&) = delete;  // Disallow copy assignment
        tracer& operator=(tracer&&) = delete;  // Disallow move assignment

        tracer(std::string const& fun, std::string const& file, int const line)
            : function_name{fun}, file_name{file}, line_number{line}
        {
            std::clog << "TRACE: Entering function " << function_name << " (" << file_name << ':' << line_number << ')' << std::endl;
        }

        ~tracer()
        {
            std::clog << "TRACE: Leaving function " << function_name << std::endl;
        }

    private:
        std::string function_name;
        std::string file_name;
        int         line_number;
    };
}
#endif // TRACING_ENABLED

#endif // TRACING_H

Sample program using the above header file:

#include "tracing.h"

struct foo
{
    int bar(int value)
    {
        TRACE();

        if (value < 10)
            return value * 2;
        else
            return value * 3;
    }
};

int main()
{
    TRACE();

    foo my_foo;
    my_foo.bar(20);
    my_foo.bar(5);
}

The output from the program as shown above could be something like

TRACE: Entering function main (/home/X/Y/main.cpp:18)
TRACE: Entering function bar (/home/X/Y/main.cpp:7)
TRACE: Leaving function bar
TRACE: Entering function bar (/home/X/Y/Testing/main.cpp:7)
TRACE: Leaving function bar
TRACE: Leaving function main

Output may vary depending on the compiler being used.