File: log.cpp

package info (click to toggle)
pilercr 1.06%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 844 kB
  • sloc: cpp: 14,339; makefile: 67; sh: 36
file content (52 lines) | stat: -rwxr-xr-x 930 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
#include "pilercr.h"

static FILE *g_fLog = 0;

void SetLog()
	{
	bool Append = false;
	const char *FileName = ValueOpt("log");
	if (0 == FileName)
		{
		FileName = ValueOpt("loga");
		if (0 == FileName)
			return;
		Append = true;
		}

	g_fLog = fopen(FileName, Append ? "a" : "w");
	if (NULL == g_fLog)
		{
		fprintf(stderr, "\n*** FATAL ERROR *** Cannot open log file\n");
		perror(FileName);
		exit(1);
		}
	setbuf(g_fLog, 0);
	}

void Log(const char Format[], ...)
	{
	if (0 == g_fLog)
		return;

	char Str[4096];
	va_list ArgList;
	va_start(ArgList, Format);
	vsprintf(Str, Format, ArgList);
	fprintf(g_fLog, "%s", Str);
	if (g_FlushLog)
		fflush(g_fLog);
	}

void Out(const char Format[], ...)
	{
	if (0 == g_fOut)
		return;

	char Str[4096];
	va_list ArgList;
	va_start(ArgList, Format);
	vsprintf(Str, Format, ArgList);
	fprintf(g_fOut, "%s", Str);
	Log("%s", Str);
	}