File: file.cpp

package info (click to toggle)
libwibble 0.1.19
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 832 kB
  • ctags: 1,940
  • sloc: cpp: 9,798; makefile: 163; perl: 84; sh: 11
file content (44 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download
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
#include <wibble/log/file.h>
#include <wibble/exception.h>
#include <stdio.h>
#include <time.h>

namespace wibble {
namespace log {

FileSender::FileSender(const std::string& filename) : out(0), name(filename)
{
	out = fopen(filename.c_str(), "at");
	if (!out)
		throw wibble::exception::File(filename, "opening logfile for append");
}

FileSender::~FileSender()
{
	if (out) fclose((FILE*)out);
}

void FileSender::send(Level level, const std::string& msg)
{
	time_t now = time(NULL);
	struct tm pnow;
	localtime_r(&now, &pnow);
	char timebuf[20];
	/*
	 * Strftime specifiers used here:
	 *	%b      The abbreviated month name according to the current locale.
	 *	%d      The day of the month as a decimal number (range 01 to 31).
	 *	%e      Like %d, the day of the month as a decimal number, but a
	 *			leading zero is replaced by a space. (SU)
	 *	%T      The time in 24-hour notation (%H:%M:%S). (SU)
	 */
	strftime(timebuf, 20, "%b %e %T", &pnow);
	fprintf((FILE*)out, "%s: %s\n", timebuf, msg.c_str());
	if (level >= WARN)
		fflush((FILE*)out);
}
	
}
}

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