2017-12-10 13 views
0

私は自分のプログラムに関するデバッグ情報を得るために簡単なロガーをファイルに記録しようとしています。私は自分で作っているので、ライブラリを使わないでください。 const char *をコンストラクタに渡すとnullが返される

logging.cpp

#include <string.h> // String stuff 
#include <time.h> // Time 

#include "logging.hpp" 

// Cathooks main logging util 
CatLogger g_CatLogging("/tmp/nekohook.log"); 
//CatLogger g_CatLogging; 

CatLogger::CatLogger(const char* _file_path, bool _ptime) : ptime(_ptime) { 
    file_path = _file_path; 
} 
CatLogger::~CatLogger() { fclose(log_handle); } 

void CatLogger::log(const char* fmt, ...) { 

    // Basicly an init, because this cant be done on construct 
    if (log_handle == nullptr) { 
     log_handle = fopen(file_path, "w"); 
    } 

    // Print our time if needed 
    if (ptime) { 
     // Get our time 
     time_t current_time = time(0); 
     struct tm* time_info = localtime(&current_time); 

     // print it to a string 
     char timeString[10]; 
     strftime(timeString, sizeof(timeString), "%H:%M:%S", time_info); 

     // Print the time into the log 
     fprintf(log_handle, "%% [%s] ", timeString); 
    } 

    // Get the string we want to log 
    char buffer[1024]; 
    va_list list; 
    va_start(list, fmt); 
    vsprintf(buffer, fmt, list); 
    va_end(list); 

    // Write our log to the file 
    fprintf(log_handle, "%s\n", file_path, buffer); 
    fflush(log_handle); 

    // Push result var to a console here, if i ever make a console api 
} 

logging.hpp

#include <stdarg.h> // ... arg 
#include <stdio.h> // fopen(), fprint(), fputs() 

class CatLogger { 
public: 
    CatLogger(const char* _file_path, bool _ptime = false); 
    ~CatLogger(); 
    void log(const char* fmt, ...); // Use to log with 
private: 
    FILE* log_handle = 0; // Handle used to log to files with 
    const char* file_path; // Path to log file 
    const bool ptime; // Whether to print time 
}; 

// Use this to log 
extern CatLogger g_CatLogging; 

私は、ログ機能を使用し、それが失敗します。なぜ私は考えていない。入力のgdbから情報を得るために走ったときにクラッシュするダミー関数を作った。私はそれにfile_path変数を入力し、0x0を返します。私はなぜこれが起こるのか分からない、私はこれを使用しているライブラリとは別のサンプル実行可能ファイルを作った、そしてそれは完璧に動作する。これは私が図書館をリンクしているか、またはその欠如のためか?

私はロギングファイルに直接リンクしているライブラリです。 https://github.com/oneechanhax/nekohook/blob/master/src/util/logging.cpp

それはのconstのchar *が何らかの理由でパスされていないため、順番にあるファイルハンドルを返さないfopenの両方によるものでfprintf()でクラッシュします。

これをデバッグする方法を教えてください。または、私が自分自身を試して負けているので、これがどこに間違っていたかを指摘してください。

EDIT: 私はCatLogger::log

if (log_handle == nullptr) { 
    log_handle = fopen("/tmp/nekohook.log", "w"); 
} 

if (log_handle == nullptr) { 
    log_handle = fopen(file_path, "w"); 
} 

に次のように置き換えるとそれが機能するようになりましたが、私は今、他のログクラスのログの場所を変更カント...

EDIT2: ここにいくつかのデバッグ情報があります。何とかconst char * doesntはクラスに保存されます。潜在的なバグがたくさんあります

+3

「fopen」実装に独自の静的データがある場合は、静的初期化順序の失敗のように聞こえるため、動作させるためには初期化する必要があります。あなたのレポートは、それが何かのために失敗するように聞こえる。 – StoryTeller

+1

提案。 C++を使用している場合は、Cヘッダーファイルの代わりにC++ヘッダーファイルを使用します。 '#include '、 '#include '、 '#include 'となります。 – Eljay

+0

g_CatLoggingグローバルが呼び出されているときに、まだ初期化されていない可能性があります。 'main'が呼び出される前にロギングをしようとしていますか? – Eljay

答えて

0

...私が持っている主な問題...

example

はたぶん文字列が構築した後にゼロになる厥。

if (log_handle == nullptr) { 
    log_handle = fopen(file_path, "w"); 
    if(!log_handle) { 
     perror("File opening failed"); // check your console output. 
     return EXIT_FAILURE; 
    } 
} 


// Get the string we want to log 
char buffer[1024]; 
va_list list; 
va_start(list, fmt); 
vsprintf(buffer, fmt, list); // potential segmentation fault 
va_end(list); 

使用この代わりに

int vsnprintf(char* buffer, std::size_t buf_size, const char* format, va_list vlist); // (since C++11) 

そして、より多くのそれのプログラムがマルチスレッド化されています。

関連する問題