summaryrefslogtreecommitdiff
path: root/src/multilog.cpp
blob: 64ff967d4976bdf5c313df01f0aa6cb56e0a737a (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
/***************************************************************************
                          multilog.cpp  -  description
                             -------------------
    begin                : Sat Sep 6 2003
    copyright            : (C) 2003 by Mike Buland
    email                : eichlan@yf-soft.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "multilog.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

#include "multilogchannel.h"

// This section is what we need to make this a singleton
// this makes this class easy to use from anywhere, without
// worrying about re-creating every output form and all of that crazy jazz
MultiLog *MultiLog::singleLog = NULL;

MultiLog *MultiLog::getLog()
{
	if( singleLog == NULL )
	{
		singleLog = new MultiLog;
		atexit( cleanup );
	}
	return singleLog;
}

void MultiLog::cleanup()
{
	if( singleLog != NULL )
	{
		delete singleLog;
		singleLog = NULL;
	}
}

MultiLog::MultiLog()
{
	lChannel = new LinkedList();
	rEntry = new RingList( 150 );
	nEntriesLost = 0;
}

MultiLog::~MultiLog()
{
	int nMax = lChannel->getSize();
	for( int j = 0; j < nMax; j++ )
	{
		((MultiLogChannel *)lChannel->getAt(j))->closeLog();
		delete ((MultiLogChannel *)lChannel->getAt(j));
	}
	delete lChannel;
	
	for( int j = 0; j < rEntry->getSize(); j++ )
	{
		delete (LogEntry *)rEntry->getAt( j );
	}
	delete rEntry;
}
/*
void MultiLog::Log( int nLevel, const char *lpFormat, ...)
{
	switch( nLevel )
	{
		default:
			break;
	}
	va_list ap;
	va_start(ap, lpFormat);

	vprintf( lpFormat, ap );

	va_end(ap);
}*/

void MultiLog::DetailLog( int nLevel, const char *lpFile, int nLine, const char *lpFunction, const char *lpFormat, ...)
{
	LogEntry *e = new LogEntry();

	va_list ap;
	va_start(ap, lpFormat);
	char *text;
	vasprintf( &text, lpFormat, ap );
	va_end(ap);
	
	time( &e->xTime );
	e->nLevel = nLevel;
	e->nLine = nLine;
	e->lpFile = new char[strlen(lpFile)+1];
	strcpy( e->lpFile, lpFile );
	e->lpText = new char[strlen(text)+1];
	strcpy( e->lpText, text );
	free( text );
	
	append( e );
}

void MultiLog::append( LogEntry *pEntry )
{
	rEntry->append( pEntry );
	if( rEntry->getPushBuf() )
	{
		delete (LogEntry *)rEntry->getPushBuf();
		nEntriesLost++;
	}

	for( int j = 0; j < lChannel->getSize(); j++ )
	{
		((MultiLogChannel *)lChannel->getAt( j ))->append( pEntry );
	}
}

void MultiLog::addChannel( MultiLogChannel *pChannel )
{
	lChannel->append( pChannel );
	
	pChannel->openLog();
	
	for( int j = 0; j < rEntry->getSize(); j++ )
	{
		pChannel->append( (LogEntry *)rEntry->getAt( j ) );
	}
}

MultiLog::LogEntry::~LogEntry()
{
	delete[] lpFile;
	delete[] lpText;
}