C++: function to append file, called within other functions

So, I am trying to log information about the status of the c++ project code in a text file. The program terminates unexpectedly, so I need to append the file as I go rather than storing info in an array along the way. I wanted to call the function to write to the file from within other functions, eventually in the other c++ files as well.

The code is a huge project that has many files and the "main()" technically exists in a separate file from all of the functions that are called throughout the function of the code (therefore not a useful file for me). My plan was to open the file in the setup() function, and then call the function within other functions along the way. Just in case I did not explain the setup of the code well enough, here is the link to the file I am trying to add to: https://github.com/cstracq2/ardupilot/blob/master/ArduCopter/ArduCopter.cpp

I have seen other notes on what may help, but I am not that familiar with c++ and I don't know what most of it means. From what I saw, this is one of the ways I tried, and it is failing to compile.

#include "<existing header>.h"
#include <fstream>
#include <iostream>

void log_data( ofstream &datafile, int value);

void <>::function1()
{ ....<stuff that was already there>
    log_data( datafile, <value> );
}

void <>::function2()
{ ....<stuff that was already there>
    log_data( datafile, <value> );
}

void setup()
{ ....<stuff that was already there>
    ofstream datafile;
    datafile.open("data_log_file.txt");
}

void log_data( ofstream &datafile, int value)
{
    data_file << value << endl;
}

If there is any advice that you could give me, I would really appreciate it.

Upvotes: 0

Views: 227

Answers (2)

asalic
asalic

Reputation: 664

In your case I would suggest to use the Singleton Pattern. Here is an example of how you could do it:

class Logger
{
    std::ifstream logF;
    static Logger *s_instance;
    Logger(std::string &path)
    {
        logF.open(path, std::ios_base::in);
    }
public:
    void log_data(int val)
    {
        logF << val << std::endl;
    }

    static void create_instance(std::string &path)
    {
        s_instance = new Logger(path);
    }
    static Logger *instance()
    {                
        return s_instance;
    }
};

Now you can just include the header with the class def and call something like:

Logger::instance()->log_data(<value>);

And do not forget to init the class before calling the static method (somewhere in main for instance):

Logger::create_instance(<path>);

Of course, you can just make it easier by hard-coding a value for your path, but if the path changes you'll have to re-compile everything.

Or just use something already implemented like log4cpp

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409404

Ah yes now that you mentioned the use of datafile in other function I see the error: The variable datafile is a local variable inside the setup function.

It should either be a member variable or possible a global variable.

Upvotes: 0

Related Questions