summaryrefslogtreecommitdiff
path: root/zen/debug_log.h
blob: 43b2fea32de9e8013933b1cef1eafaefb1a7ebf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) ZenJu (zhnmju123 AT gmx DOT de) - All Rights Reserved    *
// **************************************************************************

#ifndef DEBUG_LOG_HEADER_017324601673246392184621895740256342
#define DEBUG_LOG_HEADER_017324601673246392184621895740256342

#include <string>
#include <cstdio>
#include <memory>
#include "deprecate.h"
#include "string_tools.h"
#include "time.h"


//small macro for writing debug information into a logfile
#define WRITE_LOG(x) globalLogFile().write(__FILE__, __LINE__, x);

//speed alternative: wxLogDebug(wxT("text")) + DebugView


namespace zen
{
#ifdef FFS_WIN
const char ZEN_FILE_NAME_SEPARATOR = '\\';

#elif defined FFS_LINUX
const char ZEN_FILE_NAME_SEPARATOR = '/';

#else
#error specify platform!
#endif


class DebugLog
{
public:
    class LogError {};

    ZEN_DEPRECATE
    DebugLog(const std::string& filePrefix = std::string()) :
        filename(filePrefix + "DEBUG_" + formatTime<std::string>("%Y-%m-%d %H%M%S") + ".log"),
        rowCount(0),
        handle(std::fopen(filename.c_str(), "w")) //Windows: non binary mode: automatically convert "\n" to "\r\n"; Linux: binary is default!
    {
        if (!handle)
            throw LogError();
    }

    ~DebugLog() { std::fclose(handle); }

    void write(const std::string& sourceFile,
               int sourceRow,
               const std::string& message)
    {
        const std::string logEntry = "[" + formatTime<std::string>(FORMAT_TIME) + "] " + afterLast(sourceFile, ZEN_FILE_NAME_SEPARATOR) +
                                     ", line " + toString<std::string>(sourceRow) + ": " + message + "\n";

        const size_t bytesWritten = ::fwrite(logEntry.c_str(), 1, logEntry.size(), handle);
        if (std::ferror(handle) != 0 || bytesWritten != logEntry.size())
            throw LogError();

        if (std::fflush(handle) != 0)
            throw LogError();

        ++rowCount;
    }

    size_t getRows() const { return rowCount; }

    std::string getFileName() const { return filename; }

private:
    std::string filename;
    size_t rowCount;
    FILE* handle;
};


inline
DebugLog& globalLogFile()
{
    static std::unique_ptr<DebugLog> inst(new DebugLog); //external linkage despite header definition!

    if (inst->getRows() > 50000) //prevent logfile from becoming too big
    {
        const std::string oldName = inst->getFileName();
        inst.reset();
        std::remove(oldName.c_str()); //unchecked deletion!
        inst.reset(new DebugLog);
    }

    return *inst;
}
}

#endif //DEBUG_LOG_HEADER_017324601673246392184621895740256342
bgstack15