简体   繁体   中英

C++ Logfile not outputting on Mac OSX?

I am trying to get my client which works with Linux and Windows to work with Mac. I have a log class so I can see what is going on and catch errors but my logfile isn't even outputting. The logfile is declared globally so it should at least output the logfile header regardless. I am using the terminal version C++ with Xcode.

Here is my log code:

log.h

#ifndef LOG_H
#define LOG_H

#include <fstream>

using namespace std;

class Log
{
public:

        // Constructor / Destructor
        Log();
        ~Log();

        // Class functions
        void writeNewline();
        void writeError(char * text,...);
        void writeSuccess(char * text,...);


private:

        ofstream logfile;
};

#endif

log.cpp

#include <ctime>
#include <stdarg.h>

#include "log.h"

const int BUFFER_SIZE = 1024;

using namespace std;

Log::Log()
{
    // Create a log file for output
    logfile.open ("lanternlog.txt", ios::out);

    // Grab the current system time
    time_t t = time(0);
    struct tm * now = localtime( & t );

    // TODO: Format the time correctly

    // Insert the time and date at the top
    logfile << "<---> Logfile Initialized on " << now->tm_mon + 1 << "-" << 
            now->tm_mday << "-" << now->tm_year + 1900 << " at " << now->tm_hour <<
            ":" << now->tm_min << ":" << now->tm_sec << endl;
}

// Destructor
Log::~Log()
{
    // Close the logfile
    logfile.close();
}

void Log::writeError(char * text,...)
{
    // Grab the variables and insert them
    va_list ap;
    va_start(ap, text);
    char buff[BUFFER_SIZE];
    vsnprintf(buff, sizeof(buff), text, ap);

    // Output to the log
    logfile << "<-!-> " << buff << endl;
}

void Log::writeSuccess(char * text,...)
{
    // Grab the variables and insert them
    va_list ap;
    va_start(ap, text);
    char buff[BUFFER_SIZE];
    vsnprintf(buff, sizeof(buff), text, ap);

    // Output to the log
    logfile << "<---> " << buff << endl;
}

void Log::writeNewline()
{
    // Create a new line in the logfile
    logfile << endl;
}

When the application shuts down, and I have dropped a breakpoint, the logfile should have already output something. There is also a warning with all of my log commands. For instance:

errorLog.writeSuccess("Fatal Error: Unable to initialize Lantern!");

yields: Conversion from string literal to 'char *' is deprecated

Still, the main initialization of the logfile does not use this method and should output the file.

First question was solved! Check below for other error:

Edit: It seems I have gotten a tiny bit further. The logfile is created, but is created in the harddrive/users/ folder. How would I have it simply output to the Xcode project folder like with Visual Studio.

I think you can deal with the

Conversion from string literal to 'char *' is deprecated

by changing the methods from ones which take char * parameters:

void writeError(char * text,...);
void writeSuccess(char * text,...);

to ones which take const char * parameters

void writeError(const char * text,...);
void writeSuccess(const char * text,...);

The compiler should be worried about passing string literals as parameters to functions which could try to change them.

Is the logfile being created? I'd try removing everything (using #if 0 ... #endif) from the constructor except for a dumb

logfile << "logfile constructed";

to reduce the number of ways it can break.

i used the code you provided above, changed char* to const char* and it compiled and run fine (including the expected output). The log file will be created in the place where the executable is. If you are using the default XCode paths, this will be somewhere in Library/Developer/Xcode/DerivedData/projectname-hash/Build/Products/Debug or /Release depending on whether you build in Release or Debug mode.

I guess you could give a full path when you are creating the file eg /var/log/lantern.txt.

Side question: why not to implement operator<< so you can call log << message << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM