summaryrefslogtreecommitdiff
path: root/src/multilogtext.cpp
blob: be6459526d58859ba002f47d5fc246f7d3e718f8 (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

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include "multilogtext.h"

MultiLogText::MultiLogText( const char *sFileName, const char *lpFormat )
{
	this->lpFormat = NULL;
	nFD = open( sFileName, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH );
	setLogFormat( lpFormat );
}

MultiLogText::MultiLogText( int nFileDesc, const char *lpFormat )
{
	this->lpFormat = NULL;
	nFD = nFileDesc;
	setLogFormat( lpFormat );
}

MultiLogText::~MultiLogText()
{
	if( nFD != -1 )
	{
		close( nFD );
	}
	
	delete[] lpFormat;
}

bool MultiLogText::setLogFormat( const char *lpFormat )
{
	char buf[200];
	int k = 0;
	static char fmts[10][4]={
		{'y', 'd', '0', '1'},
		{'m', 'd', '0', '2'},
		{'d', 'd', '0', '3'},
		{'h', 'd', '0', '4'},
		{'M', 'd', '0', '5'},
		{'s', 'd', '0', '6'},
		{'l', 'd', '0', '7'},
		{'f', 's', '0', '8'},
		{'L', 'd', '0', '9'},
		{'t', 's', '1', '0'},
	};
	
	for( int j = 0; lpFormat[j] != '\0'; j++ )
	{
		if( lpFormat[j] == '%' )
		{
			buf[k++] = '%';
			int nPlace = k++;
			k++;
			buf[k++] = '$';
			bool bDone = false;
			for( j++; bDone == false; j++ )
			{
				int l;
				for( l = 0; l < 10; l++ )
				{
					if( lpFormat[j] == fmts[l][0] )
					{
						buf[nPlace] = fmts[l][2];
						buf[nPlace+1] = fmts[l][3];
						buf[k++] = fmts[l][1];
						bDone = true;
						break;
					}
				}
				if( l == 10 )
				{
					buf[k++] = lpFormat[j];
				}
			}
			j--;
		}
		else
		{
			buf[k++] = lpFormat[j];
		}
	}
	buf[k++] = '\n';
	buf[k] = '\0';
	
	if( this->lpFormat != NULL )
	{
		delete[] this->lpFormat;
	}
	this->lpFormat = new char[k+1];
	strcpy( this->lpFormat, buf );

	return true;
}

bool MultiLogText::openLog()
{
	if( nFD == -1 )
	{
		return false;
	}
	return true;
}

bool MultiLogText::append( MultiLog::LogEntry *pEntry )
{
	if( nFD == -1 )
	{
		return false;
	}

	char *line = NULL;
	struct tm *pTime;
	pTime = localtime( &pEntry->xTime );
	asprintf(
		&line,
		lpFormat,
		pTime->tm_year+1900,
		pTime->tm_mon+1,
		pTime->tm_mday,
		pTime->tm_hour,
		pTime->tm_min,
		pTime->tm_sec,
		pEntry->nLevel,
		pEntry->lpFile,
		pEntry->nLine,
		pEntry->lpText
		);
	write( nFD, line, strlen(line) );
	free( line );

	return true;
}

bool MultiLogText::closeLog()
{
	if( nFD == -1 )
	{
		return false;
	}
	// Don't close it if it's sdtout or errorout
	if( nFD > 2 )
	{
		close( nFD );
	}
	nFD = -1;
	return true;
}