繁体   English   中英

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

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

我想让我的客户端可以在Linux和Windows上与Mac一起使用。 我有一个日志类,因此我可以查看正在发生的情况并捕获错误,但是我的日志文件甚至没有输出。 日志文件是全局声明的,因此无论如何至少应输出日志文件头。 我在Xcode上使用终端版本C ++。

这是我的日志代码:

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;
}

当应用程序关闭时,我删除了一个断点,日志文件应该已经输出了一些东西。 我的所有日​​志命令中也有一个警告。 例如:

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

yields:不建议将字符串文字转换为“ char *”

日志文件的主要初始化仍然不使用此方法,而应输出该文件。

第一个问题解决了! 检查以下是否有其他错误:

编辑:看来我已经走了一点。 日志文件已创建,但是在harddrive / users /文件夹中创建。 我将如何像Visual Studio一样简单地将其输出到Xcode项目文件夹。

我认为您可以处理

从字符串文字到'char *'的转换已被弃用

通过更改采用char *参数的方法:

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

那些使用const char *参数的

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

编译器应该担心将字符串文字作为参数传递给可能试图更改它们的函数。

是否正在创建日志文件? 我会尝试从构造函数中删除所有内容(使用#if 0 ... #endif),除了哑巴

logfile << "logfile constructed";

减少破坏的方式。

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

我想您可以在创建文件时提供完整路径,例如/var/log/lantern.txt。

附带的问题:为什么不实现operator<<所以可以调用log << message << endl;

暂无
暂无

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

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