File: file.cpp

package info (click to toggle)
libwibble 1.1-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 1,040 kB
  • ctags: 2,721
  • sloc: cpp: 14,542; makefile: 196; perl: 87; sh: 26
file content (64 lines) | stat: -rw-r--r-- 1,402 bytes parent folder | download | duplicates (2)
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
#include <wibble/sys/macros.h>
#include <wibble/sys/filelock.h>
#include <wibble/log/file.h>
#include <wibble/exception.h>

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

namespace wibble {
namespace log {

FileSender::FileSender(const std::string& filename) : out(-1), name(filename)
{
	out = open(filename.c_str(), O_WRONLY | O_CREAT, 0666);
	if (out < 0)
		throw wibble::exception::File(filename, "opening logfile for append");
}

FileSender::~FileSender()
{
	if (out >= 0) close(out);
}

void FileSender::send(Level level, const std::string& msg)
{
#ifndef __gnu_hurd__
#ifdef POSIX // FIXME
	// Write it all in a single write(2) so multiple processes can log to
	// the same file
	sys::fs::FileLock lock(out, F_WRLCK);
#endif
#endif

    // Seek to end of file
    if (lseek(out, 0, SEEK_END) < 0)
        throw wibble::exception::File(name, "moving to end of file");

	// Write it all out
	size_t done = 0;
	while (done < msg.size())
	{
		ssize_t res = write(out, msg.data() + done, msg.size() - done);
		if (res < 0)
			throw wibble::exception::File(name, "writing to file");
		done += res;
	}

    // Write trailing newline
    while (true)
    {
        ssize_t res = write(out, "\n", 1);
        if (res < 0)
            throw wibble::exception::File(name, "writing to file");
        if (res > 0)
            break;
    }
}
	
}
}

// vim:set ts=4 sw=4: