summaryrefslogtreecommitdiff
path: root/util/logfile.cpp
blob: 0386a592247e15649ec2f3c6324193f1dfeb9ea3 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// @file logfile.cpp simple file log writing / journaling

/**
*    Copyright (C) 2008 10gen Inc.
*
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "pch.h"
#include "logfile.h"
#include "text.h"
#include "mongoutils/str.h"
#include "unittest.h"

using namespace mongoutils;

namespace mongo {
    struct LogfileTest : public UnitTest {
        LogfileTest() { }
        void run() {
            if( 0 && debug ) {
                try {
                    LogFile f("logfile_test");
                    void *p = malloc(16384);
                    char *buf = (char*) p;
                    buf += 4095;
                    buf = (char*) (((size_t)buf)&(~0xfff));
                    memset(buf, 'z', 8192);
                    buf[8190] = '\n';
                    buf[8191] = 'B';
                    buf[0] = 'A';
                    f.synchronousAppend(buf, 8192);
                    f.synchronousAppend(buf, 8192);
                    free(p);
                }
                catch(DBException& e ) {
                    log() << "logfile.cpp test failed : " << e.what() << endl;
                    throw;
                }
            }
        }
    } __test;
}

#if defined(_WIN32)

namespace mongo {

    LogFile::LogFile(string name) : _name(name) {
        _fd = CreateFile(
                  toNativeString(name.c_str()).c_str(),
                  GENERIC_WRITE,
                  FILE_SHARE_READ,
                  NULL,
                  OPEN_ALWAYS,
                  FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
                  NULL);
        if( _fd == INVALID_HANDLE_VALUE ) {
            DWORD e = GetLastError();
            uasserted(13518, str::stream() << "couldn't open file " << name << " for writing " << errnoWithDescription(e));
        }
        SetFilePointer(_fd, 0, 0, FILE_BEGIN);
    }

    LogFile::~LogFile() {
        if( _fd != INVALID_HANDLE_VALUE )
            CloseHandle(_fd);
    }

    void LogFile::synchronousAppend(const void *buf, size_t len) {
        assert(_fd);
        DWORD written;
        if( !WriteFile(_fd, buf, len, &written, NULL) ) {
            DWORD e = GetLastError();
            if( e == 87 )
                massert(13519, "error appending to file - misaligned direct write?", false);
            else
                uasserted(13517, str::stream() << "error appending to file " << errnoWithDescription(e));
        }
        else {
            dassert( written == len );
        }
    }

}

#else

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

namespace mongo {

    LogFile::LogFile(string name) : _name(name) {
        _fd = open(name.c_str(),
                   O_CREAT
                   | O_WRONLY
#if defined(O_DIRECT)
                   | O_DIRECT
#endif
#if defined(O_NOATIME)
                   | O_NOATIME
#endif
                   ,
                   S_IRUSR | S_IWUSR);
        if( _fd < 0 ) {
            uasserted(13516, str::stream() << "couldn't open file " << name << " for writing " << errnoWithDescription());
        }

    }

    LogFile::~LogFile() {
        if( _fd >= 0 )
            close(_fd);
        _fd = -1;
    }

    void LogFile::synchronousAppend(const void *b, size_t len) {
        const char *buf = (char *) b;
        assert(_fd);
        assert(((size_t)buf)%4096==0); // aligned
        if( len % 4096 != 0 ) {
            log() << len << ' ' << len % 4096 << endl;
            assert(false);
        }
        ssize_t written = write(_fd, buf, len);
        if( written != (ssize_t) len ) {
            log() << "write fails written:" << written << " len:" << len << " buf:" << buf << ' ' << errnoWithDescription() << endl;
            uasserted(13515, str::stream() << "error appending to file " << _fd  << ' ' << errnoWithDescription());
        }

        if( 
#if defined(__linux__)
           fdatasync(_fd) < 0 
#else
           fsync(_fd)
#endif
            ) {
            uasserted(13514, str::stream() << "error appending to file on fsync " << ' ' << errnoWithDescription());
        }

    }

}

#endif