简体   繁体   English

C ++日志文件无法在Mac OSX上输出?

[英]C++ Logfile not outputting on Mac OSX?

I am trying to get my client which works with Linux and Windows to work with Mac. 我想让我的客户端可以在Linux和Windows上与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. 我在Xcode上使用终端版本C ++。

Here is my log code: 这是我的日志代码:

log.h 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 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 yields:不建议将字符串文字转换为“ char *”

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. 日志文件已创建,但是在harddrive / users /文件夹中创建。 How would I have it simply output to the Xcode project folder like with Visual Studio. 我将如何像Visual Studio一样简单地将其输出到Xcode项目文件夹。

I think you can deal with the 我认为您可以处理

Conversion from string literal to 'char *' is deprecated 从字符串文字到'char *'的转换已被弃用

by changing the methods from ones which take char * parameters: 通过更改采用char *参数的方法:

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

to ones which take const char * parameters 那些使用const char *参数的

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 我会尝试从构造函数中删除所有内容(使用#if 0 ... #endif),除了哑巴

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). 我使用了您上面提供的代码,将char*更改为const char* ,并且编译并运行良好(包括预期的输出)。 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. 如果您使用默认的XCode路径,则该路径将位于Library / Developer / Xcode / DerivedData / projectname-hash / Build / Products / Debug或/ Release中,具体取决于您是在Release模式还是Debug模式下进行构建。

I guess you could give a full path when you are creating the file eg /var/log/lantern.txt. 我想您可以在创建文件时提供完整路径,例如/var/log/lantern.txt。

Side question: why not to implement operator<< so you can call log << message << endl; 附带的问题:为什么不实现operator<<所以可以调用log << message << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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